Java : TemporalAdjusters with Examples

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


Summary

Common and useful TemporalAdjusters.

Class diagram

final var adjuster = TemporalAdjusters.lastDayOfMonth();

final var date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01
System.out.println(date1.with(adjuster)); // 2100-01-31

final var date2 = LocalDate.of(2100, 2, 15);
System.out.println(date2); // 2100-02-15
System.out.println(date2.with(adjuster)); // 2100-02-28

final var dateTime = LocalDateTime.of(2100, 4, 2, 12, 30);
System.out.println(dateTime); // 2100-04-02T12:30
System.out.println(dateTime.with(adjuster)); // 2100-04-30T12:30

Methods

static TemporalAdjuster dayOfWeekInMonth (int ordinal, DayOfWeek dayOfWeek)

Returns the day-of-week in month adjuster, which returns a new date with the ordinal day-of-week based on the month.

final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);

final var date = LocalDate.of(2100, 12, 1);
System.out.println(date.format(formatter)); // Wednesday, December 1, 2100

{
    final var ret1 = date.with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.WEDNESDAY));
    System.out.println(ret1.format(formatter)); // Wednesday, December 1, 2100

    final var ret2 = date.with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.THURSDAY));
    System.out.println(ret2.format(formatter)); // Thursday, December 2, 2100

    final var ret3 = date.with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.FRIDAY));
    System.out.println(ret3.format(formatter)); // Friday, December 3, 2100
}
{
    final var ret1 = date.with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.SUNDAY));
    System.out.println(ret1.format(formatter)); // Sunday, December 5, 2100

    final var ret2 = date.with(TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.SUNDAY));
    System.out.println(ret2.format(formatter)); // Sunday, December 12, 2100

    final var ret3 = date.with(TemporalAdjusters.dayOfWeekInMonth(3, DayOfWeek.SUNDAY));
    System.out.println(ret3.format(formatter)); // Sunday, December 19, 2100
}

static TemporalAdjuster firstDayOfMonth ()

Returns the "first day of month" adjuster, which returns a new date set to the first day of the current month.

final var adjuster = TemporalAdjusters.firstDayOfMonth();

final var date1 = LocalDate.of(2100, 1, 15);
System.out.println(date1); // 2100-01-15
System.out.println(date1.with(adjuster)); // 2100-01-01

final var date2 = LocalDate.of(2100, 2, 28);
System.out.println(date2); // 2100-02-28
System.out.println(date2.with(adjuster)); // 2100-02-01

final var date3 = LocalDate.of(1999, 12, 31);
System.out.println(date3); // 1999-12-31
System.out.println(date3.with(adjuster)); // 1999-12-01

static TemporalAdjuster firstDayOfNextMonth ()

Returns the "first day of next month" adjuster, which returns a new date set to the first day of the next month.

final var adjuster = TemporalAdjusters.firstDayOfNextMonth();

final var date1 = LocalDate.of(2100, 1, 15);
System.out.println(date1); // 2100-01-15
System.out.println(date1.with(adjuster)); // 2100-02-01

final var date2 = LocalDate.of(2100, 2, 28);
System.out.println(date2); // 2100-02-28
System.out.println(date2.with(adjuster)); // 2100-03-01

final var date3 = LocalDate.of(1999, 12, 31);
System.out.println(date3); // 1999-12-31
System.out.println(date3.with(adjuster)); // 2000-01-01

static TemporalAdjuster firstDayOfNextYear ()

Returns the "first day of next year" adjuster, which returns a new date set to the first day of the next year.

final var adjuster = TemporalAdjusters.firstDayOfNextYear();

final var date1 = LocalDate.of(2100, 1, 15);
System.out.println(date1); // 2100-01-15
System.out.println(date1.with(adjuster)); // 2101-01-01

final var date2 = LocalDate.of(2100, 2, 28);
System.out.println(date2); // 2100-02-28
System.out.println(date2.with(adjuster)); // 2101-01-01

final var date3 = LocalDate.of(1999, 12, 31);
System.out.println(date3); // 1999-12-31
System.out.println(date3.with(adjuster)); // 2000-01-01

static TemporalAdjuster firstDayOfYear ()

Returns the "first day of year" adjuster, which returns a new date set to the first day of the current year.

final var adjuster = TemporalAdjusters.firstDayOfYear();

final var date1 = LocalDate.of(2100, 1, 15);
System.out.println(date1); // 2100-01-15
System.out.println(date1.with(adjuster)); // 2100-01-01

final var date2 = LocalDate.of(2100, 2, 28);
System.out.println(date2); // 2100-02-28
System.out.println(date2.with(adjuster)); // 2100-01-01

final var date3 = LocalDate.of(1999, 12, 31);
System.out.println(date3); // 1999-12-31
System.out.println(date3.with(adjuster)); // 1999-01-01

static TemporalAdjuster firstInMonth (DayOfWeek dayOfWeek)

Returns the first in month adjuster, which returns a new date in the same month with the first matching day-of-week.

final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);

final var date = LocalDate.of(2100, 12, 1);
System.out.println(date.format(formatter)); // Wednesday, December 1, 2100

final var ret1 = date.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
System.out.println(ret1.format(formatter)); // Monday, December 6, 2100

final var ret2 = date.with(TemporalAdjusters.firstInMonth(DayOfWeek.TUESDAY));
System.out.println(ret2.format(formatter)); // Tuesday, December 7, 2100

final var ret3 = date.with(TemporalAdjusters.firstInMonth(DayOfWeek.WEDNESDAY));
System.out.println(ret3.format(formatter)); // Wednesday, December 1, 2100

final var ret4 = date.with(TemporalAdjusters.firstInMonth(DayOfWeek.THURSDAY));
System.out.println(ret4.format(formatter)); // Thursday, December 2, 2100

final var ret5 = date.with(TemporalAdjusters.firstInMonth(DayOfWeek.FRIDAY));
System.out.println(ret5.format(formatter)); // Friday, December 3, 2100

static TemporalAdjuster lastDayOfMonth ()

Returns the "last day of month" adjuster, which returns a new date set to the last day of the current month.

final var adjuster = TemporalAdjusters.lastDayOfMonth();

final var date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01
System.out.println(date1.with(adjuster)); // 2100-01-31

final var date2 = LocalDate.of(2100, 2, 15);
System.out.println(date2); // 2100-02-15
System.out.println(date2.with(adjuster)); // 2100-02-28

final var date3 = LocalDate.of(1999, 4, 3);
System.out.println(date3); // 1999-04-03
System.out.println(date3.with(adjuster)); // 1999-04-30

static TemporalAdjuster lastDayOfYear ()

Returns the "last day of year" adjuster, which returns a new date set to the last day of the current year.

final var adjuster = TemporalAdjusters.lastDayOfYear();

final var date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01
System.out.println(date1.with(adjuster)); // 2100-12-31

final var date2 = LocalDate.of(2100, 2, 15);
System.out.println(date2); // 2100-02-15
System.out.println(date2.with(adjuster)); // 2100-12-31

final var date3 = LocalDate.of(1999, 12, 3);
System.out.println(date3); // 1999-12-03
System.out.println(date3.with(adjuster)); // 1999-12-31

static TemporalAdjuster lastInMonth (DayOfWeek dayOfWeek)

Returns the last in month adjuster, which returns a new date in the same month with the last matching day-of-week.

final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);

final var date = LocalDate.of(2100, 6, 1);
System.out.println(date.format(formatter)); // Tuesday, June 1, 2100

final var ret1 = date.with(TemporalAdjusters.lastInMonth(DayOfWeek.MONDAY));
System.out.println(ret1.format(formatter)); // Monday, June 28, 2100

final var ret2 = date.with(TemporalAdjusters.lastInMonth(DayOfWeek.TUESDAY));
System.out.println(ret2.format(formatter)); // Tuesday, June 29, 2100

final var ret3 = date.with(TemporalAdjusters.lastInMonth(DayOfWeek.WEDNESDAY));
System.out.println(ret3.format(formatter)); // Wednesday, June 30, 2100

final var ret4 = date.with(TemporalAdjusters.lastInMonth(DayOfWeek.THURSDAY));
System.out.println(ret4.format(formatter)); // Thursday, June 24, 2100

final var ret5 = date.with(TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY));
System.out.println(ret5.format(formatter)); // Friday, June 25, 2100

static TemporalAdjuster next (DayOfWeek dayOfWeek)

Returns the next day-of-week adjuster, which adjusts the date to the first occurrence of the specified day-of-week after the date being adjusted.

final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);

final var date = LocalDate.of(2100, 12, 15);
System.out.println(date.format(formatter)); // Wednesday, December 15, 2100

final var ret1 = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
System.out.println(ret1.format(formatter)); // Monday, December 20, 2100

final var ret2 = date.with(TemporalAdjusters.next(DayOfWeek.TUESDAY));
System.out.println(ret2.format(formatter)); // Tuesday, December 21, 2100

final var ret3 = date.with(TemporalAdjusters.next(DayOfWeek.WEDNESDAY));
System.out.println(ret3.format(formatter)); // Wednesday, December 22, 2100

final var ret4 = date.with(TemporalAdjusters.next(DayOfWeek.THURSDAY));
System.out.println(ret4.format(formatter)); // Thursday, December 16, 2100

final var ret5 = date.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
System.out.println(ret5.format(formatter)); // Friday, December 17, 2100

static TemporalAdjuster nextOrSame (DayOfWeek dayOfWeek)

Returns the next-or-same day-of-week adjuster, which adjusts the date to the first occurrence of the specified day-of-week after the date being adjusted unless it is already on that day in which case the same object is returned.

final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);

final var date = LocalDate.of(2100, 12, 15);
System.out.println(date.format(formatter)); // Wednesday, December 15, 2100

final var ret1 = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));
System.out.println(ret1.format(formatter)); // Monday, December 20, 2100

final var ret2 = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.TUESDAY));
System.out.println(ret2.format(formatter)); // Tuesday, December 21, 2100

final var ret3 = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.WEDNESDAY));
System.out.println(ret3.format(formatter)); // Wednesday, December 15, 2100

final var ret4 = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.THURSDAY));
System.out.println(ret4.format(formatter)); // Thursday, December 16, 2100

final var ret5 = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY));
System.out.println(ret5.format(formatter)); // Friday, December 17, 2100

static TemporalAdjuster ofDateAdjuster (UnaryOperator<LocalDate> dateBasedAdjuster)

Obtains a TemporalAdjuster that wraps a date adjuster.

final var adjuster = TemporalAdjusters.ofDateAdjuster(date -> {
    return date.plusDays(2);
});

final var date1 = LocalDate.of(2100, 1, 1);
System.out.println(date1); // 2100-01-01
System.out.println(date1.with(adjuster)); // 2100-01-03

final var date2 = LocalDate.of(1999, 12, 31);
System.out.println(date2); // 1999-12-31
System.out.println(date2.with(adjuster)); // 2000-01-02

static TemporalAdjuster previous (DayOfWeek dayOfWeek)

Returns the previous day-of-week adjuster, which adjusts the date to the first occurrence of the specified day-of-week before the date being adjusted.

final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);

final var date = LocalDate.of(2100, 12, 15);
System.out.println(date.format(formatter)); // Wednesday, December 15, 2100

final var ret1 = date.with(TemporalAdjusters.previous(DayOfWeek.MONDAY));
System.out.println(ret1.format(formatter)); // Monday, December 13, 2100

final var ret2 = date.with(TemporalAdjusters.previous(DayOfWeek.TUESDAY));
System.out.println(ret2.format(formatter)); // Tuesday, December 14, 2100

final var ret3 = date.with(TemporalAdjusters.previous(DayOfWeek.WEDNESDAY));
System.out.println(ret3.format(formatter)); // Wednesday, December 8, 2100

final var ret4 = date.with(TemporalAdjusters.previous(DayOfWeek.THURSDAY));
System.out.println(ret4.format(formatter)); // Thursday, December 9, 2100

final var ret5 = date.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY));
System.out.println(ret5.format(formatter)); // Friday, December 10, 2100

static TemporalAdjuster previousOrSame (DayOfWeek dayOfWeek)

Returns the previous-or-same day-of-week adjuster, which adjusts the date to the first occurrence of the specified day-of-week before the date being adjusted unless it is already on that day in which case the same object is returned.

final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);

final var date = LocalDate.of(2100, 12, 15);
System.out.println(date.format(formatter)); // Wednesday, December 15, 2100

final var ret1 = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
System.out.println(ret1.format(formatter)); // Monday, December 13, 2100

final var ret2 = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.TUESDAY));
System.out.println(ret2.format(formatter)); // Tuesday, December 14, 2100

final var ret3 = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.WEDNESDAY));
System.out.println(ret3.format(formatter)); // Wednesday, December 15, 2100

final var ret4 = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.THURSDAY));
System.out.println(ret4.format(formatter)); // Thursday, December 9, 2100

final var ret5 = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.FRIDAY));
System.out.println(ret5.format(formatter)); // Friday, December 10, 2100

Related posts

To top of page