提示:获取指定年的所有节假日信息
前言
每次找节假日很痛苦,这次整理了一下,在此也分享给各位,看下面demo,好用就点个赞,增加一下小编的动力(看看有没有帮助到人)。
一、获取节假日地址,自己换年份。
网站(基本每年11月更新明年的节假日):http://timor.tech/api/holiday/
接口:http://timor.tech/api/holiday/year/2024?type=Y&week=Y。
二、使用步骤
1.Demo示例
代码如下(示例):
public static void main(String[] args) {
try {
String data = HttpClientUtil.httpGetRequest("http://timor.tech/api/holiday/year/2024?type=Y&week=Y");
JSONObject jsonObject = JSONObject.parseObject(data).getJSONObject("type");
//懒的弄HttpClientUtil的,直接掉接口:http://timor.tech/api/holiday/year/2024?type=Y&week=Y[直接从这级,复制字符串,自己转一下json,不知道他的接口为啥给了两个格式不一样的两遍节假日](https://img-blog.csdnimg.cn/direct/5e0e83a4d0bf49abb7c7ef86cdc7ef7b.png)
//String a="{\"2024-01-01\":{\"type\":2,\"name\":\"元旦\",\"week\":1},\"2024-01-06\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-01-07\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-01-13\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-01-14\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-01-20\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-01-21\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-01-27\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-01-28\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-02-03\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-02-04\":{\"type\":3,\"name\":\"春节前补班\",\"week\":7},\"2024-02-10\":{\"type\":2,\"name\":\"初一\",\"week\":6},\"2024-02-11\":{\"type\":2,\"name\":\"初二\",\"week\":7},\"2024-02-12\":{\"type\":2,\"name\":\"初三\",\"week\":1},\"2024-02-13\":{\"type\":2,\"name\":\"初四\",\"week\":2},\"2024-02-14\":{\"type\":2,\"name\":\"初五\",\"week\":3},\"2024-02-15\":{\"type\":2,\"name\":\"初六\",\"week\":4},\"2024-02-16\":{\"type\":2,\"name\":\"初七\",\"week\":5},\"2024-02-17\":{\"type\":2,\"name\":\"初八\",\"week\":6},\"2024-02-18\":{\"type\":3,\"name\":\"春节后补班\",\"week\":7},\"2024-02-24\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-02-25\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-03-02\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-03-03\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-03-09\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-03-10\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-03-16\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-03-17\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-03-23\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-03-24\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-03-30\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-03-31\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-04-04\":{\"type\":2,\"name\":\"清明节\",\"week\":4},\"2024-04-05\":{\"type\":2,\"name\":\"清明节\",\"week\":5},\"2024-04-06\":{\"type\":2,\"name\":\"清明节\",\"week\":6},\"2024-04-07\":{\"type\":3,\"name\":\"清明节后补班\",\"week\":7},\"2024-04-13\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-04-14\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-04-20\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-04-21\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-04-27\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-04-28\":{\"type\":3,\"name\":\"劳动节前补班\",\"week\":7},\"2024-05-01\":{\"type\":2,\"name\":\"劳动节\",\"week\":3},\"2024-05-02\":{\"type\":2,\"name\":\"劳动节\",\"week\":4},\"2024-05-03\":{\"type\":2,\"name\":\"劳动节\",\"week\":5},\"2024-05-04\":{\"type\":2,\"name\":\"劳动节\",\"week\":6},\"2024-05-05\":{\"type\":2,\"name\":\"劳动节\",\"week\":7},\"2024-05-11\":{\"type\":3,\"name\":\"劳动节后补班\",\"week\":6},\"2024-05-12\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-05-18\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-05-19\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-05-25\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-05-26\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-06-01\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-06-02\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-06-08\":{\"type\":2,\"name\":\"端午节\",\"week\":6},\"2024-06-09\":{\"type\":2,\"name\":\"端午节\",\"week\":7},\"2024-06-10\":{\"type\":2,\"name\":\"端午节\",\"week\":1},\"2024-06-15\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-06-16\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-06-22\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-06-23\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-06-29\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-06-30\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-07-06\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-07-07\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-07-13\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-07-14\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-07-20\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-07-21\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-07-27\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-07-28\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-08-03\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-08-04\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-08-10\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-08-11\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-08-17\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-08-18\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-08-24\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-08-25\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-08-31\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-09-01\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-09-07\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-09-08\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-09-14\":{\"type\":3,\"name\":\"中秋节前补班\",\"week\":6},\"2024-09-15\":{\"type\":2,\"name\":\"中秋节\",\"week\":7},\"2024-09-16\":{\"type\":2,\"name\":\"中秋节\",\"week\":1},\"2024-09-17\":{\"type\":2,\"name\":\"中秋节\",\"week\":2},\"2024-09-21\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-09-22\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-09-28\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-09-29\":{\"type\":3,\"name\":\"国庆节前补班\",\"week\":7},\"2024-10-01\":{\"type\":2,\"name\":\"国庆节\",\"week\":2},\"2024-10-02\":{\"type\":2,\"name\":\"国庆节\",\"week\":3},\"2024-10-03\":{\"type\":2,\"name\":\"国庆节\",\"week\":4},\"2024-10-04\":{\"type\":2,\"name\":\"国庆节\",\"week\":5},\"2024-10-05\":{\"type\":2,\"name\":\"国庆节\",\"week\":6},\"2024-10-06\":{\"type\":2,\"name\":\"国庆节\",\"week\":7},\"2024-10-07\":{\"type\":2,\"name\":\"国庆节\",\"week\":1},\"2024-10-12\":{\"type\":3,\"name\":\"国庆节后补班\",\"week\":6},\"2024-10-13\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-10-19\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-10-20\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-10-26\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-10-27\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-11-02\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-11-03\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-11-09\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-11-10\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-11-16\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-11-17\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-11-23\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-11-24\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-11-30\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-12-01\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-12-07\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-12-08\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-12-14\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-12-15\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-12-21\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-12-22\":{\"type\":1,\"name\":\"周日\",\"week\":7},\"2024-12-28\":{\"type\":1,\"name\":\"周六\",\"week\":6},\"2024-12-29\":{\"type\":1,\"name\":\"周日\",\"week\":7}}";
Set<String> keySet = jsonObject.keySet();
for (String key : keySet) {
JSONObject jsonObjectData = jsonObject.getJSONObject(key);
//目前这个网站的类型3是个补班类型,不知道为啥算到节假日里面了,我这里给排掉了
if(!jsonObjectData.getString("type").equals("3")){
//打印了所有该年度的节假日时间的插入语句
System.out.println("INSERT INTO `sys_holiday` (`date`, `name`, `type`, `week`) VALUES ('"+key+" 00:00:00', '"+jsonObjectData.getString("name")+"', '"+jsonObjectData.getString("type")+"', '"+jsonObjectData.getString("week")+"');");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
2.表结构
表结构(示例):
CREATE TABLE `sys_holiday` (
`id` bigint(20) 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,
INDEX `date`(`date`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1246 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '节假日表' ROW_FORMAT = DYNAMIC;
---
# 总结
就到这了,自行扩展吧。