简体   繁体   中英

Date.from(ZonedDateTime to instant) returning previous day

My variable in java of type ZonedDateTime is say time="2017-01-03T00:00Z[UTZ]" . And when i try to get Date from like this - Date.from(time.toInstance()) it returns previous day ie Mon Jan 02 19:00:00 EST 2017 , I dont know why ? Could anyone shed some light on my it returns previous day ?

Avoid legacy date-time classes

Never use java.util.Date class. That terrible class, along with Calendar & SimpleDateFormat and others are now legacy. The new to / from conversion methods added to the old classes are intended only for use when you are interoperating with old code not yet updated to java.time . Avoid Date whenever possible.

Among the many flaws in Date is its unfortunate behavior of dynamically applying the JVM's current default time zone while generating the text in its toString method. So it appears a Date has a time zone while actually a Date represents a moment in UTC. In other words, Date::toString lies. One of many reasons to avoid this class.

➥ In the winter of 2017, many of the time zones on the east coast of North America are five hours behind UTC. So midnight in UTC is simultaneously 7 PM (19:00) in New York, Montréal, etc. Same moment, different wall-clock time.

java.time

The Date class was supplanted by Instant years ago.

ZonedDateTime is say time="2017-01-03T00:00Z[UTZ]"

If you are trying to track moments in UTC , use either:

Use the ZonedDateTime class when you have a moment in the context of a time zone. A time zone is a history of the past, present, and future changes to the offset-from-UTC used by the people of a particular region.

ZonedDateTime.now( 
    ZoneId.of( "Africa/Tunis" )
)

You can adjust between UTC and a zone. Same moment, different ways to view it, different wall-clock times.

ZonedDateTime zdt = instant.atZone( ZoneId.of( "Pacific/Auckland" ) ) ;

…and…

Instant instant = zdt.toInstant() ;

Java中的日期时间类型表,包括现代和旧版


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 .

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

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

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for 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 .

ZonedDateTime.toInstant() adjusts a moment from a time zone to UTC. You end up with the same moment, different wall-clock time, and possibly a different date for the same simultaneous point on the timeline. What you are seeing is not a problem, not a discrepancy.

Classes like LocalDate and ZonedDateTime provide a human view on time. However, often we need to work with time viewed from a machine perspective. For this, we can use the Instant class which represents timestamps.

An Instant counts the time beginning from the first second of January 1, 1970 (1970-01-01 00:00:00) also called the EPOCH.

Instant values can be negative if they occurred before the epoch. They followISO 8601 the standard for representing date and time.

Also, use the Java Time API libraries introduced in Java 8 as there were many issues in the existing Date and Calendar APIs Please refer: https://www.baeldung.com/java-8-date-time-intro

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM