Joker Joker - 29 days ago 9
Java Question

something about the result of Date(long date )

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)

Answer

Avoid legacy date-time classes

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.

java.time

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.

Comments