How to convert ZonedDateTime to Date?
I am trying to set a server agnostic date time in my database and I believe the best practice to do so is to set a UTC DateTime. My db server is Cassandra and the db driver for Java understands only the Date type.
So assuming that in my code I am using the new Java 8 ZonedDateTime to get the UTC now (ZonedDateTime.now(ZoneOffset.UTC)
), how can I convert this ZonedDateTime instance to the "legacy" Date class?
Solution 1:
You can convert ZonedDateTime to an instant, which you can use directly with Date.
Date.from(java.time.ZonedDateTime.now().toInstant());
Solution 2:
tl;dr
java.util.Date.from( // Transfer the moment in UTC, truncating any microseconds or nanoseconds to milliseconds.
Instant.now() ; // Capture current moment in UTC, with resolution as fine as nanoseconds.
)
Though there was no point in that code above. Both java.util.Date
and Instant
represent a moment in UTC, always in UTC. Code above has same effect as:
new java.util.Date() // Capture current moment in UTC.
No benefit here to using ZonedDateTime
. If you already have a ZonedDateTime
, adjust to UTC by extracting a Instant
.
java.util.Date.from( // Truncates any micros/nanos.
myZonedDateTime.toInstant() // Adjust to UTC. Same moment, same point on the timeline, different wall-clock time.
)
Other Answer Correct
The Answer by ssoltanid correctly addresses your specific question, how to convert a new-school java.time object (ZonedDateTime
) to an old-school java.util.Date
object. Extract the Instant
from the ZonedDateTime and pass to java.util.Date.from()
.
Data Loss
Note that you will suffer data loss, as Instant
tracks nanoseconds since epoch while java.util.Date
tracks milliseconds since epoch.
Your Question and comments raise other issues.
Keep Servers In UTC
Your servers should have their host OS set to UTC as a best practice generally. The JVM picks up on this host OS setting as its default time zone, in the Java implementations that I'm aware of.
Specify Time Zone
But you should never rely on the JVM’s current default time zone. Rather than pick up the host setting, a flag passed when launching a JVM can set another time zone. Even worse: Any code in any thread of any app at any moment can make a call to java.util.TimeZone::setDefault
to change that default at runtime!
Cassandra Timestamp
Type
Any decent database and driver should automatically handle adjusting a passed date-time to UTC for storage. I do not use Cassandra, but it does seem to have some rudimentary support for date-time. The documentation says its Timestamp
type is a count of milliseconds from the same epoch (first moment of 1970 in UTC).
ISO 8601
Furthermore, Cassandra accepts string inputs in the ISO 8601 standard formats. Fortunately, java.time uses ISO 8601 formats as its defaults for parsing/generating strings. The Instant
class’ toString
implementation will do nicely.
Precision: Millisecond vs Nanosecord
But first we need to reduce the nanosecond precision of ZonedDateTime to milliseconds. One way is to create a fresh Instant using milliseconds. Fortunately, java.time has some handy methods for converting to and from milliseconds.
Example Code
Here is some example code in Java 8 Update 60.
ZonedDateTime zdt = ZonedDateTime.now( ZoneId.of( "America/Montreal" ) );
…
Instant instant = zdt.toInstant();
Instant instantTruncatedToMilliseconds = Instant.ofEpochMilli( instant.toEpochMilli() );
String fodderForCassandra = instantTruncatedToMilliseconds.toString(); // Example: 2015-08-18T06:36:40.321Z
Or according to this Cassandra Java driver doc, you can pass a java.util.Date
instance (not to be confused with java.sqlDate
). So you could make a j.u.Date from that instantTruncatedToMilliseconds
in the code above.
java.util.Date dateForCassandra = java.util.Date.from( instantTruncatedToMilliseconds );
If doing this often, you could make a one-liner.
java.util.Date dateForCassandra = java.util.Date.from( zdt.toInstant() );
But it would be neater to create a little utility method.
static public java.util.Date toJavaUtilDateFromZonedDateTime ( ZonedDateTime zdt ) {
Instant instant = zdt.toInstant();
// Data-loss, going from nanosecond resolution to milliseconds.
java.util.Date utilDate = java.util.Date.from( instant ) ;
return utilDate;
}
Notice the difference in all this code than in the Question. The Question’s code was trying to adjust the time zone of the ZonedDateTime instance to UTC. But that is not necessary. Conceptually:
ZonedDateTime = Instant + ZoneId
We just extract the Instant part, which is already in UTC (basically in UTC, read the class doc for precise details).
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?
-
Java SE 8, Java SE 9, Java SE 10, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
-
Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
-
Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
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.