While using util.date and giving date with time to service from browser and then saving to db and taking it back it gives different date time against setting zoneddatetime directly from service.
Any help would be appreciated..
While using util.date and giving date with time to service from browser and then saving to db and taking it back it gives different date time against setting zoneddatetime directly from service.
Any help would be appreciated..
Sixteen years ago, I wrote an article for JavaWorld magazine entitled What's Your Time Zone which dealt with the issue you describe when using class
java.util.Date
. In order to "see" the value of an instance of classjava.util.Date
, you have to convert it to aString
. Before java 1.8, this conversion always took the time zone into consideration as well. That's one of the reasons why, in Java 1.8, a new Date-Time API was introduced. New code that's written for Java versions 1.8 and above, should use the new API. I believe you should also consider converting old code to use the new API, if possible.java.util.Date
is an almost deprecated class which provide a wrapper around millisecond value. It does not provide any information about the time zone, hasn't any helpful methods and you should always avoid using it.ZonedDateTime
is a class from newJDK8
date API which provides a date-time with a time-zone in the ISO-8601 calendar system. You should checkOracle's explanation for new date format
tl;dr
Date
represents a moment in UTC, whileZonedDateTime
represents a moment in a particular time zone.Date
is a terrible class, riddled with design flaws, that should never be used, whileZonedDateTime
is a modern class from the java.time package that you will find quite useful.Java comes with two very different frameworks for handling date-time work: a terribly awkward and failed set of legacy classes, and an modern industry-leading set of classes found in the java.time package.
Legacy ➙ modern:
java.util.Date
was replaced byjava.time.Instant
java.util.GregorianCalendar
was replaced byjava.time.ZonedDateTime
java.util.Date
The
Date
class represents a moment in UTC. That is, a date, a time-of-day, plus the context of UTC.Internally, it is a count of milliseconds since the epoch reference date of first moment of 1970 in UTC, 1970-01-01T00:00:00Z.
To complicate matters:
equals
.toString
this class has the very confusing behavior of dynamically applying the JVM’s current time zone while generating text to represent the value of this object. While well-intentioned, this anti-feature has caused incalculable pain among Java programmers trying to learn date-time handling.Confused? Yes, this class is confusing, a wretched mess of poor design decisions. Compounded by the later addition of
java.util.Calendar
&GregorianCalendar
.All of these troublesome date-time classes bundled with the earliest version of Java are now supplanted entirely by the java.time classes.
In particular,
java.util.Date
is replaced byjava.time.Instant
. Both represent a moment in UTC as count from the epoch of 1970 UTC. ButInstant
carries a finer resolution, nanoseconds rather than milliseconds.You can convert back-and-forth between the legacy class
Date
and the modern classInstant
by calling new methods added to the old class. Usually, you will avoid ever usingDate
. But when interfacing with old code not yet updated to java.time, you may need to convert.java.time.ZonedDateTime
The modern class
ZonedDateTime
represent a moment as seen in the wall-clock time used by the people of a certain region (a time zone).So
Instant
andZonedDateTime
are similar in that they both represent a moment, a specific point on that timeline. The difference is thatZonedDateTime
knows about the rules of a time zone. So aZonedDateTime
knows how to account for anomalies such as Daylight Saving Time (DST) or other changes to time-keeping required by politicians.You can think of it as:
We can easily adjust from UTC to some time zone by applying a time zone (
ZoneId
) to aInstant
object.See this code run live at IdeOne.com. Note the different date and the different time-of-day, yet the same simultaneous moment.
Crucial concept:
Instant
andZonedDateTime
both represent the same moment, the same simultaneous point on the timeline. They differ in the wall-clock time. For example, if someone in Japan calls someone in Iceland (where UTC is used for their clocks all the time), and they both look up at the clock hanging on their respective walls, they will see a different time-of-day and possibly even a different date on the monthly calendar. Same moment, different wall-clock time.As for the legacy classes, the equivalent of
ZonedDateTime
isGregorianCalendar
, a concrete implementation ofjava.util.Calendar
. Indeed, the old classGregorianCalendar
gained new methods for conversion to/fromZonedDateTime
.…and…
Conclusion
So a
Date
is equivalent to aInstant
, both being a moment in UTC. But aZonedDateTime
differs from both in that a time zone has adjusted the perception of the moment by applying the lens of a region’s people’s wall-clock time adjustment.Tips:
Date
. When handed aDate
, immediately convert toInstant
. Then proceed with your business logic.Database
The Question mentions database work. Here is a quick summary. Search Stack Overflow for more detail as this has been handled many times already.
As of JDBC 4.2, we can directly exchange java.time objects with the database. Use
PreparedStatement::setObject
andResultSet::getObject
. No need to ever again touch the terriblejava.sql.*
classes such asjava.sql.Timestamp
.You may be able to exchange an
Instant
but the JDBC spec does not require that. The spec instead requiresOffsetDateTime
.Retrieval.
And storage.
If you have an
Instant
in hand, convert toOffsetDateTime
using the constantZoneOffset.UTC
.To view that moment through the wall-clock time of some region, apply a
ZoneId
.To store a
ZonedDateTime
to the database, convert toOffsetDateTime
. This strips off the time zone information (the history of past, present, and future changes made to the offset used by the people of this region as decided by their politicians), leaving the date, the time-of-day, and the offset-from-UTC (a number of hours-minutes-seconds).Most databases store a moment in UTC for a column of the SQL-standard type
TIMESTAMP WITH TIMESTAMP
. When submitting yourOffsetDateTime
to the database, your JDBC driver is likely to adjust the offset in theOffsetDateTime
to zero hours-minutes-seconds (to UTC itself). But I like do so explicitly. It makes debugging easier, and it demonstrates to the reader my understanding of the moment being stored in UTC.