Java Reference ยท Date and Time
The TemporalAdjusters class
Reference for java.time.temporal.TemporalAdjusters: find the next Friday, the last day of the month or the second Tuesday, and write your own adjuster.
A TemporalAdjuster is a small strategy that takes a date-time and returns an adjusted one. You apply it with with(adjuster), which every LocalDate, LocalDateTime, ZonedDateTime and similar class offers. The TemporalAdjusters class is a collection of ready-made adjusters for the usual business questions: the first day of next month, the next Monday, the last Friday of the month.
Because the date-time classes are immutable, with returns a new object and leaves the original unchanged.
LocalDate d = LocalDate.of(2026, 3, 9); LocalDate end = d.with(TemporalAdjusters.lastDayOfMonth()); // 2026-03-31
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
interface TemporalAdjuster { Temporal adjustInto(Temporal temporal); }
| Factory method | Result |
|---|---|
firstDayOfMonth(), lastDayOfMonth(), firstDayOfNextMonth() |
Month boundaries. |
firstDayOfYear(), lastDayOfYear(), firstDayOfNextYear() |
Year boundaries. |
next(DayOfWeek), previous(DayOfWeek) |
The next or previous such day, strictly after or before the input. |
nextOrSame(DayOfWeek), previousOrSame(DayOfWeek) |
Like the above, but the input counts if it already matches. |
firstInMonth(DayOfWeek), lastInMonth(DayOfWeek) |
First or last such weekday in the same month. |
dayOfWeekInMonth(int ordinal, DayOfWeek) |
The nth such weekday; -1 means last, 0 means the last one of the previous month. |
ofDateAdjuster(UnaryOperator<LocalDate>) |
Wraps a date-only function as an adjuster (Java 8+). |
Returns: the adjuster; applying it with with(...) returns an adjusted copy of the same type, keeping the time of day and zone.
Usage notes
nextis strict.next(WEDNESDAY)on a Wednesday gives the following Wednesday. UsenextOrSameto keep the date when it already matches.- Enums are adjusters too.
date.with(DayOfWeek.FRIDAY)anddate.with(Month.MAY)work because those enums implementTemporalAdjuster; the former stays in the same ISO week (Monday to Sunday). - Time of day is preserved. Applying an adjuster to a
LocalDateTimechanges only the date fields. - Custom adjusters should be pure functions without side effects. Call
temporal.with(newDate)rather than modifying anything. - Skipping to a boundary instead of a date?
truncatedTo(seeChronoUnit) handles time-of-day boundaries; adjusters handle calendar boundaries. - Thread safety. The factory adjusters are stateless and can be stored in constants.
Related
DayOfWeekandMonth,LocalDate,YearMonth- Lessons: Dates and times