Java8求本月每周末的日期

public static void main(String[] args) {
        LocalDate today = LocalDate.now();
       //本月每周日的日期
        //本月的第一天
        LocalDate firstday = LocalDate.of(today.getYear(),today.getMonth(),1);
        //本月的最后一天
        LocalDate lastDay =today.with(TemporalAdjusters.lastDayOfMonth());
        //最后一天的那天是本周的第几周
        int weeks = lastDay.get(ChronoField.ALIGNED_WEEK_OF_MONTH);
        int i=1;
        //第一周的星期天
        LocalDate weekDate =firstday.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
        while (i<weeks){
            System.out.println("第"+i+"周的周末是:"+weekDate+" ");
            weekDate = weekDate.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
            i++;
        }
    }

 

posted @ 2022-11-21 19:56  锐洋智能  阅读(204)  评论(0编辑  收藏  举报