Duration and periods
What you will learn
Duration measures elapsed seconds and nanoseconds. Period describes calendar years, months and days. They are not interchangeable: a calendar day in a region may contain fewer or more than 24 elapsed hours.
- Calculate elapsed time with Duration and calendar differences with Period.
- Predict the effect of adding a day versus 24 hours across daylight saving.
- Handle month-end adjustments and distinguish elapsed time from calendar billing.
Each example is a complete Main.java file. Use a fresh directory, javac --release 21 Main.java, then java Main. No external dependencies.
Measure a signed event interval
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 start = Instant.parse("2024-01-01T10:00:00Z"); Instant end = Instant.parse("2024-01-01T11:35:30Z"); Duration elapsed = Duration.between(start, end); System.out.println(elapsed); System.out.println(elapsed.toMinutes()); System.out.println(elapsed.toSeconds()); System.out.println(Duration.between(end,start).isNegative()); }}Expected output:
PT1H35M30S955730trueWhole minutes omit the remaining 30 seconds; reversing endpoints creates a negative amount.
Elapsed time
Use Duration.between(start, end) for two instants or compatible time-line values. It can be negative, zero or positive. Duration.ofDays(1) means exactly 86,400 seconds. toHours() returns whole hours, discarding the fractional part toward zero for ordinary positive durations; use minutes or seconds when the remainder matters. Do not mix local clock readings across zones as if they were instants. For elapsed performance measurement use System.nanoTime differences, not wall-clock timestamps; this chapter tests calendar and event intervals, not benchmarks.
Calendar amounts
Use Period.ofMonths(1) for a date-based monthly operation. Period.between(startDate,endDate) expresses a calendar difference in years, months and days, not a total count of days. For total calendar days use ChronoUnit.DAYS.between on LocalDate. A month has variable length. Adding one month to a date such as January 31 resolves an invalid target day to the last valid day of the target month. Repeating that operation may drift away from an original month-end anchor; explicitly retain an anchor if required.
Calendar days versus elapsed hours
On ZonedDateTime, plusDays and a Period day operate on the local calendar, resolving offsets using zone rules. A Duration operates on the timeline. Compare the resulting instants to measure actual elapsed time. Define which one your requirement means: a reservation tomorrow at noon is a local calendar rule, whereas a timeout after 24 hours is an elapsed interval.
Calendar month and leap day
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 { LocalDate start = LocalDate.of(2024, 1, 31); LocalDate next = start.plus(Period.ofMonths(1)); System.out.println(next); System.out.println(next.plusMonths(1)); System.out.println(Period.between(start, next)); System.out.println(ChronoUnit.DAYS.between(start, next)); }}Expected output:
2024-02-292024-03-29P29D29February has 29 days in 2024. The adjusted date is not automatically a reusable month-end anchor.
Calendar day versus 24 hours
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 { var start = ZonedDateTime.of(2024,3,30,12,0,0,0,ZoneId.of("Europe/Stockholm")); var calendar = start.plus(Period.ofDays(1)); var elapsed = start.plus(Duration.ofHours(24)); System.out.println(calendar.toLocalTime()); System.out.println(elapsed.toLocalTime()); System.out.println(Duration.between(start, calendar).toHours()); System.out.println(Duration.between(start, elapsed).toHours()); }}Expected output:
12:0013:002324The spring transition removes an hour from this calendar day. A 24-hour timeout ends an hour later by local time.
Common mistakes
Do not divide a duration by 24 hours to infer a calendar date difference across zones. Period.getDays is only the day component, not the total number of days. Avoid treating plusMonths as a fixed number of seconds or using local times alone for multi-day elapsed events.
Practice
Starting at noon on 2024-10-26 in Europe/Stockholm, add one calendar day and separately 24 elapsed hours. Print both local times and the number of elapsed hours until the calendar result.
Expected output:
12:0011:0025Compare your answer with the manual practice criteria.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Duration and periods — knowledge check
Original arithmetic, time and localization questions. Java 21-compatible scope.
Which type represents 24 elapsed hours?
How many seconds does Duration.ofDays(1) represent?
What is Period.getDays?
What happens to 2024-01-31 plus one month?
Which operation counts calendar days between LocalDate values?