Java TemporalAdjusters get all Mondays in a month

Description

Java TemporalAdjusters get all Mondays in a month

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

public class Main {
  public static void main(String[] args) {
    LocalDate date = Year.now().atMonth(Month.JANUARY).atDay(1).with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
    Month mi = date.getMonth();/*from   ww  w. j a  v  a 2  s  .  c  o  m*/
    while (mi == Month.JANUARY) {
      System.out.printf("%s%n", date);
      date = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
      mi = date.getMonth();
    }
  }
}



PreviousNext

Related