Lesson · Date & Time API
Design principles: why java.time replaced the old API
12 min readDesign Principles
Date, Calendar and SimpleDateFormat were designed in the 1990s and carry zero-based months, hidden machine zones and mutable shared state. java.time starts from the design principles those flaws teach: separate concerns, be explicit, and make every value immutable.
What the old design got wrong
java.util.Date was born in 1995 as a millisecond count with a calendar bolted on: its toString renders in the machine's default zone, so the same object prints differently on different servers. Calendar arrived to fix the calendar part and brought its own traps: months are zero-based while days are not, and by default it is lenient, so February 31 quietly becomes March 3.
SimpleDateFormat is the third trap: mutable state inside the formatter, so two threads sharing one instance produce wrong output or exceptions instead of an error message about sharing.
| Legacy | Design flaw | java.time answer |
|---|---|---|
| Date | an instant pretending to be a calendar; zone-dependent toString | Instant for machine time, LocalDate for calendars |
| Calendar | zero-based months, lenient by default, mutable | 1-based months, strict parsing, immutable values |
| SimpleDateFormat | not thread-safe; shared state mutates during format | DateTimeFormatter: immutable and thread-safe |
| both | setters mutate in place; aliasing leaks everywhere | no setters: plus/with return new instances |
The principles behind the replacement
java.time (JSR-310) was built around a small set of principles, each one a direct answer to an old defect. Read them as a checklist for your own value types:
- Separate the concepts. Calendar day, wall clock, machine timeline and zone are different things in different types, never merged.
- Immutability first. Values never change; operations return new values. Sharing becomes safe, and thread safety is a property, not a promise.
- Explicit over implicit. No hidden machine zone, no defaults that depend on the server. A conversion that needs a zone demands one.
- Factory methods, fluent verbs. of, now, parse, plus, with read like sentences; constructors are private.
- ISO 8601 as the default. Parse and print round-trip machine-readable formats unless told otherwise.
- Interfaces for frameworks. Temporal and friends let custom calendars plug in without changing call sites.
The same jobs, both APIs
Pick any everyday task and the contrast is immediate: the new API says what it means, the old one requires a comment explaining which fields are meaningful and which zone the machine happens to be in.
// September 21, 2026. Old: month is zero-based, so 8 means September Calendar cal = new GregorianCalendar(2026, Calendar.SEPTEMBER, 21); // New: 1-based months, no surprises when read aloud LocalDate date = LocalDate.of(2026, 9, 21); // adding a month: old mutates in place, new returns a value cal.add(Calendar.MONTH, 1); // cal changed silently LocalDate next = date.plusMonths(1); // date untouched
Old vs new · the flaw and the principle
same job, two eras1997 · java.util
new GregorianCalendar(2026, 8, 21)
8 means September: months are zero-based, so the literal lies to every reader.
2014 · java.time
LocalDate.of(2026, 9, 21)
1-based months: the call reads exactly like the date it builds.
Living with both
java.time did not remove the old types; they remain in java.util and java.text and appear in every API that predates 2014. The migration rule is a boundary rule: convert legacy values to java.time on the way in, do all the work in the new types, convert back only when an old signature demands it.
Conversion is mechanical: Date.toInstant and Date.from, Calendar.toInstant together with GregorianCalendar.toZonedDateTime, and the reverse via Date.from(instant) or GregorianCalendar.from(zoned). The next lessons in this chapter cover each path, and the legacy lesson collects the full table.
Instant instant = legacyDate.toInstant(); // legacy in Date back = Date.from(instant); // legacy out ZonedDateTime zdt = calendar.toZonedDateTime(); // GregorianCalendar -> java.time
New code should not have a single Date, Calendar or SimpleDateFormat in it. If a framework forces one at its edge, wrap the conversion in one place and keep the rest of the codebase on java.time.
The flaws are the curriculum
Zero-based months, hidden machine zones, mutable formatters: each java.time principle exists because the old API taught that lesson the hard way. Recognizing the flaw explains the design, and the design explains the API.
Interview angles
Why was java.time introduced at all?+
The old date API was mutable, zone-implicit, confusingly indexed (zero-based months) and unsafe to share (Date, Calendar, SimpleDateFormat). java.time replaces it with immutable, thread-safe, explicitly zoned types, each representing one concept.
What is the problem with Calendar's zero-based months?+
Months are indexed 0..11 while day-of-month is 1-based, so writing 8 for September silently works in code and reads wrong in review. java.time uses 1-based months, and Calendar.SEPTEMBER constants exist only to paper over the old design.
Why is SimpleDateFormat dangerous?+
It keeps mutable internal state during formatting/parsing, so concurrent use corrupts results or throws. DateTimeFormatter is immutable and thread-safe, so a single instance can be shared as a constant.
How should legacy and modern date code coexist?+
Convert at the boundary: Date.toInstant or Calendar.toZonedDateTime on the way in, Date.from or GregorianCalendar.from on the way out, and java.time everywhere inside. The conversions are lossless for instants and explicit about zones.
Which principle of java.time prevents the most bugs in practice?+
Immutability. It removes aliasing, makes types thread-safe, and turns every operation into a visible assignment. The explicit-zone principle (no hidden default) is a close second, especially for persistence and logs.