Quiz: ZonedDateTime in Java: ZoneId, Time Zones and Daylight Saving Time
Study the lesson first: ZonedDateTime in Java: ZoneId, Time Zones and Daylight Saving Time
Total: 6 questions
1. What is ZonedDateTime in Java, and how is ZoneId different from ZoneOffset?
What is ZonedDateTime in Java, and how is ZoneId different from ZoneOffset?
java.time.ZonedDateTime is an immutable, thread-safe class that stores a date and time together with a time zone: a LocalDateTime + a zone ID (ZoneId) + the offset from UTC in effect at that moment (ZoneOffset). One object pins down a single instant on the timeline and knows its region's clock-change rules: ZonedDateTime.of(2026, 7, 15, 10, 0, 0, 0, ZoneId.of("Europe/Berlin")) prints as 2026-07-15T10:00+02:00[Europe/Berlin].
ZoneId comes in two kinds, and both extend it:
- a region (
ZoneRegion, e.g.Europe/Moscow) with its history and clock-change rules, so its offset depends on the date: Berlin is +02:00 in summer and +01:00 in winter; - a fixed offset (
ZoneOffset, e.g.+03:00) with no rules; the offset never changes. The valid range is −18 to +18 hours.
ZoneId.of("UTC") is a region, not an offset; normalized() turns it into Z. Short names ("MSK", "PST"), typos and the wrong case give ZoneRulesException: Unknown time-zone ID. Short names are only read through the map in ZoneId.of("PST", ZoneId.SHORT_IDS), and "EST" there means America/Panama with no daylight saving time, so it's safer to write the full region ID. ZoneId.getAvailableZoneIds() returns all regions; their number depends on the tzdata version in the JDK.
2. How do you convert a time to another time zone in Java, and how is withZoneSameInstant() different from withZoneSameLocal()?
How do you convert a time to another time zone in Java, and how is withZoneSameInstant() different from withZoneSameLocal()?
The time in another zone comes from withZoneSameInstant(zone): the instant stays the same, the wall-clock reading and the offset change. withZoneSameLocal(zone) keeps the same date and time on the clock but in another zone, which is a different instant.
ZonedDateTime ny = ZonedDateTime.of(2026, 7, 15, 10, 0, 0, 0, ZoneId.of("America/New_York"));
ny.withZoneSameInstant(ZoneId.of("Europe/Berlin")); // 2026-07-15T16:00+02:00[Europe/Berlin]
ny.withZoneSameInstant(ZoneId.of("Asia/Kolkata")); // 2026-07-15T19:30+05:30[Asia/Kolkata]
ny.withZoneSameLocal(ZoneId.of("Europe/Berlin")); // 2026-07-15T10:00+02:00[Europe/Berlin] - 6 hours earlier
For "what time is it in Berlin when it's 10:00 in New York" you need withZoneSameInstant(). withZoneSameLocal() is for fixing a zone that was set by mistake or for "10:00 local time in every office".
Adding a fixed difference ("plus 6 hours") doesn't work: the US and Europe change their clocks on different dates. In 2026 the US switched to daylight saving time on 8 March and Europe on 29 March, so from 9 to 23 March a 10:00 New York call starts at 15:00 in Berlin, not 16:00. Code that stores regions and uses withZoneSameInstant() handles this on its own.
3. What happens to ZonedDateTime when the clocks change for daylight saving time (gaps and overlaps), and how do you control it explicitly?
What happens to ZonedDateTime when the clocks change for daylight saving time (gaps and overlaps), and how do you control it explicitly?
When daylight saving time starts, the clocks jump forward and part of the local time doesn't exist: that's a gap. When it ends, the clocks go back and part of the time happens twice: that's an overlap. ZonedDateTime throws no exceptions:
- gap: a non-existent time is shifted forward by the length of the gap. In Berlin on 29 March 2026,
LocalDateTime.of(2026, 3, 29, 2, 30).atZone(berlin)gives03:30+02:00;plusDays()andwithHour()behave the same way; - overlap: of the two options, the earlier, summer offset is taken. On 25 October 2026, 02:30 in Berlin gives
02:30+02:00, and the second 02:30 (+01:00) comes one hour later.
How to choose the behaviour explicitly:
ZonedDateTime.ofStrict(ldt, offset, zone)throwsDateTimeExceptioninstead of shifting silently, which is useful for user input (catch the exception);withLaterOffsetAtOverlap()andwithEarlierOffsetAtOverlap()pick the offset in an overlap;ofLocal(ldt, zone, preferredOffset)uses the preferred offset only if it is valid;zone.getRules().getValidOffsets(ldt)checks a time in advance: an empty list means a gap, two elements an overlap, one element a normal time.getTransition(ldt)returns the transition itself ornull.
If your input is instants (Instant), there is no ambiguity. One more trap: some zones change their clocks exactly at midnight, so that day has no 00:00 — LocalDate.of(2026, 3, 8).atStartOfDay(ZoneId.of("America/Havana")) gives 01:00-04:00. Get the start of a day with date.atStartOfDay(zone) and don't rely on getHour() == 0.
4. How is plusDays(1) different from plusHours(24) for a ZonedDateTime on a clock-change day?
How is plusDays(1) different from plusHours(24) for a ZonedDateTime on a clock-change day?
Calendar units (days, weeks, months) keep the wall-clock time, while time units (hours, minutes, Duration) keep the physical interval. On ordinary days there is no difference, but a clock-change day has 23 or 25 hours, and the results are an hour apart:
ZonedDateTime before = ZonedDateTime.of(2026, 3, 28, 12, 0, 0, 0, ZoneId.of("Europe/Berlin"));
before.plusDays(1); // 2026-03-29T12:00+02:00[Europe/Berlin]
before.plusHours(24); // 2026-03-29T13:00+02:00[Europe/Berlin]
Duration.between(before, before.plusDays(1)); // PT23H
ChronoUnit.DAYS.between(before, before.plusDays(1)); // 1
In autumn it's the other way round: from 24 October 2026 at 12:00 to the next day is PT25H, and plusHours(24) gives 2026-10-25T11:00+01:00.
For a "same time tomorrow" reminder use plusDays(1); for a "24 hours from now" timeout use plusHours(24). A daily 09:00 Berlin event "jumps" by an hour in UTC (08:00Z before 29 March, 07:00Z after), and that is correct. A scheduler that counts "every 24 hours from an instant in UTC" starts firing at 10:00 Berlin time after the switch. LocalDateTime doesn't see clock changes at all and counts 24 hours.
5. How should you store a date and time with a time zone: past vs future events, ZonedDateTime or OffsetDateTime in a database and JSON?
How should you store a date and time with a time zone: past vs future events, ZonedDateTime or OffsetDateTime in a database and JSON?
Time zone rules are set by governments, and they change (Russia dropped clock changes in 2011 and changed its offset in 2014). Java takes its rules from the IANA tz database bundled in the JDK (tzdb.dat). ZoneRulesProvider.getVersions("Europe/Berlin").lastKey() shows the version; you update the database by updating the JDK or with Oracle's Timezone Updater Tool (tzupdater). That leads to these storage rules:
- past events, logs, audit trails:
Instant(orOffsetDateTime); the moment has already happened, and future rule changes can't touch it; - future events in local time (a meeting on 15 May 2027 at 10:00 in Berlin):
LocalDateTime+ZoneIdas two fields, computing the instant when you need it. A precomputedInstantdrifts away from 10:00 if the rules change; - a
TIMESTAMP WITH TIME ZONEcolumn:OffsetDateTime; JDBC 4.2 supports it as the standard mapping (rs.getObject(col, OffsetDateTime.class)), while support forZonedDateTimeis not required by the spec; - JSON and REST APIs: an ISO string with an offset or an
Instant; if you need the region, send it as a separate field; ZonedDateTime: in-memory calculations, converting between zones and calendar arithmetic.
The difference between the classes shows up in arithmetic: OffsetDateTime keeps a fixed offset with no rules. ZonedDateTime.of(2026, 3, 28, 9, 0, 0, 0, berlin).plusDays(1) gives 09:00+02:00, while toOffsetDateTime().plusDays(1) gives 09:00+01:00, which is 10:00 on Berlin clocks.
6. How do you connect ZonedDateTime and ZoneId with the legacy TimeZone, GregorianCalendar and Date, and what is the danger of TimeZone.getTimeZone()?
How do you connect ZonedDateTime and ZoneId with the legacy TimeZone, GregorianCalendar and Date, and what is the danger of TimeZone.getTimeZone()?
In legacy code a time zone is a java.util.TimeZone, and a date with a zone is a GregorianCalendar. The bridges to java.time:
TimeZone.getTimeZone(zoneId)andtimeZone.toZoneId();GregorianCalendar.from(zdt)andcalendar.toZonedDateTime(); the zone is kept:2026-03-28T09:00+01:00[Europe/Berlin];Date.from(zdt.toInstant());Datehas no zone, only the instant survives.
The key difference is how they react to a wrong ID. TimeZone.getTimeZone("Europe/Moskow") with a typo throws nothing and silently returns GMT (getID() is GMT, getRawOffset() is 0), so the program runs with offset 0 instead of +3. ZoneId.of("Europe/Moskow") throws ZoneRulesException: Unknown time-zone ID right away, on the line with the mistake. That alone is a good reason to move to java.time.