Duration and Period are immutable classes in the java.time package that represent an amount of time. Duration measures time-based amounts in seconds and nanoseconds (hours, minutes, seconds), while Period measures date-based amounts in years, months and days. Choose Duration for machine time and elapsed time, and Period for calendar time.
The distinction matters because a day is not always 24 hours and a month is not a fixed number of days. Period follows the calendar, so adding one month to 31 January gives the last day of February, whereas Duration is an exact number of seconds.
Both classes arrived in Java 8 with the Date and Time API (JSR-310) and replace manual arithmetic on java.util.Date and Calendar. Both are immutable and thread-safe, and every method that appears to modify one returns a new instance.
| Feature | Duration | Period |
|---|---|---|
| Measures | Seconds and nanoseconds | Years, months, days |
| Works with | Instant, LocalTime, LocalDateTime, ZonedDateTime | LocalDate, ZonedDateTime, LocalDateTime |
| Calendar aware | No | Yes |
| Typical use | Timeouts, elapsed time, benchmarks | Age, subscription terms, deadlines in days or months |
| ISO-8601 form | PT2H30M | P1Y2M3D |
Use Duration.between to get the gap between two points on the time-line, and Duration.of... factories to create fixed amounts.
toMinutes() returns the whole duration expressed in minutes, while the to...Part() methods (added in Java 9) return only that component, which is what you want when formatting 1h 45m.
To time code, prefer System.nanoTime() or Instant.now() pairs:
Period.between compares two LocalDate values and returns the result split into years, months and days.
The three fields are independent parts, not totals. age.getMonths() is the months component (0 to 11 here), not the total number of months. For a total, call toTotalMonths(). To count the days between two dates, use ChronoUnit.DAYS.between(birth, today) instead of Period.getDays().
Period.ofDays(1) and Duration.ofHours(24) behave differently across a daylight saving change. Adding them to a ZonedDateTime shows it.
On the day clocks move forward in New York, the period keeps the same local time (midnight the next day), while the duration adds exactly 24 hours of elapsed time and lands an hour later on the clock. Pick the class that matches your meaning: "same time tomorrow" is a Period, "86,400 seconds from now" is a Duration.
Both classes read and write ISO-8601 strings.
normalized() folds months into years but never converts days into months, because the length of a month varies. Duration has no normalized step, since seconds always convert cleanly. Neither class has a built-in pretty printer, so format the parts yourself or use toString() for logs and APIs. In Spring Boot, Duration works directly in @ConfigurationProperties with values such as 30s or 5m.
Duration.ofDays(n) exists and means exactly n * 24 hours. It ignores daylight saving changes, so use Period for calendar days.
Use ChronoUnit.DAYS.between(start, end). Period.between(start, end).getDays() returns only the leftover days component after years and months.
No. Duration.between throws a UnsupportedTemporalTypeException for LocalDate because it has no time-of-day. Use Period or ChronoUnit for dates.
Yes. Both are immutable value-based classes, so you can share them freely between threads and use them as constants.
Duration is an exact amount of seconds and nanoseconds for elapsed time, timeouts and instants. Period is a calendar amount of years, months and days for dates and human deadlines. Choose by meaning, remember that the components of a Period are not totals, and expect the two to differ around daylight saving changes.