java判断节假日函数
时间: 2023-05-21 21:03:38 浏览: 585
以下是一个简单的 Java 判断节假日的函数:
```java
public static boolean isHoliday(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
// 判断是否为周末
if (week == 0 || week == 6) {
return true;
}
// 判断是否为法定节假日
if ((month == 1 && day == 1) // 元旦
|| (month == 2 && day <= 7 && week == 1) // 春节
|| (month == 4 && day == 4) // 清明节
|| (month == 5 && day == 1) // 劳动节
|| (month == 6 && day <= 7 && week == 1) // 端午节
|| (month == 9 && day <= 7 && week == 1) // 中秋节
|| (month == 10 && day <= 7 && week == 1) // 国庆节
) {
return true;
}
// 判断是否为调休工作日
if ((month == 1 && day == 2 && isHoliday(new Date(year - 1900, 0, 1))) // 元旦调休
|| (month == 2 && day >= 11 && day <= 17 && isHoliday(new Date(year - 1900, 1, 15))) // 春节调休
|| (month == 4 && day == 6 && isHoliday(new Date(year - 1900, 3, 4))) // 清明节调休
|| (month == 5 && day == 3 && isHoliday(new Date(year - 1900, 4, 1))) // 劳动节调休
|| (month == 6 && day == 8 && isHoliday(new Date(year - 1900, 5, 7))) // 端午节调休
|| (month == 9 && day == 13 && isHoliday(new Date(year - 1900, 8, 10))) // 中秋节调休
|| (month == 10 && day == 10 && isHoliday(new Date(year - 1900, 9, 1))) // 国庆节调休
) {
return false;
}
return false;
}
```
该函数可以判断指定日期是否为节假日,包括周末、法定节假日和调休工作日。
阅读全文