Reputation: 29
The JDK1.8 API saies the result of the Date(long date) is based on the time January 1, 1970, 00:00:00 GMT, but when I test it by set the date=0, I find the result is not the Thu Jan 01 08:00:00 CST 1970, it's not the 00:00:00,but the 08:00:00,why?the result about the Date(long date)
Upvotes: 1
Views: 77
Reputation: 339043
Instant.EPOCH.toString()
1970-01-01T00:00:00Z
Instant.EPOCH.atZone( ZoneId.of( "America/Chicago" ) ).toString() // Use proper time zone names (continent/region). Avoid pseudo-zones such as `CST`, which could be Central Standard Time in US or China Standard Time.
1969-12-31T18:00-06:00[America/Chicago]
Instant.EPOCH.atZone( ZoneId.of( "Asia/Hong_Kong" ) ).toString()
1970-01-01T08:00+08:00[Asia/Hong_Kong]
You are using you using old date-time classes, now legacy, supplanted by the java.time classes.
Among its many problems, the toString
method of java.util.Date
implicitly applies the JVM’s current default time zone while generating a string. Avoid this class.
Instead use Instant
. Represents a moment on the timeline in UTC with a resolution of nanoseconds.
Instant.ofEpochSecond( 0 ).toString()
1970-01-01T00:00:00Z
Or use the constant for that value, Instant.EPOCH
.
For current moment in UTC, call now
.
Instant.now()
To adjust into a specific time zone, search Stack Overflow for ZonedDateTime
and ZoneId
.
Adjust into another time zone. Same moment, same simultaneous point on the time line, but different wall-clock time.
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( "Asia/Hong_Kong" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
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.
Upvotes: 0
Reputation: 22442
It is because of the TimeZone
set, by default by the JVM when & where you run the program, look at the below statement from the Java doc.
Typically, you get a TimeZone using getDefault which creates a TimeZone based on the time zone where the program is running. For example, for a program running in Japan, getDefault creates a TimeZone object based on Japanese Standard Time.
You can look at here
Upvotes: 0
Reputation: 12840
It's because of your timezone.
If you change the time zone to GMT it will show 00:00:00
Date date = new Date(0L);
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
System.out.println(date);
Output : Thu Jan 01 00:00:00 GMT 1970
Upvotes: 1