LocalDate獲取距離當前日期三個月所有周末的資訊

墨客領域發表於2020-12-30

LocalDate獲取距離當前三個月所有周末的資訊

LocalDate方法介紹

LocalDate獲取距離當前三個月所有周末的資訊

public static void main(String[] args) {
    LocalDate date = LocalDate.now();
    LocalDate dateFirst = LocalDate.now();
    List<String> weekDay = new ArrayList<>();
    while (Period.between(dateFirst, date).getMonths() < 3){
        DayOfWeek week = date.getDayOfWeek();
        if(week == DayOfWeek.SATURDAY || week == DayOfWeek.SUNDAY) {
            weekDay.add(date.toString());
        }
        date = date.plusDays(1);
    }
    System.out.println(weekDay);
}

在這裡插入圖片描述

相關文章