Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Convert between legacy and java.time types: a java.util.Date to LocalDate in Europe/Berlin, a java.sql.Timestamp to Instant, and a LocalDate back to java.sql.Date.

A java.util.Date is an instant, so we convert it with date.toInstant().atZone(ZoneId.of("Europe/Berlin")).toLocalDate(), naming the zone explicitly. A java.sql.Timestamp converts with timestamp.toInstant(), which keeps its nanoseconds, and a LocalDate goes back to a java.sql.Date with java.sql.Date.valueOf(localDate). The trap is that java.sql.Date.toInstant() throws UnsupportedOperationException, so for it we use toLocalDate(); a Calendar converts with GregorianCalendar.toZonedDateTime().

Why this question is asked

Very few codebases are pure java.time. Old libraries, JDBC code and public APIs still hand us Date, Timestamp and Calendar objects, so converting at the boundary is a daily task. Interviewers ask for these conversions because each one hides a decision, usually about a time zone, and one of them throws an exception that surprises most people. In this answer, we will recap what each legacy type actually represents, then do the three conversions in the question one at a time, look at the java.sql.Date trap, cover Calendar, and finish with a reference table.

1A Quick Recap: What the Legacy Types Really Hold

java.util.Date has a misleading name. It does not hold a date; it holds an instant, a count of milliseconds since 1970-01-01T00:00Z, exactly like Instant but mutable and with only millisecond precision. Its toString() prints that instant in the JVM's default zone, which is why it looks like a local date and time.

java.sql.Timestamp extends Date and adds a nanoseconds field, so it can hold the full precision of a database timestamp. java.sql.Date also extends Date, but it is meant to represent a calendar date only, a SQL DATE with no time. Calendar, usually a GregorianCalendar, is an instant plus a TimeZone, which makes it the legacy cousin of ZonedDateTime.

Legacy types and their java.time equivalents
Legacy typeWhat it really holdsClosest java.time typeConvert with
java.util.Dateinstant, millisecond precisionInstantdate.toInstant(), Date.from(instant)
java.sql.Timestampinstant with nanosecondsInstant or LocalDateTimets.toInstant(), Timestamp.from(instant)
java.sql.Datecalendar date (SQL DATE)LocalDatetoLocalDate(), java.sql.Date.valueOf(d)
GregorianCalendarinstant + time zoneZonedDateTimetoZonedDateTime(), GregorianCalendar.from(z)

2Date to LocalDate in Europe/Berlin

Because a Date is an instant, it has no date until we pick a zone. The same instant can be 31 December in London and 1 January in Berlin. So the conversion goes through Instant and then names the zone.

Java
Date date = new Date(1735687800000L); // 2024-12-31T23:30:00Z
LocalDate berlin = date.toInstant()
.atZone(ZoneId.of("Europe/Berlin"))
.toLocalDate();
System.out.println(berlin); // 2025-01-01 (00:30 in Berlin)
LocalDate utc = date.toInstant().atZone(ZoneOffset.UTC).toLocalDate();
System.out.println(utc); // 2024-12-31
LocalDate shorter = LocalDate.ofInstant(date.toInstant(), ZoneId.of("Europe/Berlin")); // Java 9+, same result

Notice that the two results differ by a day. That is not a bug in the conversion; it is the reason the conversion needs a zone. Code that uses ZoneId.systemDefault() here gets whichever answer the server's configuration gives, so we always name the zone the business means.

3Timestamp to Instant

A Timestamp is an instant with nanosecond precision, and toInstant() keeps all of it. That matters because a database column may hold microseconds, and Date's millisecond view would drop them.

Java
Timestamp ts = Timestamp.from(Instant.parse("2025-01-01T10:15:30.123456789Z"));
Instant i = ts.toInstant();
System.out.println(i); // 2025-01-01T10:15:30.123456789Z
System.out.println(ts.getNanos()); // 123456789: the full fraction is kept

There is a catch if the Timestamp came from Timestamp.valueOf("2025-01-01 10:15:30") or from rs.getTimestamp() on a column without time zone. Those interpret the local date-time in the JVM default zone, so the resulting instant depends on the server. For such columns, toLocalDateTime() is often the more honest conversion, or better, reading the column directly with rs.getObject("col", LocalDateTime.class).

4LocalDate Back to java.sql.Date, and the toInstant() Trap

Going back, java.sql.Date.valueOf(localDate) builds a SQL date for the same calendar day, and toLocalDate() turns it back. Both directions keep the day exactly.

Java
LocalDate d = LocalDate.of(2025, 3, 10);
java.sql.Date sqlDate = java.sql.Date.valueOf(d);
System.out.println(sqlDate); // 2025-03-10
System.out.println(sqlDate.toLocalDate()); // 2025-03-10
sqlDate.toInstant();
// java.lang.UnsupportedOperationException
Step throughConverting a legacy value someone hands us
  1. Is it a java.sql.Date?
    YestoLocalDate(), never toInstant()NoContinue
  2. Is it a Calendar?
    YesGregorianCalendar.toZonedDateTime()NoContinue
  3. Date or Timestamp: toInstant()keeps nanos for Timestamp
  4. Need a local date or time?
    YesatZone(explicit ZoneId)NoKeep the Instant

4 steps in order. Play them, or step through them one at a time.

The exception surprises people because toInstant() works on its parent class, Date. java.sql.Date overrides it to throw on purpose: a SQL DATE has no time and no zone, so it is not an instant, and the JDK refuses to invent one. java.sql.Time does the same. So when generic code receives a Date that might be a java.sql.Date, for example from an old DAO, it must check the type and call toLocalDate(), or convert through new Date(sqlDate.getTime()) if it truly wants the underlying milliseconds.

5Calendar to ZonedDateTime

A GregorianCalendar carries an instant and a TimeZone, so it converts directly into a ZonedDateTime with the same zone. For a Calendar that is not a GregorianCalendar, such as a Buddhist or Japanese calendar, we go through the instant and the zone ourselves.

Java
GregorianCalendar cal = GregorianCalendar.from(
ZonedDateTime.of(2025, 3, 10, 9, 0, 0, 0, ZoneId.of("Europe/Berlin")));
ZonedDateTime z = cal.toZonedDateTime();
System.out.println(z); // 2025-03-10T09:00+01:00[Europe/Berlin]
Calendar any = Calendar.getInstance();
ZonedDateTime z2 = any.toInstant().atZone(any.getTimeZone().toZoneId()); // works for every Calendar

In new code we rarely create Calendars, but these conversions let us remove Calendar from one layer at a time while the layers around it still use it.

6Follow-up questions

How do we avoid the legacy types in JDBC code altogether?

JDBC 4.2 drivers support java.time directly: rs.getObject("col", LocalDate.class) for a DATE, LocalDateTime for a timestamp without time zone, and OffsetDateTime for a timestamp with time zone, plus ps.setObject(index, value) in the other direction. That skips Date, Timestamp and the default-zone conversions they bring.

Why is Date.from(instant) sometimes lossy?

Because Date has only millisecond precision. An Instant with microseconds or nanoseconds is truncated to milliseconds. Timestamp.from(instant) keeps the nanoseconds.

What does new Date().toString() tell us?

It prints the instant in the JVM's default zone, such as Mon Mar 10 09:00:00 CET 2025. It looks like a local date-time, which is the source of the common misunderstanding that a Date holds a zone. It does not; only the printing uses one.

7Conclusion

In this answer, we saw that java.util.Date and java.sql.Timestamp are instants, so they convert with toInstant(), with Timestamp keeping its nanoseconds, and a local date needs an explicit zone: date.toInstant().atZone(ZoneId.of("Europe/Berlin")).toLocalDate(). A LocalDate becomes a SQL date with java.sql.Date.valueOf and comes back with toLocalDate(), while java.sql.Date.toInstant() throws UnsupportedOperationException because a SQL date is not an instant. GregorianCalendar converts with toZonedDateTime(), and JDBC 4.2 lets us skip most of these types entirely.

What to say in the interviewAbout 70 seconds aloud

Open with

The key is knowing what each legacy type really holds. A java.util.Date is an instant, so to get a LocalDate in Berlin I write date.toInstant().atZone(ZoneId.of("Europe/Berlin")).toLocalDate(), or LocalDate.ofInstant on Java 9 and later.

Then, if they want more

The zone matters: 23:30 UTC on December 31st is already January 1st in Berlin, so I never rely on the system default here. A java.sql.Timestamp is also an instant but with nanosecond precision, and timestamp.toInstant() keeps the nanos.

I'd just watch out for Timestamps created from local values, like valueOf or getTimestamp on a column without time zone, because those used the JVM default zone. To go from LocalDate back to SQL, java.sql.Date.valueOf(localDate), and toLocalDate() reverses it.

The trap is that java.sql.Date.toInstant() throws UnsupportedOperationException, because a SQL date has no time or zone, so it's not an instant; I use toLocalDate() instead. For Calendar, GregorianCalendar.toZonedDateTime() converts directly, or toInstant plus the calendar's zone for other calendars.

And in JDBC code, I'd prefer getObject and setObject with java.time types, which JDBC 4.2 drivers support, so most of these conversions disappear.

About this answer

Last updated
12 November 2026
Versions it names
Java 9
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Convert between legacy and java.time types: a java.util.Date to LocalDate in...": https://scrivio-interview-library.netlify.app/java/date-and-time/547ba96a01/