Java Reference ยท Date and Time
Converting legacy and modern date types
Convert between java.util.Date, Calendar, java.sql types and java.time classes: Date to Instant, to LocalDate, to ZonedDateTime, and back, with zone pitfalls.
Most real programs live in both worlds. A library hands you a java.util.Date, your own code wants a LocalDate, and the database driver wants a java.sql.Timestamp. Converting is easy once you remember one rule: Date and Instant are the same kind of thing (a point on the timeline), whereas LocalDate and LocalDateTime have no zone. Whenever you cross between a point in time and a local date, you must name a time zone.
The bridge methods live on the legacy classes (Date.toInstant(), Date.from(Instant), GregorianCalendar.toZonedDateTime(), Timestamp.toLocalDateTime()), so the conversion code is short.
Instant instant = date.toInstant(); LocalDate local = instant.atZone(ZoneId.of("Europe/Paris")).toLocalDate(); Date back = Date.from(local.atStartOfDay(ZoneId.of("Europe/Paris")).toInstant());
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
| From | To | Code |
|---|---|---|
java.util.Date |
Instant |
date.toInstant() |
Instant |
java.util.Date |
Date.from(instant) |
Date |
LocalDate |
date.toInstant().atZone(zone).toLocalDate() |
LocalDate |
Date |
Date.from(localDate.atStartOfDay(zone).toInstant()) |
GregorianCalendar |
ZonedDateTime |
cal.toZonedDateTime() |
ZonedDateTime |
GregorianCalendar |
GregorianCalendar.from(zdt) |
Calendar (any) |
Instant |
cal.toInstant() |
TimeZone |
ZoneId |
tz.toZoneId() |
ZoneId |
TimeZone |
TimeZone.getTimeZone(zoneId) |
java.sql.Date |
LocalDate |
sqlDate.toLocalDate(); reverse is java.sql.Date.valueOf(localDate) |
java.sql.Timestamp |
LocalDateTime |
ts.toLocalDateTime(); reverse is Timestamp.valueOf(ldt); also ts.toInstant() and Timestamp.from(instant) |
java.sql.Time |
LocalTime |
time.toLocalTime(); reverse is Time.valueOf(lt) |
Usage notes
java.sql.Date.toInstant()throws.java.sql.Dateandjava.sql.Timeoverride it to throwUnsupportedOperationException. CalltoLocalDate()ortoLocalTime()instead.Timestamp.valueOf(LocalDateTime)uses the default time zone (the JVM's), so the wrapped instant changes between machines, while the round trip throughtoLocalDateTime()stays consistent. PreferTimestamp.from(instant)and an explicit zone.- Choose a zone consciously.
ZoneId.systemDefault()is convenient but makes results depend on the machine. State the zone your business rules use. - Precision.
DateandCalendarkeep milliseconds;InstantandTimestampkeep nanoseconds. Converting toDatetruncates. - Prefer drivers with JDBC 4.2 support.
PreparedStatement.setObject(i, localDate)andResultSet.getObject(i, LocalDate.class)avoid the legacy types entirely. - Keep conversions at the boundary. Convert on input and output, and use
java.timeeverywhere in between. - Midnight may not exist.
atStartOfDay(zone)returns the first valid moment of the day, which can be 01:00 in zones whose daylight saving change skips midnight.