Get all of the Mondays in the current year and the specified month

Description

The following code shows how to get all of the Mondays in the current year and the specified month.

Example


import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.Year;
import java.time.temporal.TemporalAdjusters;
/*from   w  w w  .j  a  va 2s  .c  o m*/
public class Main {
    public static void main(String[] args) {
        Month month = Month.valueOf("March".toUpperCase());

        System.out.printf("For the month of %s:%n", month);
        LocalDate date = Year.now().atMonth(month).atDay(1).
              with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
        Month mi = date.getMonth();
        while (mi == month) {
            System.out.printf("%s%n", date);
            date = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
            mi = date.getMonth();
        }
    }
}

The code above generates the following result.





















Home »
  Java Date Time »
    Example »




Convert
Date
Timezone