skip to content

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 pageshow

questions

page 2 of 2

How do Duration and Period behave differently across a Daylight Saving Time transition, and why does it matter?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Adding 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.

open as a page

What is DateTimeParseException, and how do lenient, smart, and strict ResolverStyles change parsing behavior?

level: seniorimportance: should knowfreq 42%

basics

~10 s

Parsing 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).

open as a page

How does Date's mutability complicate API design, and how does java.time eliminate the problem?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Because 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.

open as a page

What happens when you add a year to February 29 (a leap day), and how does java.time handle leap years generally?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Feb 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.

open as a page

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?

level: seniorimportance: should knowfreq 30%

basics

~20 s

Start 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.

open as a page

What goes wrong when converting wall-clock times across DST gaps and overlaps, and how does atZone resolve them?

level: seniorimportance: should knowfreq 40%

basics

~20 s

When 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().

open as a page

Why are java.time types safe to share across threads and cache as constants, unlike java.util.Date and SimpleDateFormat?

level: seniorimportance: should knowfreq 55%

basics

~10 s

java.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.

open as a page

When should you use ZonedDateTime versus OffsetDateTime, and how do they differ?

level: seniorimportance: should knowfreq 55%

basics

~20 s

OffsetDateTime 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.

open as a page

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?

level: principalimportance: should knowfreq 40%

basics

~20 s

Store 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.

open as a page

What are the risks of using LocalDateTime for persistence and APIs, and what would you use instead?

level: principalimportance: should knowfreq 50%

basics

~20 s

LocalDateTime 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.

open as a page

What is the tz database (ZoneRules), and why do tz-data updates matter for stored future date-times?

level: principalimportance: should knowfreq 35%

basics

~20 s

The 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.

open as a page

How do you write a custom TemporalAdjuster, for example a next-business-day rule?

level: seniorimportance: nice to knowfreq 28%

basics

~20 s

TemporalAdjuster 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.

open as a page

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?

level: principalimportance: nice to knowfreq 25%

basics

~20 s

Each 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.

open as a page

showing 31–43 of 43