Explain Codes LogoExplain Codes Logo

How to convert ZonedDateTime to Date?

java
date-time
java-time
time-zone
Alex KataevbyAlex Kataev·Feb 16, 2025
TLDR

To convert ZonedDateTime to Date, do:

Date date = Date.from(zonedDateTime.toInstant());

This holds true where zonedDateTime is your ZonedDateTime instance. The Date.from() method flips the ZonedDateTime to a Date while upholding the same point in time.

But remember, the Date is always in UTC. If your ZonedDateTime is in a different zone, it will be adapted to UTC.

Clarifying time zones during conversion

Consistency in handling time zones is key when juggling with ZonedDateTime and Date. Always use the UTC time zone across all your systems, including Cassandra databases. Why, you may ask? This is to prevent awkward situations caused by differing time zones. Since Date doesn't store time zone information, you should convert your ZonedDateTime to UTC first, then to Date.

To do that, use this code:

Date date = Date.from(zonedDateTime.withZoneSameInstant(ZoneId.of("UTC")).toInstant());

The code first changes the time zone of the ZonedDateTime to UTC, and then converts it to a Date.

Be aware of the gotchas

Beware! Downscaling from ZonedDateTime to Date can cause lost millisecond precision. ZonedDateTime handles nanoseconds, while Date only goes as precise as milliseconds. While seemingly insignificant, if you're into high-precision sports like splitting atoms, this can be a stinker.

Also, Date objects are as mutable as zippers, so always avoid passing around the original instance - use immutable copies instead.

Staying modern with java.time

Wanna be hip and trendy in the Java world? Ditch old-school APIs like java.sql.Timestamp to avoid confusing time zone mix-ups. The java.time classes are your new best friends. Need to show off? Go for ThreeTen-Extra for more advanced date-time operations. It's like the secret sauce.

Setting up your server time zone

Imagine being a puppeteer with your fancy JVM and server time zone settings. You set both to UTC, and voila! No more unpredictable surprises at runtime. Use this piece of magic:

-Duser.timezone=UTC

This convinces the JVM to obey UTC time zone laws, breaking free from the chains of system defaults.

Playing nice with databases

Working with databases? Here's your new mantra - "Thou shalt use ISO 8601 formats for all date-time dealings". When feeding timestamps to Cassandra from your Java realm, keep them in UTC. Remember, databases too deserve the luxury of being timezone agnostic.

Fall back to the ThreeTen Backport

Older Java versions dragging you down? Impress your peers with the ThreeTen Backport library. Here's a code hors d'oeuvre for Java 6 & 7 or Android users:

org.threeten.bp.ZonedDateTime zdtThreeTen = // your ZonedDateTime object, with a sprinkle of Backport magic Date date = new Date(zdtThreeTen.toInstant().toEpochMilli());

Awww... Look at that ZonedDateTime becoming a Date again. They grow up so fast.

Exploring extra utility libraries

Who doesn't like extra goodies?! Check out DateTimeUtils.toDate from Apache Commons or similar utility libraries. It does not beg the difference, but nice to have options, eh?