Date & Time API
The java.time API and what it replaced: the core temporal types, durations and periods, zones and daylight saving, formatting and parsing, and immutability. Date handling questions are common because so much production code gets time zones wrong.
part ofJavaoverview, primer and where to startread it →on this pageshowhide
explore
- Legacy Date & Calendar Pitfalls5 questions
- LocalDate / LocalTime / LocalDateTime5 questions
- Daylight Saving Time Handling4 questions
- Duration vs Period & ChronoUnit5 questions
- DateTimeFormatter: Parsing & Formatting5 questions
- TemporalAdjusters & truncatedTo5 questions
- java.time Conversions & Interop5 questions
- Immutability & Fluent API of java.time4 questions
questions
page 2 of 2How do Duration and Period behave differently across a Daylight Saving Time transition, and why does it matter?
basics
~20 sAdding a Period of 1 day to a ZonedDateTime moves to the same wall-clock time next day, even if that day was 23 or 25 hours long. Adding a Duration of 24 hours adds exactly 24 hours of real time, so the wall-clock time can shift across a DST change.
What is DateTimeParseException, and how do lenient, smart, and strict ResolverStyles change parsing behavior?
basics
~10 sParsing bad text throws DateTimeParseException. The ResolverStyle decides how strict validation is: STRICT rejects out-of-range values, SMART (the default) corrects mild ones, LENIENT rolls overflow over (e.g. day 32 -> next month).
How does Date's mutability complicate API design, and how does java.time eliminate the problem?
basics
~20 sBecause Date can be changed after creation, getters and constructors must return defensive copies, or callers can secretly mutate your object's state. java.time types are immutable, so no copying is needed and they are safe to share.
What happens when you add a year to February 29 (a leap day), and how does java.time handle leap years generally?
basics
~20 sFeb 29 only exists in leap years. If you add one year to Feb 29 and the target year isn't a leap year, java.time adjusts the result to Feb 28 instead of failing. It never produces an invalid date like Feb 30.
Using TemporalAdjusters and truncatedTo, how would you compute a deterministic billing window: the start-of-day on the first of next month, and the inclusive last instant of the current month?
basics
~20 sStart of next month: date.with(firstDayOfNextMonth()).atStartOfDay(). The clean way to bound the current month is a half-open range [firstDayOfMonth at 00:00, firstDayOfNextMonth at 00:00), using less-than for the upper bound instead of trying to find the exact last instant.
What goes wrong when converting wall-clock times across DST gaps and overlaps, and how does atZone resolve them?
basics
~20 sWhen clocks change, some local times don't exist (spring-forward gap) and some happen twice (fall-back overlap). atZone resolves a gap by shifting the time forward, and an overlap by picking the earlier offset by default; you can override with withLaterOffsetAtOverlap().
Why are java.time types safe to share across threads and cache as constants, unlike java.util.Date and SimpleDateFormat?
basics
~10 sjava.time types never change after creation (immutable), so multiple threads reading the same instance can't interfere. The old Date is mutable and SimpleDateFormat keeps internal state, so sharing them across threads causes corruption.
When should you use ZonedDateTime versus OffsetDateTime, and how do they differ?
basics
~20 sOffsetDateTime is a local date-time plus a fixed UTC offset (like +02:00). ZonedDateTime is a local date-time plus a full region (like Europe/Paris) that knows daylight saving rules, so it can adjust offsets correctly over time.
For a global app that schedules events and stores timestamps, how should you model time to stay correct across DST, and what are the trade-offs of storing Instant/UTC versus zoned/local time?
basics
~20 sStore an exact instant (UTC) for things that already happened, so they are unambiguous. For future events tied to a place's wall clock, store the local time plus the ZoneId (not a fixed offset), so the right offset is applied when DST rules change. Never store bare local time alone.
What are the risks of using LocalDateTime for persistence and APIs, and what would you use instead?
basics
~20 sLocalDateTime has no time zone, so a stored value like 2026-02-14T09:30 is ambiguous — you can't tell which real moment it was. For timestamps that cross zones, store an Instant or OffsetDateTime instead, and keep LocalDateTime only for true wall-clock data.
What is the tz database (ZoneRules), and why do tz-data updates matter for stored future date-times?
basics
~20 sThe tz database is the global dataset of time-zone rules (offsets and daylight-saving changes) that Java uses behind ZoneId. Governments change those rules, so updates matter: a future local time stored as a region can resolve to a different instant if the rules change.
How do you write a custom TemporalAdjuster, for example a next-business-day rule?
basics
~20 sTemporalAdjuster has one method, so you can write it as a lambda. The easiest way is TemporalAdjusters.ofDateAdjuster, which takes a function from LocalDate to LocalDate; inside it you check the weekday and add the right number of days to skip the weekend.
Given java.time creates a new object on every plus/minus/with call, when (if ever) is this allocation a real concern, and how would you reason about it?
basics
~20 sEach call makes a small new object, but these are tiny and short-lived, so the JVM handles them cheaply. It only matters in very hot, high-volume loops; usually you measure first and prefer correctness and clarity over avoiding allocation.
showing 31–43 of 43