我有一个问题是要查找当前日期是否是该月的第一天,如果是,我必须做一些逻辑。该月的第一天应排除周末和节假日。从数据库中提取当年的假期列表
函数应接受今天的日期作为输入,如果当前日期是每月的第一天(不包括周末和节假日),则给出布尔值。
我尝试过这个逻辑:
boolean firstBusinessDayOfMonth(YearMonth month) {
final Set<DayOfWeek> businessDays = EnumSet.of(
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY
);
return
month.atDay(1).datesUntil(month.plusMonths(1).atDay(1))
.filter(date -> businessDays.contains(date.getDayOfWeek()))
.filter(date -> !isHoliday(date))
.findFirst();
//return should compare with current date and give true or false
}
一些例子:
2024 年 6 月
6 月 1 日(星期六)=> false
6 月 2 日(星期日)=> false
6 月 3 日(星期一和!假期)=> true
June4(星期二和!假期,但不是月初)=> false
鉴于您需要第一个日期,请将返回值从
boolean
更改为 o LocalDate
并添加 .orElseThrow()
,以确保如果不存在该日期也会出现异常。
/**
* @throws NoSuchElementException if there are no business days in the month
* @return First business day of the month
*/
LocalDate firstBusinessDayOfMonth(YearMonth month) {
EnumSet<DayOfWeek> businessDays = EnumSet.complementOf(EnumSet.of(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY));
return month.atDay(1).datesUntil(month.plusMonths(1).atDay(1))
.filter(date -> businessDays.contains(date.getDayOfWeek()))
.filter(date -> !isHoliday(date) )
.findFirst().orElseThrow();
}