使用java获取当前日期后几天内的工作日和节假日数
程序员文章站
2022-05-15 11:34:19
...
使用java获取当前日期后几天内的工作日和节假日数
public static String getWorkDayStart(Date startDay, int num) throws Exception {
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDay);
int startWeekDay = calendar.get(Calendar.DAY_OF_WEEK);
calendar.add(Calendar.DATE, num);
int endWeekDay = calendar.get(Calendar.DAY_OF_WEEK);
//节假日数
int holidayNum = 0;
//工作日数
int workdayNum = 0;
int number;
if(startWeekDay > endWeekDay){
number = endWeekDay - startWeekDay + 8;
}else{
number = endWeekDay - startWeekDay + 1;
}
if(number != 0){
for(int i = 0;i < (num < 7?num+1:number);i++){
if(calendar.get(Calendar.DAY_OF_WEEK) == 7 || calendar.get(Calendar.DAY_OF_WEEK) == 1){
holidayNum++;
}else {
workdayNum++;
}
calendar.add(Calendar.DATE, -1);
}
}
if(num >= 7){
holidayNum = holidayNum + 2 * (num - number + 1) / 7;
workdayNum = workdayNum + 5 * (num - number + 1) / 7;
}
return holidayNum + "," + workdayNum;
}
上一篇: 潘石屹:富士康员工生产苹果手机没有成就感 不如我做个木碗
下一篇: 在NPM发布自己造的*的方法步骤