使用java 排除 两个日期段中的 休息日和节假日 。在线等待中十万火急,求大牛帮忙,多谢了。。。。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用java 排除 两个日期段中的 休息日和节假日 。在线等待中十万火急,求大牛帮忙,多谢了。。。。相关的知识,希望对你有一定的参考价值。
列入:排除掉2016-01-01 至 2016-05-01 期间的 休息日(星期6和星期天)和节假日,在线急等~最好求实现的代码~~~
时间处理,更方便和更易读的代码角度讲,肯定要用Java8的新date api啦,由于涉及到一系列日期,所以结合Java8的Stream也是理所当然
总体思路:
由于节假日每年在变,所以需要罗列出所有的节假日集合A
生成2016-01-01 至 2016-05-01所有的日期,生成日期集合B
从B中过滤掉A中的节假日
从B中过滤掉周六周日
最后把B中集合打印
结合思路,所见即所得的代码如下:
// 所有节假日的日期集合,这里你可以自己添加,只写了两个仅供参考(完成思路1)List<LocalDate> holidays = Arrays.asList(LocalDate.parse("2016-01-01"), LocalDate.parse("2016-05-01"));
// 按照起始2016-01-01,每次递增一天的方式生成一个Stream
Stream.iterate(LocalDate.parse("2016-01-01"), localDate -> localDate.plusDays(1))
// 按照要求的时间间隔2016-01-01 至 2016-05-01中的实际间隔天数截断Stream(完成思路2)
.limit(ChronoUnit.DAYS.between(LocalDate.parse("2016-01-01"), LocalDate.parse("2016-05-01")))
// 过滤其中的节假日(完成思路3)
.filter(localDate -> !holidays.contains(localDate))
// 过滤其中的周六
.filter(localDate -> !DayOfWeek.SATURDAY.equals(DayOfWeek.of(localDate.get(ChronoField.DAY_OF_WEEK))))
// 过滤其中的周日(完成思路4)
.filter(localDate -> !DayOfWeek.SUNDAY.equals(DayOfWeek.of(localDate.get(ChronoField.DAY_OF_WEEK))))
// 打印最后结果(完成思路5)
.forEach(System.out::println);
打印的结果:
综上:结合新时间API的易用性+Stream处理集合的快捷性,写出代码还是很简洁的
追问多谢热心网友解答,3Q, 我利用第三方接口,和自己利用java的Calendar日历类解决了
调用这个接口:
http://apistore.baidu.com/apiworks/servicedetail/1116.html?qq-pf-to=pcqq.group
它根据传入指定的参数,返回处理结果
检查具体日期是否为节假日,工作日对应结果为 0, 休息日对应结果为 1, 节假日对应的结果为
嗯,没事,这个看你自己选择了,都可以的...你想要的目的不一样,那有多重选择的时候,你肯定应该选最能满足你的目的,同时工作量小的,明显你是希望能立马使用又不需要自己去把节假日罗列出来这么繁琐
参考技术A 不可能的。除非你自己把所有节假日列出来。 参考技术B 运用日历类MySQL排除节假日,计算日期差
计算两个日期的差,并且需要排除节假日。需要两张表,一张是主表,需要计算日期差的表;一张是节假日表,存放法定节假日和周末的表。
主表
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
`id` bigint NOT NULL AUTO_INCREMENT,
`start_time` datetime NULL DEFAULT NULL,
`end_time` datetime NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
INSERT INTO `test` VALUES (1, '2021-12-01 12:50:27', '2021-12-04 12:53:38');
INSERT INTO `test` VALUES (2, '2021-12-03 08:22:11', '2021-12-06 17:54:19');
INSERT INTO `test` VALUES (3, '2022-01-01 09:54:28', '2022-01-02 19:50:31');
INSERT INTO `test` VALUES (4, '2021-12-10 13:53:34', '2021-12-25 23:50:55');
INSERT INTO `test` VALUES (5, '2022-01-10 07:20:06', '2022-01-11 22:55:13');
INSERT INTO `test` VALUES (6, '2021-12-26 16:10:17', '2021-12-28 16:55:25');
INSERT INTO `test` VALUES (7, '2021-12-27 20:55:35', '2022-01-02 17:35:43');
INSERT INTO `test` VALUES (8, '2022-01-12 21:35:49', '2022-01-16 14:22:54');
节假日表(有21年和22年的节假日)
DROP TABLE IF EXISTS `sys_holiday`;
CREATE TABLE `sys_holiday` (
`id` bigint NOT NULL AUTO_INCREMENT,
`date` datetime NULL DEFAULT NULL COMMENT '日期',
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '节日名称',
`type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '节假日类型(0 工作日、1 周末、2 节日、3 调休)',
`week` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '1 - 7,分别表示 周一 至 周日',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '节假日表' ROW_FORMAT = DYNAMIC;
INSERT INTO `sys_holiday` VALUES (1, '2022-06-04 00:00:00', '端午节', '2', '6');
INSERT INTO `sys_holiday` VALUES (2, '2022-08-06 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (3, '2022-02-20 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (4, '2021-10-03 00:00:00', '国庆节', '2', '7');
INSERT INTO `sys_holiday` VALUES (5, '2021-09-20 00:00:00', '中秋节', '2', '1');
INSERT INTO `sys_holiday` VALUES (6, '2022-12-10 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (7, '2021-06-13 00:00:00', '端午节', '2', '7');
INSERT INTO `sys_holiday` VALUES (8, '2022-06-25 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (9, '2022-01-08 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (10, '2021-10-01 00:00:00', '国庆节', '2', '5');
INSERT INTO `sys_holiday` VALUES (11, '2022-06-18 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (12, '2022-12-04 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (13, '2022-05-28 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (14, '2021-05-16 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (15, '2021-10-02 00:00:00', '国庆节', '2', '6');
INSERT INTO `sys_holiday` VALUES (16, '2022-04-04 00:00:00', '清明节', '2', '1');
INSERT INTO `sys_holiday` VALUES (17, '2022-05-08 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (18, '2022-09-24 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (19, '2022-01-03 00:00:00', '元旦', '2', '1');
INSERT INTO `sys_holiday` VALUES (20, '2021-03-13 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (21, '2021-04-04 00:00:00', '清明节', '2', '7');
INSERT INTO `sys_holiday` VALUES (22, '2021-01-10 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (23, '2021-04-03 00:00:00', '清明节', '2', '6');
INSERT INTO `sys_holiday` VALUES (24, '2022-10-03 00:00:00', '国庆节', '2', '1');
INSERT INTO `sys_holiday` VALUES (25, '2021-01-24 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (26, '2021-02-16 00:00:00', '初五', '2', '2');
INSERT INTO `sys_holiday` VALUES (27, '2022-03-13 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (28, '2021-05-30 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (29, '2022-11-26 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (30, '2021-01-02 00:00:00', '元旦', '2', '6');
INSERT INTO `sys_holiday` VALUES (31, '2022-05-03 00:00:00', '劳动节', '2', '2');
INSERT INTO `sys_holiday` VALUES (32, '2022-05-15 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (33, '2021-02-28 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (34, '2021-08-15 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (35, '2021-04-11 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (36, '2021-07-11 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (37, '2021-01-03 00:00:00', '元旦', '2', '7');
INSERT INTO `sys_holiday` VALUES (38, '2021-08-29 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (39, '2022-05-21 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (40, '2022-11-20 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (41, '2021-03-27 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (42, '2022-01-31 00:00:00', '除夕', '2', '1');
INSERT INTO `sys_holiday` VALUES (43, '2022-07-09 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (44, '2022-02-26 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (45, '2022-11-12 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (46, '2022-06-11 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (47, '2022-12-18 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (48, '2022-09-17 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (49, '2022-08-07 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (50, '2021-08-21 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (51, '2021-06-12 00:00:00', '端午节', '2', '6');
INSERT INTO `sys_holiday` VALUES (52, '2021-01-23 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (53, '2022-04-09 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (54, '2021-11-14 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (55, '2021-09-19 00:00:00', '中秋节', '2', '7');
INSERT INTO `sys_holiday` VALUES (56, '2021-09-05 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (57, '2022-07-16 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (58, '2022-03-05 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (59, '2022-06-12 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (60, '2022-02-19 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (61, '2021-02-14 00:00:00', '初三', '2', '7');
INSERT INTO `sys_holiday` VALUES (62, '2021-01-09 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (63, '2022-10-06 00:00:00', '国庆节', '2', '4');
INSERT INTO `sys_holiday` VALUES (64, '2022-07-10 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (65, '2021-12-12 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (66, '2022-03-06 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (67, '2022-04-16 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (68, '2021-10-24 00:00:00', '周日', '1', '7');
INSERT INTO `sys_holiday` VALUES (69, '2021-10-16 00:00:00', '周六', '1', '6');
INSERT INTO `sys_holiday` VALUES (70, '2022-06-03 00:00:00', '端午节', '2', '5');
INSERT INTO `sys_holiday` VALUES (71, '2022-10-02 00:00:00', '国庆节', '2', '7');
INSERT INTO `sys_holiday` VALUES (72, '2021-10-06 00:00:00', '国庆节', '2', '以上是关于使用java 排除 两个日期段中的 休息日和节假日 。在线等待中十万火急,求大牛帮忙,多谢了。。。。的主要内容,如果未能解决你的问题,请参考以下文章