Java : TemporalAdjuster with Examples

TemporalAdjuster (Java SE 21 & JDK 21) with Examples.
You will find code examples on most TemporalAdjuster methods.


Summary

Strategy for adjusting a temporal object.

Class diagram

Please see also : TemporalAdjusters

final var temporal = LocalDateTime.of(2100, 4, 10, 17, 30);
System.out.println(temporal); // 2100-04-10T17:30

final TemporalAdjuster adjuster = LocalDate.of(1999, 1, 2);
System.out.println(adjuster); // 1999-01-02

final var ret = adjuster.adjustInto(temporal);
System.out.println(ret); // 1999-01-02T17:30
final var date = LocalDate.of(2100, Month.APRIL, 15);
System.out.println(date); // 2100-04-15

final var adjuster1 = TemporalAdjusters.firstDayOfMonth();
System.out.println(date.with(adjuster1)); // 2100-04-01

final var adjuster2 = TemporalAdjusters.lastDayOfMonth();
System.out.println(date.with(adjuster2)); // 2100-04-30

Methods

Temporal adjustInto (Temporal temporal)

Adjusts the specified temporal object.

final var temporal = LocalDateTime.of(2100, 4, 10, 17, 30);
System.out.println(temporal); // 2100-04-10T17:30

final TemporalAdjuster adjuster = LocalDate.of(1999, 1, 2);
System.out.println(adjuster); // 1999-01-02

final var ret = adjuster.adjustInto(temporal);
System.out.println(ret); // 1999-01-02T17:30

Related posts

To top of page