算法学习笔记
计算指定日期后多少天的工作日
使用一个循环不断将日期增加一天,并检查是否为周末(周六或周日),如果不是周末,则工作日计数器减一。当工作日计数器减为零时,返回计算得到的日期
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;
}

浙公网安备 33010602011771号