Skip to content
Tutorials Center
My learning

Topics

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.

Java
LocalDate d = LocalDate.of(2026, 3, 9);
LocalDate end = d.with(TemporalAdjusters.lastDayOfMonth());   // 2026-03-31

Try it

Java The built-in adjusters

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.

Java Writing your own adjuster

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

Java
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

  • next is strict. next(WEDNESDAY) on a Wednesday gives the following Wednesday. Use nextOrSame to keep the date when it already matches.
  • Enums are adjusters too. date.with(DayOfWeek.FRIDAY) and date.with(Month.MAY) work because those enums implement TemporalAdjuster; the former stays in the same ISO week (Monday to Sunday).
  • Time of day is preserved. Applying an adjuster to a LocalDateTime changes 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 (see ChronoUnit) handles time-of-day boundaries; adjusters handle calendar boundaries.
  • Thread safety. The factory adjusters are stateless and can be stored in constants.