Duration measures elapsed seconds, while Period advances calendar fields on a date or zoned date-time.
Java Duration versus Period across a daylight-saving change
Choose a timeline
A shipping deadline 24 hours from now needs Duration. A daily collection at noon needs calendar-day arithmetic. In Europe/Paris, the spring clock change makes those two rules produce different wall times even though both are described casually as one day later.
The fixture starts before the 2024 spring transition. Calendar addition keeps noon and spans 23 elapsed hours; a 24-hour duration lands at 13:00. Gap and overlap rules explain why a local date-time cannot always be converted to one instant without policy.
Persist the right value
An absolute expiry belongs on the instant timeline. A recurring local appointment needs its local rule and region zone, not merely the next computed instant. Injected clocks make expiry tests deterministic.
Working program
import java.time.Duration;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ParisCollectionSchedule {
public static void main(String[] args) {
ZonedDateTime pickup = ZonedDateTime.of(2024, 3, 30, 12, 0, 0, 0, ZoneId.of("Europe/Paris"));
ZonedDateTime calendarNext = pickup.plus(Period.ofDays(1));
ZonedDateTime elapsedNext = pickup.plus(Duration.ofDays(1));
System.out.println(calendarNext.toLocalTime());
System.out.println(elapsedNext.toLocalTime());
System.out.println(Duration.between(pickup, calendarNext).toHours());
}
}Output
12:00
13:00
23Cost and ownership
Both operations use fixed-size time objects and constant application storage. Their computational cost is small relative to persistence or scheduling, but their business meanings differ by an hour here; converting a recurring rule into a duration loses that rule.
Common Mistakes
- Do not substitute 24 hours for a calendar day in a regional schedule.
- Do not store only the next instant for a recurring local-time rule.
- Do not compare LocalDateTime values as if they were elapsed timestamps.
Read next
Java daylight-saving transitions: reject gaps and choose overlaps, Java date and time: local schedules versus instants, Java Clock: test expiry without sleeping, zoned zone conversion.
