剔除工作日(工具类)
实际情况中,总是会遇到处理工作日的情况,例如办理车牌,一般7个工作日邮寄送达。
工作日顾名思义,就是工作的日子,而不是顺延。
将非工作日剔除,就是我写下这个工具类的缘由了。
这里因为每年的工作日与节假日,日期都不固定,所以需要每年进行手动维护一次。
不想手动维护代码,也可以将其放入数据库,将维护代码修改为维护数据库。
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import io.swagger.annotations.Api;
import org.apache.commons.lang.time.DateFormatUtils;
/**
* @ClassName WorkDayUtil
* @Author 青禾潋水
* @Description :工作日工具类——需每年维护一次时间
* @Version 1.0
*/
public class WorkDayUtil {
private WorkDayUtil(){}
private static String formateDate="yyyy-MM-dd HH:mm:ss";
/**
* 特殊的工作日(星期六、日工作)
*/
private static final List<String> SPECIAL_WORK_DAYS = new ArrayList<>();
/**
* 特殊的休息日(星期一到五休息)
*/
private static final List<String> SPECIAL_REST_DAYS = new ArrayList<>();
static {
initSpecialDays();
}
public static Date getDate(Date currentDate, int days) {
if (days == 0) {
return currentDate;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(currentDate);
int step = days < 0 ? -1 : 1;
int i = 0;
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(), formateDate))) {
i--;
}
} else {
// 周1到周五休息就算1个休息日
if (SPECIAL_REST_DAYS.contains(DateFormatUtils.format(calendar.getTime(), formateDate))) {
i--;
}
}
}
return calendar.getTime();
}
/**
* 手工维护特殊日(因为是未知的,所以必须手工维护)
*/
private static void initSpecialDays() {
//特殊工作日
SPECIAL_WORK_DAYS.add("2021-02-20 00:00:00");
SPECIAL_WORK_DAYS.add("2021-04-25 00:00:00");
SPECIAL_WORK_DAYS.add("2021-05-08 00:00:00");
SPECIAL_WORK_DAYS.add("2021-09-26 00:00:00");
SPECIAL_WORK_DAYS.add("2021-10-09 00:00:00");
//元旦
SPECIAL_REST_DAYS.add("2021-01-01 00:00:00");
SPECIAL_REST_DAYS.add("2021-01-02 00:00:00");
SPECIAL_REST_DAYS.add("2021-01-03 00:00:00");
SPECIAL_REST_DAYS.add("2021-01-24 00:00:00");
//春节
SPECIAL_REST_DAYS.add("2021-02-11 00:00:00");
SPECIAL_REST_DAYS.add("2021-02-12 00:00:00");
SPECIAL_REST_DAYS.add("2021-02-13 00:00:00");
SPECIAL_REST_DAYS.add("2021-02-14 00:00:00");
SPECIAL_REST_DAYS.add("2021-02-15 00:00:00");
SPECIAL_REST_DAYS.add("2021-02-16 00:00:00");
SPECIAL_REST_DAYS.add("2021-02-17 00:00:00");
//清明
SPECIAL_REST_DAYS.add("2021-04-03 00:00:00");
SPECIAL_REST_DAYS.add("2021-04-04 00:00:00");
SPECIAL_REST_DAYS.add("2021-04-05 00:00:00");
//劳动节
SPECIAL_REST_DAYS.add("2021-05-01 00:00:00");
SPECIAL_REST_DAYS.add("2021-05-02 00:00:00");
SPECIAL_REST_DAYS.add("2021-05-03 00:00:00");
SPECIAL_REST_DAYS.add("2021-05-04 00:00:00");
SPECIAL_REST_DAYS.add("2021-05-05 00:00:00");
//端午节
SPECIAL_REST_DAYS.add("2021-06-12 00:00:00");
SPECIAL_REST_DAYS.add("2021-06-13 00:00:00");
SPECIAL_REST_DAYS.add("2021-06-14 00:00:00");
//2021年国庆中秋
SPECIAL_REST_DAYS.add("2021-09-19 00:00:00");
SPECIAL_REST_DAYS.add("2021-09-20 00:00:00");
SPECIAL_REST_DAYS.add("2021-09-21 00:00:00");
SPECIAL_REST_DAYS.add("2021-10-01 00:00:00");
SPECIAL_REST_DAYS.add("2021-10-02 00:00:00");
SPECIAL_REST_DAYS.add("2021-10-03 00:00:00");
SPECIAL_REST_DAYS.add("2021-10-04 00:00:00");
SPECIAL_REST_DAYS.add("2021-10-05 00:00:00");
SPECIAL_REST_DAYS.add("2021-10-06 00:00:00");
SPECIAL_REST_DAYS.add("2021-10-07 00:00:00");
}
}
觉得实用,给个赞呗~