5
Date date = new Date(0L);

Shouldn't it give me a zero date? Like 00/00/0000? Gives me Wed Dec 31 19:00:00 EST 1969

sblundy
  • 60,628
  • 22
  • 121
  • 123
  • 9
    The date 00/00/0000 does not exist (the entire year '0' never existed). If software pretends it exists that's a bug. – beetstra Jun 22 '11 at 16:37
  • 4
    It all started with the Big Bang.. – Alexander Sabot Jun 22 '11 at 16:38
  • 2
    That's just a theory (hi Kaley!). And that would have been around 1/1/13,700,000,000 BC (I might be a few days off) – beetstra Jun 22 '11 at 16:49
  • 3
    There was no 0 day, 0 month or 0 year. ;) – Peter Lawrey Jun 22 '11 at 16:56
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Feb 13 '18 at 02:49

6 Answers6

26

Per the javadocs, this constructor on Date uses an offset from baseline time:

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.

Presumably you are on EST, hence the result.

As an aside, I would not expect the result you noted to be produced by any conceivable Date manipulation, since that's not even a valid date (month and day = 0).

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
5

From the Java API:

Date(long date): 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.

See: http://download.oracle.com/javase/1.4.2/docs/api/java/util/Date.html

FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
1

The time is milliseconds since the epoch.

The epoch is 1/1/1970 GMT.

Reverend Gonzo
  • 39,701
  • 6
  • 59
  • 77
1

Because that constructor creates the object 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.

Rob Goodwin
  • 2,676
  • 2
  • 27
  • 47
0

tl;dr

First moment of 1970 in UTC:

Instant.EPOCH.toString()

1970-01-01T00:00:00Z

And the same moment as seen in New York region:

Instant.EPOCH.atZone( ZoneId.of( "America/New_York" ) ).toString()

1969-12-31T19:00-05:00[America/New_York]

Details

The other Answers are correct: The epoch reference is first moment of 1970 in UTC while your time zone is 5 hours behind UTC. Here's updated info.

java.time

The modern approach uses java.time classes that supplanted the troublesome old legacy date-time classes.

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

The epoch reference of 1970-01-01T00:00:00Z is defined as a constant, Instant.EPOCH.

Instant.EPOCH.toString()

1970-01-01T00:00:00Z

To see that same moment in another time zone, apply a ZoneId to get a ZonedDateTime. You can think of it as: “ZonedDateTime = Instant + ZoneId”.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/New_York" ) ;  
ZonedDateTime zdt = Instant.EPOCH.atZone( z ) ;

zdt.toString(): 1969-12-31T19:00-05:00[America/New_York]

The result is a time-of-day five hours earlier, on last day of previous year. This ZonedDateTime represents the same moment, the same simultaneous point on the timeline, but a different wall-clock time.

That same moment in India is five and a half hours ahead of UTC rather than behind.

ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
ZonedDateTime zdt = Instant.EPOCH.atZone( z ) ;

    System.out.println(zdt );

zdt.toString(): 1970-01-01T05:30+05:30[Asia/Kolkata]


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

With a JDBC driver complying with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings or java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

Java dates, and most computer implementation of dates, are stored as milliseconds of seconds starting from what's called the epoch. That is 1 January, 1970. Java date is represented as milliseconds since epoch. and 0 milliseconds since epoch is the epoch itself. And that's why you get that value.

Raze
  • 2,175
  • 14
  • 30