Java ZonedDateTime set to new date via TemporalAdjuster

Description

Java ZonedDateTime set to new date via TemporalAdjuster

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

public class Main {
  public static void main(String[] args) {
    // Use it to adjust a ZonedDateTime
    ZonedDateTime zdt = ZonedDateTime.now();
    ZonedDateTime zdtAdjusted = zdt.with(my);
    System.out.println(zdt + " adjusted to " + zdtAdjusted);

  }/*from  www  .  j  av  a  2s .c  o m*/

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

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

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



PreviousNext

Related