java 取下个工作日
时间: 2024-08-13 15:03:25 浏览: 144
在Java中获取下一个工作日,你可以编写一个自定义函数,利用Java 8引入的日期时间API,结合WeekFields和DayOfWeek枚举来处理周末的情况。这里是一个简单的示例:
```java
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
public class NextWorkingDayCalculator {
public static LocalDate getNextWorkingDay(LocalDate currentDate, boolean isWeekendFlagged) {
// 设置工作日规则,比如中国的工作日是从周一到周五
WeekFields weekFields = WeekFields.of(Locale.getDefault());
DayOfWeek workingDays[] = {DayOfWeek.MONDAY, DayOfWeek.TUESDAY, DayOfWeek.WEDNESDAY, DayOfWeek.THURSDAY, DayOfWeek.FRIDAY};
// 如果默认是周末,则排除周六和周日
DayOfWeek nextDay = isWeekendFlagged ? weekFields.nextDay(currentDate.getDayOfWeek()) : workingDays[currentDate.getDayOfWeek()];
// 通过TemporalAdjusters.withDayOfWeek返回指定工作日之后的第一个日期
return currentDate.plus(1, ChronoUnit.DAYS).with(TemporalAdjusters.lastOrSame(nextDay));
}
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
System.out.println("Next working day: " + getNextWorkingDay(currentDate, true)); // 假设默认周末标记为true(包括周六)
}
}
```
这个函数首先确定当前的工作日,然后移动一天并调整到下一个工作日。如果`isWeekendFlagged`为真,则跳过周末(周六和周日)。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![java](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)