Explain Codes LogoExplain Codes Logo

Converting between java.time.LocalDateTime and java.util.Date

java
time-zone-handling
date-time-api
java-8
Nikita BarsukovbyNikita Barsukov·Jan 15, 2025
TLDR

LocalDateTime to Date: Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()).

Date to LocalDateTime: date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().

Example:

// LocalDateTime to Date LocalDateTime ldt = LocalDateTime.now(); // it's always now.. until it's not Date fromLDT = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant()); // Date to LocalDateTime Date dt = new Date(); // All we have is now.. and this conversion LocalDateTime toLDT = dt.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();

Diving into the time tunnel

Dance with time-zones and daylight saving time

Converting between Dateand LocalDateTime, the default time zone is your dance floor. Keep watch on your steps during Daylight Saving Time (DST) leap. You might just trip over an hour! When times are critical, avoid the DST trap; stick to fixed time zones.

Rewinding the clock: Pre-1582

In the world before October 15, 1582, things were... different. The Julian calendar was the norm, not the Gregorian calendar of java.util.Date. The Java 8 LocalDateTime only speaks ISO calendar language, though. Tread lightly here. Converting historical dates? Could be rocky.

Skipping the time zone: Use java.sql.Timestamp

You're not always dancing with Date and LocalDateTime alone. Sometimes, java.sql.Timestamp cuts in. Useful when mixing Java 8 Date-Time API with JDBC. Check out these smooth moves:

// Alternative LocalDateTime to Date: Timestamp to the rescue! LocalDateTime ldt = LocalDateTime.now(); // This time was now.. a few CPU cycles ago Timestamp timestamp = Timestamp.valueOf(ldt); Date date = new Date(timestamp.getTime()); // Alternative Date to LocalDateTime Date dt = new Date(); // What's in a timestamp? All time is relative, after all. Timestamp timestamp = new Timestamp(dt.getTime()); LocalDateTime ldt = timestamp.toLocalDateTime();

Efficient integration: Old meets new

If your operations involve legacy systems using java.util.Date and the new cool kid LocalDateTime, here's how you finagle them together efficiently, still bearing the time zone in mind:

// Efficient LocalDateTime to Date LocalDateTime ldt = LocalDateTime.now(); // Converting the now to a more tangible format Instant instant = ldt.atZone(ZoneId.systemDefault()).toInstant(); Date date = Date.from(instant); // Efficient Date to LocalDateTime Date date = new Date(); // It was "that" date a moment ago Instant instant = date.toInstant(); LocalDateTime ldt = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();

Check your rear-view mirror: Reverse conversions

Do you know that feeling when returning home feels longer than going out? That's because LocalDateTime might play tricks on you. Check your reverse conversion paths. They might take a detour, especially when LocalDateTime forgets about the time zone.

Important notes to consider

Time zones are your friends (or enemies)

Remember those time-zone differences can be your best friend or your worst enemy. Use explicit time-zone handling methods to keep friends close, but enemies... accurately converted.

Conversion hurdles and their solutions

No time-zone information in LocalDateTime can lead to pitfalls. Maintain accuracy by choosing the explicit time-zone handling path via ZonedDateTime or Instant.

java.sql.Timestamp: The handyman

java.sql.Timestamp may feel like an artifact from the past, but it's the bridge between JDBC database interactions and Java 8 Date-Time operations. This bridge is crucial for maintaining legacy system integration.

Master the art of accurate time conversions

Working with time needs precision. The complexities of time manipulations demand thoroughly tested conversion methods for maintaining software robustness.