给出周几和天数,写一段计算工作日天数的java程序
时间: 2023-05-27 17:07:18 浏览: 126
以下是一段计算工作日天数的Java程序,假设工作日是周一至周五,不包括法定节假日:
```java
import java.time.DayOfWeek;
import java.time.LocalDate;
public class WorkdayCalculator {
public static int calculateWorkdays(int dayOfWeek, int days) {
LocalDate startDate = LocalDate.now().plusDays(dayOfWeek - LocalDate.now().getDayOfWeek().getValue());
int workdays = 0;
for (int i = 0; i < days; i++) {
if (startDate.getDayOfWeek() != DayOfWeek.SATURDAY && startDate.getDayOfWeek() != DayOfWeek.SUNDAY) {
workdays++;
}
startDate = startDate.plusDays(1);
}
return workdays;
}
public static void main(String[] args) {
int dayOfWeek = 1; // 假设今天是周一
int days = 10; // 假设计算10天后工作日数量
int workdays = calculateWorkdays(dayOfWeek, days);
System.out.println("工作日数量:" + workdays);
}
}
```
运行结果:
```
工作日数量:8
```
说明:假设今天是周一,计算10天后的工作日数量,其中周六和周日不算工作日,因此工作日数量为8。
阅读全文