DEV Community

Kentama7
Kentama7

Posted on

カレンダーの行数を求める

カレンダーの行数を求める。

算出方法は以下を参考。
https://biz.hino-tama.com/creative/blog0012/

int calendarRows(LocalDate date) {
    if (date == null) {
        throw new IllegalArgumentException();
    }

    LocalDate firstDay = date.with(TemporalAdjusters.firstDayOfMonth());
    DayOfWeek dow = firstDay.getDayOfWeek();
    Month month = firstDay.getMonth();

    if (DayOfWeek.SATURDAY.equals(dow)) {
        return Month.FEBRUARY.equals(month) ? 5 : 6;
     }

    if (DayOfWeek.FRIDAY.equals(dow)) {
        return firstDay.lengthOfMonth() == 31 ? 6 : 5;
    }

    if (DayOfWeek.SUNDAY.equals(dow) && Month.FEBRUARY.equals(month)) {
        return firstDay.isLeapYear() ? 5 :4;
    }

    return 5;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)