Lesson · Date & Time API
Legacy API and interop: Date, Calendar, SimpleDateFormat
12 min readLegacy API & Interop
The old date classes still sit at the edges of the platform: JDBC, some frameworks, old signatures. Read their three design traps, learn the conversion table both ways, and keep java.time as the working language.
Date and Calendar: the traps in reading order
java.util.Date is a millisecond timestamp; getTime is honest, but toString renders it in the machine's default zone, which makes logs differ between servers. Calendar wraps a Date with a calendar, and its field model is where the confusion lives: months are zero-based, days are not, and by default it is lenient.
Lenient means impossible input is plausible output: setting February 31 rolls into March. That behavior is occasionally useful for incremental arithmetic and regularly fatal for parsing user input, where a rejection is the only correct answer.
Calendar jan31 = new GregorianCalendar(2026, Calendar.JANUARY, 31); jan31.set(Calendar.MONTH, Calendar.FEBRUARY); jan31.set(Calendar.DAY_OF_MONTH, 31); // lenient: Feb 31 becomes March 3, silently
SimpleDateFormat: the thread-safety wound
SimpleDateFormat both formats and parses, and it keeps mutable state while doing it. Sharing one instance across threads corrupts results or throws; the class is not thread-safe and never was. The historical workarounds (a new instance per call, synchronize, or ThreadLocal) all trade correctness for cost.
DateTimeFormatter has none of this: immutable, thread-safe, strict by default. Migrating a format string is usually mechanical except for the pattern-letter traps (mm versus MM, YYYY versus yyyy) and locale defaults.
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy", Locale.ROOT); // never share across threads DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd.MM.yyyy", Locale.ROOT); // share freely
In a review, a static SimpleDateFormat field is a defect. If you see one, the fix is a DateTimeFormatter constant, not a synchronized block.
The conversion table
Every legacy value converts into java.time and back through a small, total set of methods. Learn them as a table and interop stops being scary: the new types are the working language, the old ones appear only at boundaries.
| Legacy | To java.time | Back |
|---|---|---|
| Date | date.toInstant() | Date.from(instant) |
| Calendar | cal.toInstant() | Date.from then Calendar.getInstance().setTime (or GregorianCalendar.from) |
| GregorianCalendar | gcal.toZonedDateTime() | GregorianCalendar.from(zoned) |
| SimpleDateFormat | no state to convert: rewrite as DateTimeFormatter | format(pattern) both ways |
| TimeZone | TimeZone.toZoneId() | TimeZone.getTimeZone(zoneId) |
Legacy bridge · in at the door, out at the door
pick a legacy typethe trap
months are zero-based and lenient: Feb 31 becomes March 3
modern in
cal.toInstant()
modern out
GregorianCalendar.from(zoned) (or setTime on a fresh instance)
Calendar itself is a millisecond count plus fields; the instant preserves the moment.
A migration that stays finished
The pragmatic path is one-directional: convert on entry, convert on exit, never let the legacy types travel inward. A single adapter class per boundary keeps the conversions reviewable and gives future readers one place to look.
- Boundary inventory: JDBC (java.sql.Date/Timestamp), frameworks with old signatures, serialized state, third-party SDKs.
- Adapters in one place: toInstant / toZonedDateTime on the way in, Date.from on the way out.
- Pin the zone explicitly at every conversion: legacy types smuggled the machine zone in, do not let the conversion eat the same bug.
- java.sql.Date.toLocalDate and Timestamp.toInstant convert JDBC rows without going through millis arithmetic.
- Delete one-off utilities (year/month math on Calendar) as you convert; they are the source of most lingering zero-based-month bugs.
Old types at the doorstep only
Legacy date classes are boundary formats, not working models. Convert them at the door, name the zone in the conversion, and the interior of the codebase stays on types that cannot leak a default zone or a zero-based month.
Interview angles
What are the three classic java.util date traps?+
Date's zone-dependent toString (machine default leaks into logs), Calendar's zero-based months with lenient rollover, and SimpleDateFormat's mutable state making it unsafe to share. Each disappears in java.time's design.
How does Calendar's leniency behave?+
Lenient mode (the default) normalizes impossible values: February 31 becomes March 3. It never throws. Set lenient(false) or parse strictly with DateTimeFormatter when input must be validated.
Date to Instant and back: the exact calls?+
date.toInstant() converts in; Date.from(instant) converts out. The pair is lossless: both sides are the same millisecond count.
How do GregorianCalendar and ZonedDateTime interconvert?+
gcal.toZonedDateTime() and GregorianCalendar.from(zoned). Unlike plain Calendar, GregorianCalendar carries the zone information the conversion needs, which is why the pair exists.
Why is a static SimpleDateFormat a defect?+
It is shared mutable state: concurrent format or parse calls corrupt results or raise exceptions. The replacement is a DateTimeFormatter constant, which is immutable and safe to share.