获取今天为准的第n个工作日,n为任意不为0的整数

public static LocalDateTime getWorkDateTime(LocalDateTime dateTime, int days) {
        if (days > 0) {
            for (int i = 0; i < days; i++) {
                dateTime = (LocalDateTime) getNextWorkDay.adjustInto(dateTime);
            }
        } else if (days < 0) {
            for (int i = 0; i < (-days); i++) {
                dateTime = (LocalDateTime) getPreWorkDay.adjustInto(dateTime);
            }
        }
        return dateTime;
    }

    private final static TemporalAdjuster getNextWorkDay = TemporalAdjusters.ofDateAdjuster((d) -> {
        if (d.getDayOfWeek().equals(DayOfWeek.FRIDAY)) {
            return d.plusDays(3);
        } else if (d.getDayOfWeek().equals(DayOfWeek.SATURDAY)) {
            return d.plusDays(2);
        } else {
            return d.plusDays(1);
        }
    });

    private final static TemporalAdjuster getPreWorkDay = TemporalAdjusters.ofDateAdjuster((d) -> {
        if (d.getDayOfWeek().equals(DayOfWeek.MONDAY)) {
            return d.minusDays(3);
        } else if (d.getDayOfWeek().equals(DayOfWeek.SUNDAY)) {
            return d.minusDays(2);
        } else {
            return d.minusDays(1);
        }
    });
posted @ 2023-02-12 20:26  菜阿  阅读(15)  评论(0编辑  收藏  举报