Migrating Joda time to Java 8
Joda:
UserObject user = new UserObject()
user.setCreatedAt(new DateTime(rs.getTimestamp("columnName")));`
Migrating to Java 8
This is my code; it does compile; I am doubtful if it works:
ZonedDateTime.ofInstant(rs.getTimestamp("columnName").toLocalDateTime().toInstant(ZoneOffset.UTC),ZoneId.of("UTC")));
In some cases, the date is wrong. Any advice?
Use the following:
You need to use a
ZoneId
appropriate to the region (you may try withZoneId.systemDefault()
for a start).For more details about the differences between various Java-Time APIs, see this great answer.
This seems to work:-
tl;dr
To track a moment in history, use
Instant
as the type of your class member variable. Specifically, this moment is seen as a date and time-of-day in UTC.Usage, capturing the current moment.
Usage, populating value from database.
JDBC 4.2 does not require support for
Instant
(a moment in UTC). If your driver does not support that class, switch toOffsetDateTime
which is required.Present to the user, localized for the user-interface.
Notice that
Locale
has nothing to do with time zone, an orthogonal issue. The code above might be for a business person from Québec who is traveling in New Zealand. She wants to see the wall-clock time used by the kiwis around her, but she prefers to read its textual display in her native French. Both time zone and locale are issues best left to presentation only; generally best to use UTC in the rest of your code. Thus, we defined our member variablecreatedAt
as anInstant
, withInstant
always being in UTC by definition.Avoid
java.sql.Timestamp
java.sql.Timestamp
, along withjava.sql.Date
,java.util.Date
, andCalendar
are all part of the terribly troublesome old date-time classes that were supplanted years ago by the java.time classes.java.time
As of JDBC 4.2 and later, we can directly exchange java.time objects with the database.
Instant
Send the current moment to the database using
Instant
class. TheInstant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).And retrieval.
ZonedDateTime
To see that same moment through the lens of the wall-clock time used by the people of a particular region (a time zone), apply a
ZoneId
to get aZonedDateTime
.LocalDateTime
is not a momentNever involve
LocalDateTime
class when representing a specific moment in time. The class purposely lacks any concept of time zone or offset-from-UTC. As such, it cannot represent a moment, is not a point on the timeline. It is a vague idea about potential moments along a range of about 26-27 hours (the range of time zones).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
.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.
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.