get weeks In Calendar - Java java.time

Java examples for java.time:Week

Description

get weeks In Calendar

Demo Code


//package com.java2s;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.ArrayList;

import java.util.List;

public class Main {
    private static final DayOfWeek FIRST_DAY_OF_WEEK = DayOfWeek.MONDAY;

    public static List<LocalDate> weeksInCalendar(YearMonth month) {
        List<LocalDate> firstDaysOfWeeks = new ArrayList<>();
        for (LocalDate day = firstDayOfCalendar(month); stillInCalendar(
                month, day); day = day.plusWeeks(1)) {
            firstDaysOfWeeks.add(day);/*from  w  w w  . j a v a2 s .  com*/
        }
        return firstDaysOfWeeks;
    }

    private static LocalDate firstDayOfCalendar(YearMonth month) {
        return month.atDay(1).with(FIRST_DAY_OF_WEEK);
    }

    private static boolean stillInCalendar(YearMonth yearMonth,
            LocalDate day) {
        return !day.isAfter(yearMonth.atEndOfMonth());
    }
}

Related Tutorials