Sub topic 18
Converting to and from Date, Calendar, and java.sql Types
Older Java code still uses java.util.Date, java.util.Calendar, and the java.sql date classes. You may need to pass a java.time value to a library that only accepts a Date, or read a Calendar that an old method returns. Java 8 added conversion methods to the old classes so you can move between the two worlds safely.
Instant Is the Bridge
A bridge type is a type that both the old and new APIs understand. For java.util.Date, that type is Instant. A Date and an Instant both stand for one point on the timeline, so the conversion loses no information.
date.toInstant()turns aDateinto anInstant.Date.from(instant)turns anInstantback into aDate.
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
public class Legacy {
public static void main(String[] args) {
Date legacy = new Date();
Instant instant = legacy.toInstant();
LocalDateTime local = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
Date back = Date.from(instant);
System.out.println(legacy.equals(back)); // true
System.out.println(local);
}
}To get a LocalDateTime, you must pick a time zone. A Date has no zone of its own, so the code above uses the computer's default zone.
Calendar and ZonedDateTime
A GregorianCalendar holds both a moment and a time zone. That makes ZonedDateTime its closest match.
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class CalendarBridge {
public static void main(String[] args) {
GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Paris"));
cal.clear();
cal.set(2024, Calendar.MARCH, 18, 9, 30);
ZonedDateTime zdt = cal.toZonedDateTime();
System.out.println(zdt); // 2024-03-18T09:30+01:00[Europe/Paris]
GregorianCalendar back = GregorianCalendar.from(zdt);
System.out.println(back.get(Calendar.HOUR_OF_DAY)); // 9
}
}In Calendar, months start at 0, so Calendar.MARCH is 2. That is one reason java.time replaced it.
The java.sql Types
Database code (JDBC) uses java.sql.Date for dates and java.sql.Timestamp for date-times. Both have direct conversions.
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
public class SqlBridge {
public static void main(String[] args) {
java.sql.Date sqlDate = java.sql.Date.valueOf(LocalDate.of(2024, 3, 18));
System.out.println(sqlDate); // 2024-03-18
System.out.println(sqlDate.toLocalDate()); // 2024-03-18
Timestamp ts = Timestamp.valueOf(LocalDateTime.of(2024, 3, 18, 9, 30));
System.out.println(ts); // 2024-03-18 09:30:00.0
System.out.println(ts.toLocalDateTime()); // 2024-03-18T09:30
}
}Quick Reference
| Legacy type | java.time type | To java.time | Back to legacy |
|---|---|---|---|
| `java.util.Date` | `Instant` | `date.toInstant()` | `Date.from(instant)` |
| `GregorianCalendar` | `ZonedDateTime` | `cal.toZonedDateTime()` | `GregorianCalendar.from(zdt)` |
| `java.sql.Date` | `LocalDate` | `sqlDate.toLocalDate()` | `java.sql.Date.valueOf(date)` |
| `java.sql.Timestamp` | `LocalDateTime` | `ts.toLocalDateTime()` | `Timestamp.valueOf(ldt)` |
| `java.sql.Timestamp` | `Instant` | `ts.toInstant()` | `Timestamp.from(instant)` |
Exercise
A legacy method returns a java.util.Date. You need a LocalDate for the user's zone, Asia/Tokyo. Write the expression.
Show answer
legacyDate.toInstant().atZone(ZoneId.of("Asia/Tokyo")).toLocalDate(). The Date becomes an Instant, the zone turns it into a ZonedDateTime, and toLocalDate keeps only the date.
Exercise
Why does sqlDate.toInstant() fail, and what should you call instead?
Show answer
A java.sql.Date holds only a calendar date with no time, so it cannot name a single instant and throws UnsupportedOperationException. Call sqlDate.toLocalDate() instead.