需求:
自动设置截止日期为工作日15天,休息日和节假日顺延。
需要手动维护节假调休的工作日,以及非周末的节假日
例如:
5月1号,周四为节假日,则需要维护为节假日
5月4号,周天需要调休工作,则需要维护为调休工作日
1、定义表结构,维护节假日或工作日
CREATE TABLE `holiday_info` (
`id` int(11) DEFAULT NULL COMMENT '主键id',
`date_info` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '日期信息',
`date_type` int(1) DEFAULT NULL COMMENT '日期类型(0:节假日 1:工作日)'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2、编写程序,实现功能
- java实现,其中days为多少个工作日之后。
public class Test{
public String getDate(int days){
int flag = 0;
String startDate = DateUtil.parseDateToString(null, new Date());
// 查询休息日
Set<String> countRest = holidayInfoDao.getCount(startDate, 0);
// 查询工作日
Set<String> countWork = holidayInfoDao.getCount(startDate, 1);
while(flag <= days){
flag ++;
Date date = DateUtil.addDateByDays(new Date(),flag);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK);
// 周天和周六为休息日 周天为1,周六为7
if (w == 1 || w == 7){
// 判断周六和周天不是调休的工作日,则延后一天
if(!countWork.contains(DateUtil.parseDateToString(null, date))){
days ++;
}
}else{
// 判断周一和周五是节假日,则延后一天
if(countRest.contains(DateUtil.parseDateToString(null, date))){
days ++;
}
}
}
String result = DateUtil.parseDateToString(null,DateUtil.addDateByDays(new Date(),days));
System.out.println(result);
return result;
}
}
- DateUtil.java日期处理类
import com.ict.framework.common.utils.StringUtil;
import org.sqlite.date.DateFormatUtils;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class DateUtil {
//日期 转 str
public static String parseDateToString(String format, Date date) {
if (format == null) {
format = "yyyy-MM-dd";
}
return DateFormatUtils.format(date, format);
}
public static Date addDateByDays(Date date, int Days) {
Calendar calendar = new GregorianCalendar();
calendar.add(Calendar.DATE, Days);
date = calendar.getTime();
return date;
}
}
- mybatis查询sql
select date_info from holiday_info where date_type = #{type} and date_info > #{startDate}