package com.b2bwings.cqjy.common.util;
import org.apache.commons.lang.time.DateFormatUtils;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class WorkDayCalculateUtils {
/**
* 特殊的工作日(星期六、日工作)
*/
public static List<String> SPECIAL_WORK_DAYS = new ArrayList<>();
/**
* 特殊的休息日(星期一到五休息)
*/
public static List<String> SPECIAL_REST_DAYS = new ArrayList<>();
/**
* 休息日
*/
public static List<String> REST_DAYS = new ArrayList<>();
/**
*
* @param currentDate 当前时间
* @param days 跳过多少工作日
* @param includingToday 是否包含今天 0否 1是
* @return
*/
public static Date getDate(Date currentDate, int days, int includingToday) {
if (days == 0) {
return currentDate;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(currentDate);
int step = days < 0 ? -1 : 1;
int i = includingToday;
int daysAbs = Math.abs(days);
while (i <= daysAbs) {
calendar.add(Calendar.DATE, step);
i++;
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
|| calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
// 周六日如果上班就算1个工作日
if (!SPECIAL_WORK_DAYS.contains(DateFormatUtils.format(calendar.getTime(), "yyyy-MM-dd"))) {
i--;
}
} else {
// 周1到周五休息就算1个休息日
if (SPECIAL_REST_DAYS.contains(DateFormatUtils.format(calendar.getTime(), "yyyy-MM-dd"))) {
i--;
}
}
}
return calendar.getTime();
}
}
java 计算工作日工具类
最新推荐文章于 2024-06-25 11:17:07 发布