findWeekSaturdayAndSundayWithCurrentMonth獲取假日

 public static List<LocalDateTime> findWeekSaturdayAndSundayWithCurrentMonth() {
        List<LocalDateTime> localDateTimeList = new ArrayList<>();
        //Sunday(星期天)、Monday(星期一)、Tuesday(星期二)、Wednesday(星期三)、Thursday(星期四)、Friday(星期五)、Saturday(星期六)
        LocalDateTime start = LocalDateTime.of(2020, 2, 1, 0, 0, 0);
        LocalDateTime newStart = start;
        LocalDateTime end = start.with(TemporalAdjusters.lastDayOfMonth());
        while (newStart.isBefore(end)) {
            //相差天數
            int daysNum = Period.between(newStart.toLocalDate(), end.toLocalDate()).getDays();
            //當前星期【1-7】
            int weekNum = newStart.getDayOfWeek().getValue();
            System.out.println("-----------------------"+weekNum);
            //日期推進【增加直到月底】邏輯
            if(weekNum == 7){
                System.out.println("A"+newStart);
                newStart = newStart.plusDays(7);
            }else if(weekNum == 6){
                System.out.println("B"+newStart+"   "+newStart.plusDays(1));
                newStart = newStart.plusDays(8);
            }else{
                System.out.println("C"+newStart.plusDays(7-weekNum)+"  "+newStart.minusDays(6-weekNum));
                newStart = newStart.plusDays(14-weekNum);
            }
        }
        return localDateTimeList;
    }
public static List<LocalDateTime> findWeekSaturdayAndSundayWithCurrentMonth(LocalDateTime inldt) {
        List<LocalDateTime> localDateTimeList = new ArrayList<>();
        //Sunday(星期天)、Monday(星期一)、Tuesday(星期二)、Wednesday(星期三)、Thursday(星期四)、Friday(星期五)、Saturday(星期六)
        LocalDateTime start = LocalDateTimeUtil.getFirstDayOfMonth(inldt);
        LocalDateTime newStart = start;
        LocalDateTime end = LocalDateTimeUtil.getLastDayOfMonth(start);
        while (newStart.isBefore(end) || newStart.isEqual(end)) {
            //當前星期【1-7】
            int weekNum = newStart.getDayOfWeek().getValue();
            //日期推進【增加直到月底】邏輯
            if (weekNum == 7) {
                localDateTimeList.add(newStart);
                newStart = newStart.plusDays(6);
            } else if (weekNum == 6) {
                localDateTimeList.add(newStart);
                newStart = newStart.plusDays(1);
            } else {
                newStart = newStart.plusDays(6 - weekNum);
            }
        }
        return localDateTimeList;
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章