java使用基础代码计算两个日期之间的工作日
时间: 2023-05-17 17:03:17 浏览: 85
可以使用Java 8中的java.time包来计算两个日期之间的工作日。以下是一个示例代码:
```java
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class WorkingDaysCalculator {
public static long getWorkingDays(LocalDate startDate, LocalDate endDate) {
long workingDays = 0;
LocalDate date = startDate;
while (date.isBefore(endDate)) {
if (date.getDayOfWeek() != DayOfWeek.SATURDAY && date.getDayOfWeek() != DayOfWeek.SUNDAY) {
workingDays++;
}
date = date.plus(1, ChronoUnit.DAYS);
}
return workingDays;
}
public static void main(String[] args) {
LocalDate startDate = LocalDate.of(2021, 1, 1);
LocalDate endDate = LocalDate.of(2021, 1, 31);
long workingDays = getWorkingDays(startDate, endDate);
System.out.println("Working days between " + startDate + " and " + endDate + ": " + workingDays);
}
}
```
该代码将计算2021年1月1日至2021年1月31日之间的工作日数量,并输出结果。
阅读全文