Alex Rivera | Logout

Why would you instantiate a Date with -1 as the argument?

Asked 2012-06-03T20:14:23.267
21

I know this may be an obvious question, but I am looking at this code:

private Date lastActivity = new Date(-1);

And am curious what this does - the Date class has six different constructors, and only one can take a single argument of long, like so:

public Date(long date)

which:

Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

So is -1 just a placeholder? I appreciate any tips or advice.

Edit
Report

1 Answer

17

One would typically do this to get a "well known" Date, perhaps for validation or comparisons (all "real Dates" are greater than this one), as a "marker object" to indicate that the Date is really unknown/illegal/pending but you don't want to use null for some reason, or perhaps for a Unit Test.

O.K., looking up the more proper terms for what I called a "marker value", I get sentinel value, flag value, trip value, rogue value, signal value, or dummy data. see wikipedia article here

answered 2012-06-03T20:16:49.193

Your Answer