Sub topic 15
Date Arithmetic Shortcuts with TemporalAdjusters
Some date questions come up again and again. When is the last day of this month? What date is next Friday? You could work these out with plusDays and some counting. Java gives you a shortcut instead.
A TemporalAdjuster is an object that takes a date and gives back a changed date. The class TemporalAdjusters (note the "s" at the end) has ready-made adjusters for the most common cases.
How an Adjuster Works
You pass an adjuster to the with method of a date. The with method returns a new date. The original date does not change, because java.time objects are immutable. Immutable means the object cannot be changed after it is created.
A Worked Example
This program starts from Tuesday, November 5, 2024, and jumps to three nearby dates.
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class Adjusters {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2024, 11, 5); // a Tuesday
System.out.println(date.with(TemporalAdjusters.lastDayOfMonth())); // 2024-11-30
System.out.println(date.with(TemporalAdjusters.next(DayOfWeek.FRIDAY))); // 2024-11-08
System.out.println(date.with(TemporalAdjusters.firstDayOfNextMonth())); // 2024-12-01
}
}You don't need to know how many days November has. The adjuster already knows, and it handles leap years too.
Common Adjusters
| Adjuster | What it returns | Result for 2024-11-05 |
|---|---|---|
| `firstDayOfMonth()` | Day 1 of the same month | 2024-11-01 |
| `lastDayOfMonth()` | Last day of the same month | 2024-11-30 |
| `firstDayOfNextMonth()` | Day 1 of the next month | 2024-12-01 |
| `next(DayOfWeek.FRIDAY)` | The next Friday, always after today | 2024-11-08 |
| `nextOrSame(DayOfWeek.TUESDAY)` | Today if it is Tuesday, else the next Tuesday | 2024-11-05 |
| `previous(DayOfWeek.MONDAY)` | The Monday before today | 2024-11-04 |
| `firstInMonth(DayOfWeek.MONDAY)` | The first Monday of the month | 2024-11-04 |
| `lastInMonth(DayOfWeek.FRIDAY)` | The last Friday of the month | 2024-11-29 |
Keep the Result
The with method makes a new object, so you must store what it returns. This line does nothing useful:
date.with(TemporalAdjusters.lastDayOfMonth()); // result thrown awayThis line keeps the answer:
LocalDate monthEnd = date.with(TemporalAdjusters.lastDayOfMonth());Adjusters also work on LocalDateTime and ZonedDateTime. The date part changes and the time of day stays the same.
Exercise
What does LocalDate.of(2024, 2, 10).with(TemporalAdjusters.lastDayOfMonth()) return, and why?
Show answer
It returns 2024-02-29. The year 2024 is a leap year, so February has 29 days, and lastDayOfMonth accounts for that.
Exercise
The date 2024-11-05 is a Tuesday. What do next(DayOfWeek.TUESDAY) and nextOrSame(DayOfWeek.TUESDAY) return for it?
Show answer
next(DayOfWeek.TUESDAY) returns 2024-11-12, because next always moves forward past today. nextOrSame(DayOfWeek.TUESDAY) returns 2024-11-05, because today is already a Tuesday.