算法学习笔记

计算指定日期后多少天的工作日

使用一个循环不断将日期增加一天,并检查是否为周末(周六或周日),如果不是周末,则工作日计数器减一。当工作日计数器减为零时,返回计算得到的日期

public static LocalDate calculateWorkdays(LocalDate startDate, int workdays) {

    LocalDate currentDate = startDate;
    int remainingWorkdays = workdays;

    while (remainingWorkdays > 0) {
        currentDate = currentDate.plusDays(1);
        DayOfWeek dayOfWeek = currentDate.getDayOfWeek();
        if (dayOfWeek != DayOfWeek.SATURDAY && dayOfWeek != DayOfWeek.SUNDAY) {
            remainingWorkdays--;
        }
    }

    return currentDate;
}
posted @ 2025-03-04 15:02  低吟不作语  阅读(19)  评论(0)    收藏  举报