java 自动设置结束时间为n个工作日后的日期

需求:
自动设置截止日期为工作日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} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑小枫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值