Durations & Periods
Durations & Periods
The previous lesson introduced LocalDate, LocalTime, and LocalDateTime as points on the timeline. This lesson is about amounts of time — the gap between two points, or a quantity of time you want to add or subtract. Java's java.time API provides two distinct classes for this, each modelling a different concept:
Duration— a time-based amount measured in seconds and nanoseconds. Use it withLocalTime,LocalDateTime,Instant, or any class that includes a time component.Period— a date-based amount measured in years, months, and days. Use it withLocalDateor the date portion of aLocalDateTime.
Period works in calendar units and handles that variation correctly, while Duration works in exact seconds and is calendar-agnostic. Confusing them leads to subtle bugs — the API forces you to choose consciously.
Duration
A Duration is always stored as a total number of seconds plus a nanosecond adjustment. Its factory methods let you express the amount in a human-readable unit:
The most powerful factory is Duration.between(), which computes the gap between two temporal objects of the same type:
toMinutesPart() (Java 9) returns the minutes component (0–59), not the total. This is the idiomatic way to format a duration without manual arithmetic.
toHoursPart(), toMinutesPart(), toSecondsPart(), and toNanosPart() break a Duration into its human-readable components. Before Java 9 you had to do the arithmetic yourself (e.g. minutes % 60).
Duration Arithmetic
Duration is immutable. Every mutating method returns a new instance:
Period
A Period stores years, months, and days as three separate integer fields — it does not convert between them. Adding a period of one month to 31 January gives 28 February (or 29 in a leap year), not 31 days later, because the calendar arithmetic is done correctly at the moment of application.
Period.between()
Like Duration.between(), Period.between() computes the difference between two LocalDate values in calendar units:
LocalDate has no time component, so passing it to Duration.between() throws a UnsupportedTemporalTypeException. Always match the class: Duration for time-bearing objects, Period for date-only objects.
Period Arithmetic
Note that toTotalMonths() ignores the days field. There is no toTotalDays() because that would require calendar knowledge (a month is not a fixed number of days). If you need total days between two dates, use ChronoUnit.DAYS.between(start, end) instead.
ChronoUnit.between() — a Simpler Alternative
Sometimes you just want a single number: "how many full weeks between these two dates?" ChronoUnit makes this one-liner:
This always returns a single long truncated to whole units. Use it when you need a scalar number rather than a structured Period or Duration.
Applying Amounts to Date-Time Objects
Both classes implement TemporalAmount, so you can pass them to plus() and minus() on any compatible temporal:
LocalDate, use a Period. If it contains a time component (LocalTime, LocalDateTime, Instant), use a Duration. When you mix them up the compiler often lets it through — but the runtime throws an exception, so the discipline needs to come from you.
Summary
Duration models machine time — an exact number of seconds and nanoseconds, calendar-agnostic. Period models human time — years, months, and days that respect the irregularity of the calendar. Both are immutable value objects. Use between() to measure the gap, and plus() / minus() to apply them. When you only need a single numeric count, reach for ChronoUnit.