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
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
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).
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
The time is milliseconds since the epoch.
The epoch is 1/1/1970 GMT.
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.
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]
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.
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]
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.
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.