SpringBoot本地判定判定工作日节假日及工作时段解决方案
文章目录
- SpringBoot本地判定判定工作日节假日及工作时段解决方案
- 主要思路
- 建立数据库表,存储节假日及工作日
- 通过代码生成增删改查Java后端代码及前端代码
- 引入 hutool maven 依赖
- 编写工具类,进行判定
主要思路
正常来说周一到周五为工作日,但是由于国家法定节假日规定;
如周一到周五,可能为休息日;周末可能为工作日,
这个时候就需要额外进行判定。
so:
数据库存储需额外判定的日期:工作日和节假日。
查询数据库存储的日期,进行判定
也有很多付费的公网api可调用:
拓展坞节假日接口
万年历
建立数据库表,存储节假日及工作日
CREATE TABLE `tb_extra_day` (`id` bigint NOT NULL AUTO_INCREMENT,`day_specific` date DEFAULT NULL COMMENT '日期',`day_type` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '0' COMMENT '日期类型,1工作日,0节假日',`create_time` datetime DEFAULT NULL COMMENT '创建时间',`update_time` datetime DEFAULT NULL COMMENT '更新时间',`remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '备注',`del_flag` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '0' COMMENT '删除标志(0代表存在 2代表删除)',`create_by` bigint DEFAULT NULL COMMENT '创建者',`status` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '0' COMMENT '禁用启用状态',`update_by` bigint DEFAULT NULL COMMENT '更新者',PRIMARY KEY (`id`) USING BTREE,KEY `create_by_idx` (`create_by`) USING BTREE,KEY `update_by_idx` (`update_by`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=COMPACT COMMENT='额外工作节假日';
通过代码生成增删改查Java后端代码及前端代码
略
示例存储数据
-- ----------------------------
-- Records of tb_extra_day
-- ----------------------------
INSERT INTO `tb_extra_day` VALUES (2, '2024-02-04', '1', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (3, '2024-02-18', '1', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (4, '2024-04-07', '1', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (5, '2024-04-28', '1', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (6, '2024-05-11', '1', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (7, '2024-09-14', '1', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (8, '2024-09-29', '1', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (9, '2024-10-12', '1', '2023-11-16 10:56:28', '2023-11-30 17:04:45', NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (10, '2024-01-01', '0', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (11, '2024-02-12', '0', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (12, '2024-02-13', '0', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (13, '2024-02-14', '0', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (14, '2024-02-15', '0', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (15, '2024-02-16', '0', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (16, '2024-04-04', '0', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (17, '2024-04-05', '0', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (18, '2024-05-01', '0', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (19, '2024-05-02', '0', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (20, '2024-05-03', '0', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (21, '2024-06-10', '0', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (22, '2024-09-16', '0', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (23, '2024-09-17', '0', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (24, '2024-10-01', '0', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (25, '2024-10-02', '0', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (26, '2024-10-03', '0', '2023-11-16 10:56:28', '2023-11-23 10:13:24', NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (27, '2024-10-04', '0', '2023-11-16 10:56:28', NULL, NULL, '0', NULL, '0', NULL);
INSERT INTO `tb_extra_day` VALUES (28, '2024-10-07', '0', '2023-11-16 10:56:28', '2023-11-23 10:11:35', NULL, '0', NULL, '0', NULL);
引入 hutool maven 依赖
<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.22</version></dependency>
编写工具类,进行判定
工具类返回布尔值,业务根据返回值自行处理
自行查询数据库存储的数据
可根据年份查询指定数据,提供查询效率
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;/*** 功能描述: 判断工作日、工作时间工具类** @author : yzd e-mail: 121665820@qq.com* @create : 2023/11/10 10:12*/
@Component
public class HolidayUtil {/*** 上午 上班时间*/private static final String MORNING_WORK_TIME = "9:00:00";/*** 上午 下班时间*/private static final String MORNING_REST_TIME = "12:00:00";/*** 下午 上班时间*/private static final String AFTERNOON_WORK_TIME = "14:00:00";/*** 下午 下班时间*/private static final String AFTERNOON_REST_TIME = "18:00:00";/*** 额外节假日,需要手动录入当年放假调休的日期*/private static List<String> HOLIDAY = new ArrayList<>(100);/*** 额外加班日, 需要手动录入当年额外的上班日*/private static List<String> EXTRA_WORK_DAY = new ArrayList<>(100);/*** 额外日期查询服务*/private static IExtraDayService iExtraDayService;/*** 初始化*/@PostConstructprivate void initialize() {iExtraDayService = SpringUtil.getBean(IExtraDayService.class);}/*** 功能描述: 判断当前日期是否为工作日 工作时间段** @param date 日期* @return : java.lang.Boolean* @author : yzd e-mail: 121665820@qq.com* @create : 2023/11/10 9:26*/public static Boolean isWorkingDay(Date date) {initDay();// 初始值不是工作日String formatDate = DateUtil.formatDate(date);// 是否额外节假日if (HOLIDAY.contains(formatDate)) {return Boolean.FALSE;}// 不是 额外工作日 ; 是周末if (!EXTRA_WORK_DAY.contains(formatDate) && DateUtil.isWeekend(date)) {return Boolean.FALSE;}// 今日上午 上班时间段DateTime morningWorkTime = DateUtil.parseDateTime(DateUtil.today() + " " + MORNING_WORK_TIME);DateTime morningRestTime = DateUtil.parseDateTime(DateUtil.today() + " " + MORNING_REST_TIME);// 今日下午 上班时间段DateTime afternoonWorkTime = DateUtil.parseDateTime(DateUtil.today() + " " + AFTERNOON_WORK_TIME);DateTime afternoonRestTime = DateUtil.parseDateTime(DateUtil.today() + " " + AFTERNOON_REST_TIME);// 在上午 上班时间段if (date.after(morningWorkTime) && date.before(morningRestTime)) {return Boolean.TRUE;}if (DateUtil.isSameTime(date, morningWorkTime) || DateUtil.isSameTime(date, morningRestTime)) {return Boolean.TRUE;}// 在下午午 上班时间段if (date.after(afternoonWorkTime) && date.before(afternoonRestTime)) {return Boolean.TRUE;}if (DateUtil.isSameTime(date, afternoonWorkTime) || DateUtil.isSameTime(date, afternoonRestTime)) {return Boolean.TRUE;}return Boolean.FALSE;}public static void main(String[] args) {DateTime dateTime = DateUtil.parseDateTime(DateUtil.today() + " " + "9:00:00");Boolean workingDay = isWorkingDay(dateTime);if (workingDay) {System.out.println("工作日,加油,打工人");} else {System.out.println("开开心心过节,高高兴兴干饭!!!");}}/*** 初始话节假日,需要手动录入当年放假调休的上班时间 初始化额外加班日,,需要手动录入当年放假调休的休息时间**/public static void initDay() {// 查询数据库,自行初始化常量 TODO }}