Java TemporalAdjuster Create from method reference

Description

Java TemporalAdjuster Create from method reference


import static java.time.DayOfWeek.MONDAY;
import static java.time.DayOfWeek.SATURDAY;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

public class Main {
  public static void main(String[] args) {
    LocalDate ld1 = LocalDate.of(2020, Month.JANUARY, 12); 

    LocalDate ld1Adjusted = ld1.with(my);
    System.out.println(ld1 + " adjusted to " + ld1Adjusted);

  }//from www . jav a  2s  .c om

  public final static TemporalAdjuster my = TemporalAdjusters
      .ofDateAdjuster(Main::newDay);

  private static LocalDate newDay(LocalDate date) {
    LocalDate newDate = date;

    DayOfWeek day = date.getDayOfWeek();
    if (day == SATURDAY && date.getDayOfMonth() == 13) {
      newDate = date.with(TemporalAdjusters.next(MONDAY));
    }
    return newDate;
  }
}



PreviousNext

Related