Sub topic 16
Daylight Saving Time: Gaps, Overlaps, and Day Lengths
Daylight saving time (DST) is a rule some places follow: clocks move forward one hour in spring and back one hour in autumn. The clock change makes some local times strange. Some never happen, some happen twice, and some days are not 24 hours long. This chapter shows how java.time handles each case, so your code does not break twice a year.
The examples use the America/New_York zone. In 2024, its clocks jumped forward on March 10 and fell back on November 3.
What Happens When the Clocks Change
A transition is the moment a zone switches from one offset to another. An offset is the gap between local time and UTC, such as -05:00. New York uses -05:00 in winter and -04:00 in summer.
A transition can produce two kinds of odd local times:
- A gap is a range of local times that never happen. On March 10, 2024, at 2:00 AM, New York clocks jumped straight to 3:00 AM. So 2:30 AM did not exist that day.
- An overlap is a range of local times that happen twice. On November 3, 2024, at 2:00 AM, clocks went back to 1:00 AM. So 1:30 AM happened once with offset
-04:00and again an hour later with offset-05:00.
| Situation | Example local time | Valid offsets | What `java.time` does by default |
|---|---|---|---|
| Normal | 2024-07-01 12:00 | one: `-04:00` | Uses that offset |
| Gap | 2024-03-10 02:30 | none | Moves the time forward by the gap length |
| Overlap | 2024-11-03 01:30 | two: `-04:00` and `-05:00` | Picks the earlier offset (`-04:00`) |
A LocalDateTime has no zone, so it never runs into these problems. They show up only when you attach a zone to turn it into a ZonedDateTime.
Gaps: Times That Never Happen
When you build a ZonedDateTime from a local time inside a gap, Java does not throw an error. It moves the time forward by the length of the gap, which is usually one hour. The program below also measures how long March 10 lasted.
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");
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]
ZonedDateTime start = LocalDate.of(2024, 3, 10).atStartOfDay(ny);
ZonedDateTime end = start.plusDays(1);
System.out.println(Duration.between(start, end)); // PT23H
}
}You asked for 2:30 AM, and Java gave you 3:30 AM. That is usually a sensible answer. For example, an alarm set for 2:30 AM should still go off. Still, it can surprise you if you expect the hour you typed.
Overlaps: Times That Happen Twice
In an overlap, one local time matches two real moments. By default Java picks the earlier offset, which means the first time the clock shows that time. You can switch to the second one with withLaterOffsetAtOverlap(). You can switch back with withEarlierOffsetAtOverlap().
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Overlap {
public static void main(String[] args) {
ZoneId ny = ZoneId.of("America/New_York");
LocalDateTime local = LocalDateTime.of(2024, 11, 3, 1, 30);
ZonedDateTime first = ZonedDateTime.of(local, ny);
ZonedDateTime second = first.withLaterOffsetAtOverlap();
System.out.println(first); // 2024-11-03T01:30-04:00[America/New_York]
System.out.println(second); // 2024-11-03T01:30-05:00[America/New_York]
System.out.println(Duration.between(first, second)); // PT1H
}
}Both values print the same wall-clock time, 1:30. The offsets differ, and the two moments are a full hour apart. Outside an overlap, these two methods do nothing and return the same value.
Checking for a Gap or Overlap
Every ZoneId has a ZoneRules object. ZoneRules holds the zone's full history of offset changes. Its getValidOffsets method tells you how many offsets a local time has: zero means a gap, two means an overlap, and one means a normal time.
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.zone.ZoneRules;
import java.util.List;
public class CheckTransitions {
public static void main(String[] args) {
ZoneRules rules = ZoneId.of("America/New_York").getRules();
LocalDateTime[] samples = {
LocalDateTime.of(2024, 7, 1, 12, 0),
LocalDateTime.of(2024, 3, 10, 2, 30),
LocalDateTime.of(2024, 11, 3, 1, 30)
};
for (LocalDateTime t : samples) {
List<ZoneOffset> offsets = rules.getValidOffsets(t);
String kind = offsets.isEmpty() ? "gap"
: offsets.size() == 2 ? "overlap"
: "normal";
System.out.println(t + " " + kind + " " + offsets);
}
// 2024-07-01T12:00 normal [-04:00]
// 2024-03-10T02:30 gap []
// 2024-11-03T01:30 overlap [-04:00, -05:00]
}
}You can use this check to warn a user before you save a time that does not exist or that is ambiguous.
Day Lengths: plusDays Versus plusHours
The first program showed that March 10, 2024, lasted only 23 hours in New York. November 3 lasted 25 hours. As a result, adding one day and adding 24 hours give different results across a transition.
plusDays(1)works on the calendar. It keeps the same local clock time on the next day. A day added this way acts like aPeriod.plusHours(24)works on the timeline. It adds exactly 24 real hours. Hours added this way act like aDuration.
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
public class DayLength {
public static void main(String[] args) {
ZoneId ny = ZoneId.of("America/New_York");
ZonedDateTime midnight = LocalDate.of(2024, 3, 10).atStartOfDay(ny);
System.out.println(midnight.plusDays(1)); // 2024-03-11T00:00-04:00[America/New_York]
System.out.println(midnight.plusHours(24)); // 2024-03-11T01:00-04:00[America/New_York]
ZonedDateTime fallStart = LocalDate.of(2024, 11, 3).atStartOfDay(ny);
ZonedDateTime fallEnd = fallStart.plusDays(1);
System.out.println(ChronoUnit.HOURS.between(fallStart, fallEnd)); // 25
}
}Pick the method that matches what you mean:
| You want | Use | Example |
|---|---|---|
| Same clock time tomorrow | `plusDays(1)` or `Period.ofDays(1)` | A daily 9:00 AM meeting |
| Exactly 24 real hours later | `plusHours(24)` or `Duration.ofHours(24)` | A token that expires after 24 hours |
Common Mistakes
- Doing DST math with
LocalDateTime. ALocalDateTimehas no zone, so it treats every day as 24 hours. UseZonedDateTimewhen real elapsed time matters. - Using a fixed offset instead of a region.
ZoneOffset.of("-05:00")never changes, so it ignores DST. Use a region ID such asZoneId.of("America/New_York"). - Assuming a day is 24 hours. Measure with
Duration.betweenon zoned values, or add calendar days withplusDays. - Trusting the user's input without checking. A time typed into a form may fall in a gap or an overlap. Check it with
getValidOffsets.
Exercise
In the America/NewYork zone, what does ZonedDateTime.of(LocalDateTime.of(2024, 3, 10, 2, 15), ZoneId.of("America/NewYork")) print, and why?
Show answer
It prints 2024-03-10T03:15-04:00[America/New_York]. 2:15 AM falls in the spring gap (2:00 to 3:00) and never happened, so Java moves it forward by the one-hour gap length.
Exercise
You build a ZonedDateTime for 2024-11-03 at 01:45 in America/New_York. Which offset does Java choose, and how do you get the other occurrence of 01:45?
Show answer
Java chooses the earlier offset, -04:00, which is the first time the clock shows 01:45. Call withLaterOffsetAtOverlap() to get the second occurrence, with offset -05:00, one hour later on the timeline.
Exercise
A reminder should fire at 8:00 AM local time every day, even across DST changes. Should you advance it with plusDays(1) or plusHours(24) on a ZonedDateTime? Explain.
Show answer
Use plusDays(1). It keeps the local clock time at 8:00 AM on the next calendar day. plusHours(24) adds exactly 24 real hours, so on a 23-hour or 25-hour day the reminder would drift to 9:00 AM or 7:00 AM.