Lesson · Date & Time API
Arithmetic and queries: plus, with and between
13 min readArithmetic & Queries
Date maths has edges: months end early, clock readings wrap, and a day is not always 24 hours. java.time makes each of those cases explicit through plus, with, adjusters and the between queries.
plus and minus clamp at month ends
Adding months to a date clamps to the last valid day: January 31 plus one month is February 28 (or 29 in a leap year), never March 3. The clamp happens on the operation, not on the value: plus two months from the same January 31 lands on March 31, because the arithmetic runs from the original day.
This is deliberately different from Calendar's lenient rollover. The clamp is the only behavior that cannot invent a month boundary, and it is documented: the day-of-month is adjusted to the largest valid value for the target month.
LocalDate.of(2026, 1, 31).plusMonths(1); // 2026-02-28 LocalDate.of(2026, 1, 31).plusMonths(2); // 2026-03-31 LocalDate.of(2024, 2, 29).plusYears(1); // 2025-02-28
Edge calendar · clamp, wrap, stretch
LocalDate.of(2026, 1, 31)
Start: January 31, 2026. Every experiment below starts from this one value.
Times wrap, dates clamp: know which you hold
LocalTime is a reading on a 24-hour dial, so 23:30 plus two hours is 01:30 the next conceptual day. LocalTime does not carry a date, so the wrap is pure arithmetic with no information lost; the day belongs to whatever LocalDate you pair it with.
If you need the wrap to move a date forward, hold a LocalDateTime where adding hours advances the date: 23:30 on the 21st plus two hours is 01:30 on the 22nd.
LocalTime.of(23, 30).plusHours(2); // 01:30, same dial LocalDateTime.of(2026, 9, 21, 23, 30).plusHours(2); // 2026-09-22T01:30
Period counts calendar units, Duration counts machine time
Period.between(a, b) breaks the gap into years, months and days: January 1 to March 15 is 2 months and 14 days. Duration.between(a, b) measures exact elapsed time: nine to 17:45 is PT8H45M. The same gap can be described by both, and which one is right depends on the question.
For totals in one unit, use ChronoUnit.between: DAYS.between counts actual days, which is the number invoicing and age calculators want. The Period breakdown is for display (2 months and 14 days) and for calendar-aligned logic such as billing cycles.
Period.between(LocalDate.of(2026, 1, 1), LocalDate.of(2026, 3, 15)); // P2M14D ChronoUnit.DAYS.between(LocalDate.of(2026, 1, 1), LocalDate.of(2026, 3, 15)); // 73 Duration.between(LocalTime.of(9, 0), LocalTime.of(17, 45)); // PT8H45M
between and until: asking questions instead of computing
The unit.enum between static methods and the instance until method answer the everyday questions: how many days until the deadline, how many months between statements. They replace hand-rolled millisecond arithmetic and its off-by-one DST errors.
ChronoUnit.DAYS.between(today, deadline); // a long, sign carries direction ChronoUnit.MONTHS.between(statementFrom, statementTo); start.until(end, ChronoUnit.MINUTES); // same measure, instance style
with* replaces one field, adjusters snap to calendar facts
The with methods replace a single field and keep everything else: withMonth, withDayOfMonth, withHour. Replacing day 31 with month February clamps the same way plusMonths does, so month order of operations rarely surprises.
TemporalAdjusters covers the snapping questions: lastDayOfMonth, next(DayOfWeek.MONDAY), firstDayOfNextMonth, dayOfWeekInMonth. They are the answer to 'the third Friday of the month' without arithmetic.
date.with(TemporalAdjusters.lastDayOfMonth()); // 2026-02-28 date.with(TemporalAdjusters.next(DayOfWeek.MONDAY)); // next Monday strictly after date date.with(TemporalAdjusters.dayOfWeekInMonth(3, DayOfWeek.FRIDAY)); // third Friday
Days are not always 24 hours
In a zone with daylight saving, a calendar day can be 23 or 25 hours. ZonedDateTime.plusDays(1) keeps the wall clock reading and crosses the transition, so 12:00 stays 12:00 even though only 23 real hours passed. Adding Duration.ofHours(24) instead lands an hour later on the clock, because it adds real elapsed time.
This is the strongest practical reason the Duration versus Period split exists: schedule people with calendar units and machines with durations, and DST stops being a bug class.
ZonedDateTime before = ZonedDateTime.of(2026, 3, 28, 12, 0, 0, 0, ZoneId.of("Europe/Berlin")); before.plusDays(1); // 2026-03-29T12:00+02:00: same reading, 23 hours later before.plus(Duration.ofHours(24)); // 2026-03-29T13:00+02:00: 24 real hours later
If a line of code uses plusDays on a ZonedDateTime, the author is scheduling a person. If it uses plus(Duration), the author is measuring elapsed time. The method choice documents the intent.
The edges are the lesson
Month ends clamp, clocks wrap, days stretch to 25 hours. Every one of these edges used to be a silent rollover in Calendar; in java.time each is a documented, testable behavior you choose by choosing the method.
Interview angles
What is January 31 plus one month, and why?+
February 28 (or 29 in a leap year). The day-of-month is clamped to the largest valid value in the target month. Adding two months from January 31 gives March 31, because each operation starts from the original value.
Difference between Period and Duration in one sentence?+
Period is calendar time (years, months, days; variable lengths), Duration is exact time (hours, minutes, seconds, nanoseconds). Use Period for human schedules and Duration for elapsed machine time.
How do you get the number of days between two dates?+
ChronoUnit.DAYS.between(start, end): one number, direction via sign, no month-arithmetic ambiguity. Period.between answers a different question, a human-readable breakdown into years, months and days.
Why can adding a day to a ZonedDateTime differ from adding 24 hours?+
Across a DST transition the calendar day is 23 or 25 hours. plusDays keeps the wall clock reading (person-facing), plus(Duration.ofHours(24)) adds exactly 24 elapsed hours (machine-facing).
How do you find the third Friday of a month?+
with(TemporalAdjusters.dayOfWeekInMonth(3, DayOfWeek.FRIDAY)) on any date in that month. Adjusters express calendar queries without manual arithmetic.