Sub topic 16
Daylight Saving Time: Gaps, Overlaps, and Day Lengths
Daylight saving time (DST) is a rule in some places that moves clocks forward one hour in spring and back one hour in autumn. These jumps cause strange local times. ZonedDateTime knows the rules for each zone, so it handles the jumps for you. You still need to know what it does.
The Spring Gap
A gap is a stretch of local time that never happens. In New York on 10 March 2024, clocks jump from 02:00 straight to 03:00. So 02:30 does not exist that day.
If you ask for 02:30, Java does not throw an error. It moves the time forward by the length of the gap, so you get 03:30.
The Autumn Overlap
An overlap is a stretch of local time that happens twice. In New York on 3 November 2024, clocks go back from 02:00 to 01:00. So 01:30 happens once at offset -04:00 and again an hour later at offset -05:00.
By default, Java picks the earlier of the two. You can ask for the later one with withLaterOffsetAtOverlap().
A Day Is Not Always 24 Hours
On the spring change day, New York's day lasts 23 hours. On the autumn change day, it lasts 25 hours. This affects how you add time:
plusDays(1)adds one calendar day. It keeps the same clock time.plusHours(24)adds exactly 24 hours of real time. The clock time can shift.
Worked Example
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class DaylightSaving {
public static void main(String[] args) {
ZoneId ny = ZoneId.of("America/New_York");
// Gap: 02:30 does not exist, so it moves forward
ZonedDateTime inGap = ZonedDateTime.of(LocalDateTime.of(2024, 3, 10, 2, 30), ny);
System.out.println(inGap); // 2024-03-10T03:30-04:00[America/New_York]
// Overlap: 01:30 happens twice
ZonedDateTime early = ZonedDateTime.of(LocalDateTime.of(2024, 11, 3, 1, 30), ny);
ZonedDateTime late = early.withLaterOffsetAtOverlap();
System.out.println(early); // 2024-11-03T01:30-04:00[America/New_York]
System.out.println(late); // 2024-11-03T01:30-05:00[America/New_York]
// Day length on the spring change day
ZonedDateTime start = LocalDate.of(2024, 3, 10).atStartOfDay(ny);
System.out.println(Duration.between(start, start.plusDays(1))); // PT23H
// Calendar day versus 24 hours
System.out.println(start.plusDays(1)); // 2024-03-11T00:00-04:00[America/New_York]
System.out.println(start.plusHours(24)); // 2024-03-11T01:00-04:00[America/New_York]
}
}Quick Reference
| Situation | What Java does | How to change it |
|---|---|---|
| Time falls in a gap | Moves it forward by the gap length | Check the result and pick another time |
| Time falls in an overlap | Uses the earlier offset | `withLaterOffsetAtOverlap()` |
| Adding a calendar day | Keeps the clock time | Use `plusHours(24)` for exact hours |
Exercise
In New York, start is 2024-11-03T00:00 (midnight on the autumn change day). What does Duration.between(start, start.plusDays(1)) print, and why?
Show answer
It prints PT25H. Clocks go back one hour that night, so the 01:00 hour happens twice and the calendar day lasts 25 real hours.
Exercise
You build ZonedDateTime.of(LocalDateTime.of(2024, 3, 10, 2, 15), ZoneId.of("America/New_York")). What local time do you get?
Show answer
03:15 at offset -04:00. The time 02:15 falls in the spring gap, so Java moves it forward by one hour.