Dates instants and zones
What you will learn
Choose the time type that expresses what you know. LocalDate is a calendar date, LocalTime is a clock reading, LocalDateTime combines those without a zone, and Instant identifies a point on the time line. A ZonedDateTime resolves local date and time using a region or offset.
- Choose a date, local time, instant or zoned date-time for a domain value.
- Resolve daylight-saving gaps and overlaps explicitly.
- Inject a fixed Clock and convert legacy Date values to modern time types.
Each example is a complete Main.java file. Use a fresh directory, javac --release 21 Main.java, then java Main. No external dependencies.
One instant, two calendar dates
import java.math.*;import java.time.*;import java.time.format.*;import java.time.temporal.*;import java.text.*;import java.util.*;public class Main { public static void main(String[] args) throws Exception { Instant event = Instant.parse("2024-01-01T00:30:00Z"); var stockholm = event.atZone(ZoneId.of("Europe/Stockholm")); var newYork = stockholm.withZoneSameInstant(ZoneId.of("America/New_York")); System.out.println(stockholm.toLocalDate()); System.out.println(newYork.toLocalDate()); System.out.println(stockholm.toLocalTime()); System.out.println(stockholm.toInstant().equals(newYork.toInstant())); }}Expected output:
2024-01-012023-12-3101:30trueThe event stays on one instant although the calendar dates differ.
Local fields versus timeline
A birthday normally needs LocalDate, not an instant; a daily opening hour can use LocalTime. An audit event needs Instant, which can be rendered in any zone. LocalDateTime has no offset or region, so it cannot identify one instant until a zone or offset is supplied. ZoneId.of("Europe/Stockholm") uses region rules, unlike a fixed offset such as +01:00. withZoneSameInstant changes displayed fields while preserving the instant. withZoneSameLocal tries to keep wall-clock fields and may change the instant.
Daylight-saving resolution
A spring-forward gap has no valid offset for some local times. A fall-back overlap has two valid offsets. atZone shifts a gap forward by its length and normally selects the earlier offset in an overlap. Inspect zone.getRules().getValidOffsets(local) before accepting a local booking: zero means gap, one ordinary time, two overlap. Ask for the intended offset or reject ambiguous data; use ZonedDateTime.ofStrict to validate a chosen offset. Do not assume every transition is one hour. Future rules can change; record the region and the chosen instant or offset according to the domain.
Predictable time and legacy boundaries
Inject a Clock into code that needs now. Clock.fixed returns the same instant each time and has an explicit zone used by LocalDate.now(clock). Production can supply Clock.systemUTC or another explicitly chosen zone. Convert legacy Date using toInstant and Date.from; Date represents milliseconds, so submillisecond precision is lost on conversion back. Calendar.toInstant bridges legacy calendars; avoid sharing mutable legacy Date or Calendar objects and remember that Calendar may have its own zone. The java.time classes used here are immutable.
Detect a spring-forward gap
import java.math.*;import java.time.*;import java.time.format.*;import java.time.temporal.*;import java.text.*;import java.util.*;public class Main { public static void main(String[] args) throws Exception { ZoneId zone = ZoneId.of("Europe/Stockholm"); LocalDateTime local = LocalDateTime.parse("2024-03-31T02:30"); System.out.println(zone.getRules().getValidOffsets(local).size()); System.out.println(local.atZone(zone).toLocalTime()); try { ZonedDateTime.ofStrict(local, ZoneOffset.ofHours(1), zone); } catch (DateTimeException ex) { System.out.println("invalid local time"); } }}Expected output:
003:30invalid local timeThis historical gap has no valid 02:30 in that region. atZone adjusts it; strict resolution rejects it.
Choose an offset in an overlap
import java.math.*;import java.time.*;import java.time.format.*;import java.time.temporal.*;import java.text.*;import java.util.*;public class Main { public static void main(String[] args) throws Exception { ZoneId zone = ZoneId.of("Europe/Stockholm"); LocalDateTime local = LocalDateTime.parse("2024-10-27T02:30"); var offsets = zone.getRules().getValidOffsets(local); System.out.println(offsets.size()); var earlier = ZonedDateTime.ofStrict(local, offsets.get(0), zone); var later = earlier.withLaterOffsetAtOverlap(); System.out.println(earlier.toInstant()); System.out.println(later.toInstant()); }}Expected output:
22024-10-27T00:30:00Z2024-10-27T01:30:00ZThe identical wall-clock reading describes two distinct instants. Store the selected offset or instant.
Fixed clock and old Date conversion
import java.math.*;import java.time.*;import java.time.format.*;import java.time.temporal.*;import java.text.*;import java.util.*;public class Main { public static void main(String[] args) throws Exception { Clock clock = Clock.fixed(Instant.parse("2024-01-01T23:30:00Z"), ZoneId.of("Europe/Stockholm")); System.out.println(LocalDate.now(clock)); System.out.println(Instant.now(clock)); Date legacy = Date.from(Instant.now(clock)); System.out.println(legacy.toInstant().equals(Instant.now(clock))); Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ROOT); cal.setTime(legacy); System.out.println(cal.toInstant()); }}Expected output:
2024-01-022024-01-01T23:30:00Ztrue2024-01-01T23:30:00ZThe fixed clock makes date-sensitive tests repeatable. This input is exactly representable in milliseconds.
Common mistakes
Do not store an audit event as LocalDateTime alone or infer the region from a numeric offset. Avoid using the machine default zone in tests. Do not silently accept adjusted booking times in a gap. Do not print Date.toString for portable expected output: its rendering uses the default zone.
Practice
Use a fixed clock at 2024-06-01T23:30:00Z in Europe/Stockholm. Print the local date, local time and instant, then print the UTC date using the same instant.
Expected output:
2024-06-0201:302024-06-01T23:30:00Z2024-06-01Compare your answer with the manual practice criteria.
Official reading: ZonedDateTime · ZoneRules. Java 25 execution remains unverified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Dates instants and zones — knowledge check
Original arithmetic, time and localization questions. Java 21-compatible scope.
Which type identifies a point on the time line?
How many valid offsets exist in a spring gap?
What does withZoneSameInstant preserve?
Which clock makes tests independent of real time?
What may Date.from lose?