Temporal Adjusters in Java 8.

Since Java 8, the TemporalAdjusters class (note the plural) provides a set of predefined adjusters which makes it easy to find the first or last day of the month, the first or last day of the year, the last Sunday of a month, or the first Tuesday after a specific date or the first Monday of a month. The predefined adjusters are defined as static methods and are designed to be used with the static import statement.

Predefined Adjusters.

    public static void main(String args[]){
        LocalDate today = LocalDate.now();
        System.out.println("Today --> " + today);
        System.out.println("First Day of this Month :" + today.with(TemporalAdjusters.firstDayOfMonth()));
        System.out.println("First Monday of this Month :" + today.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)));
        System.out.println("Last Day of this Month :" + today.with(TemporalAdjusters.lastDayOfMonth()));
        System.out.println("Last Sunday of this Month :" + today.with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY)));
        System.out.println("First Day of next Month :" + today.with(TemporalAdjusters.firstDayOfNextMonth()));
        System.out.println("First Day of next Year :" + today.with(TemporalAdjusters.firstDayOfNextYear()));
        System.out.println("Last Day of this Month :" + today.with(TemporalAdjusters.lastDayOfMonth()));
        System.out.println("Last Day of this Year :" + today.with(TemporalAdjusters.lastDayOfYear()));
        System.out.println("Next Saturday :" + today.with(TemporalAdjusters.next(DayOfWeek.SATURDAY)));
        System.out.println("Next of Same Saturday :" + today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SATURDAY)));
        System.out.println("Previous Saturday :" + today.with(TemporalAdjusters.previous(DayOfWeek.SATURDAY)));
        System.out.println("Previous of Same Saturday :" + today.with(TemporalAdjusters.previousOrSame(DayOfWeek.SATURDAY)));
    }

Output:

Today --> 2021-08-14
First Day of this Month :2021-08-01
First Monday of this Month :2021-08-02
Last Day of this Month :2021-08-31
Last Sunday of this Month :2021-08-29
First Day of next Month :2021-09-01
First Day of next Year :2022-01-01
Last Day of this Month :2021-08-31
Last Day of this Year :2021-12-31
Next Saturday :2021-08-21
Next of Same Saturday :2021-08-14
Previous Saturday :2021-08-07
Previous of Same Saturday :2021-08-14

Custom Temporal Adjusters:

We can create our own custom adjuster. To do this create a class that implements TemporalAdjuster Interface and implement adjustInto(Temporal var1) method.

A sample custom adjuster to calculate the bimonthly pay day (from Oracle tutorial blog) is provided below for reference.

public class PayDayAdjuster implements TemporalAdjuster {
@Override
public Temporal adjustInto(Temporal temporal) {
LocalDate date = LocalDate.from(temporal);
int day;
if (date.getDayOfMonth() < 15) {
day = 15;
} else {
day = date.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
}
date = date.withDayOfMonth(day);
if (date.getDayOfWeek() == DayOfWeek.SATURDAY ||
date.getDayOfWeek() == DayOfWeek.SUNDAY) {
date = date.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY));
}
return temporal.with(date);
}
}

To use this custom temporal adjuster check the code below.

public static void main(String args[]){
LocalDate today = LocalDate.of(2021, 8, 1);
System.out.println("NextPayDay --> " + today.with(new PayDayAdjuster()));
}

Output of the above code:

NextPayDay --> 2021-08-13

Two main piece of code to consider:

  1. To get a date from Temporal.
 LocalDate date = LocalDate.from(temporal); 
  1. To get a Temporal from date.
temporal.with(date);

%d bloggers like this: