Lesson · Date & Time API
Core types (java.time): the right shape for every moment
14 min readCore Types (java.time)
java.time splits the idea of time into types that each answer one question exactly: a date without a clock, a time without a calendar, an instant on the machine timeline, and a zone that glues them together. Picking the right type removes whole categories of bugs.
One question per type
The old API had one Date for everything, so every caller guessed which parts were meaningful. java.time does the opposite: each type states exactly what it knows. A LocalDate has no time and no zone, an Instant has no calendar at all, and a ZonedDateTime has all three. Reading a signature tells you what the code can and cannot do.
| Type | Answers | Has time zone? |
|---|---|---|
| LocalDate | which day | no |
| LocalTime | which time of day | no |
| LocalDateTime | which day and time, no zone | no |
| Instant | a machine timestamp on the global timeline | no, it is always UTC |
| Duration | how long, in machine units | no |
| Period | how long, in calendar units | no |
| ZonedDateTime | all three, with DST rules | yes |
Type picker · one question per type
pick the question, meet the typeknows
a count from 1970-01-01T00:00:00Z
refuses to guess
no calendar: no month, no weekday, no zone
typical use
logs, persistence, comparison, TTLs
Human time: LocalDate, LocalTime, LocalDateTime
These three carry a wall-clock reading with no zone. That is correct more often than newcomers expect: a birthday, a business opening time and an alarm set for 07:00 all mean what the local clock says, wherever the user is. Of, parse and now give the three ways in; getters, with and plus do the rest.
LocalDateTime is the awkward middle child: it knows day and time but no zone, so it cannot identify a moment on the timeline. It exists for two jobs: as the hub for conversions (atZone attaches a zone, toInstant needs one) and for genuinely zone-free concepts like a recurring appointment slot.
LocalDate birthday = LocalDate.of(2026, 9, 21); LocalTime opening = LocalTime.of(9, 30); LocalDateTime slot = LocalDateTime.of(birthday, opening); birthday.getDayOfWeek(); // MONDAY LocalDate.parse("2026-09-21"); // ISO format is the default everywhere
Machine time: Instant
Instant is a count of time since 1970-01-01T00:00:00Z with nanosecond precision. No month, no day of week, no zone: those are calendar ideas, and the machine timeline has none. Log files, database rows in UTC, token expiry and cache TTLs all belong in Instant.
The rule that prevents a whole family of bugs: machines store and compare Instants; humans see local dates. Convert at the edges, never in the middle.
Instant now = Instant.now(); Instant expiry = now.plusSeconds(3600); expiry.isBefore(now.plusSeconds(1800)); // false: comparison is on the timeline
Amounts of time: Duration versus Period
The two amount types encode the calendar-versus-machine split one level deeper. Duration counts exact nanoseconds (hours, minutes, seconds); Period counts calendar units (years, months, days), which have no fixed length: a month is 28 to 31 days and a day is 23, 24 or 25 hours when DST changes.
That is why Duration.between takes instants or times and Period.between takes dates. Adding a Duration crosses a DST boundary by the real amount of time; adding a Period keeps the wall clock reading.
Duration task = Duration.ofMinutes(90); // PT1H30M Period between = Period.between(LocalDate.of(2026, 1, 1), LocalDate.of(2026, 3, 15)); // P2M14D LocalDate.of(2026, 1, 31).plus(Period.ofMonths(1)); // 2026-02-28
Full picture: ZonedDateTime
ZonedDateTime pairs a LocalDateTime with a ZoneId, so it knows both the reading and the rules that turn it into an instant. It is the type for anything a person schedules across regions and for the rare display that must show a zone.
Two conversion methods carry the entire mental model: withZoneSameInstant keeps the moment and changes the calendar, withZoneSameLocal keeps the reading and changes the moment. The second one is almost always a bug outside scheduling UIs.
ZonedDateTime meeting = ZonedDateTime.of(2026, 9, 21, 14, 0, 0, 0, ZoneId.of("Europe/Berlin")); meeting.withZoneSameInstant(ZoneId.of("America/New_York")); // 08:00, same moment meeting.withZoneSameLocal(ZoneId.of("America/New_York")); // 14:00, different moment
Immutable by design
Every java.time value is immutable: plusDays returns a new object and leaves the original alone. That one decision makes the types thread-safe by construction, removes aliasing surprises, and lets dates be used as map keys or shared constants without ceremony.
The cost is allocation on every operation, which is irrelevant next to the bugs it prevents. It also explains the API shape: all the changing verbs (plus, minus, with) return values, and there is not a single setter.
LocalDate day = LocalDate.of(2026, 9, 21); LocalDate next = day.plusDays(10); day.equals(next); // false: day never changed
Pick by question, not by habit
Storing when something happened: Instant. A day on a calendar: LocalDate. A scheduled wall-clock reading: LocalDateTime or ZonedDateTime. The type you choose is the list of mistakes the compiler can still catch.
Interview angles
Why does java.time separate LocalDate, Instant and ZonedDateTime?+
Each represents a different concept: a calendar day, a point on the machine timeline, and a wall-clock reading tied to zone rules. Merging them (as java.util.Date did) forces every caller to guess which parts are meaningful and hides zone conversions in the wrong layer.
When should Instant be used instead of LocalDateTime?+
Whenever the value identifies a moment rather than a readable time: log timestamps, persistence, comparison, TTLs. LocalDateTime cannot identify an instant without a zone, so it breaks across time zones and DST.
Duration versus Period?+
Duration is exact time in nanoseconds (machine units, safe across DST). Period is calendar time in years, months, days, whose lengths vary. Use Period for human calendars, Duration for elapsed time and automation.
Are java.time objects thread-safe?+
Yes, all their value types are immutable, so they can be shared freely. This is a deliberate contrast with java.util.Date, Calendar and SimpleDateFormat, which are mutable and unsafe to share.
What is wrong with using LocalDateTime for a global event time?+
It has no zone, so it cannot be converted to an instant without introducing an assumption. Two servers in different zones interpret the same LocalDateTime as different moments. Use Instant (or ZonedDateTime) and convert to local only for display.