教你怎么用Java獲取國(guó)家法定節(jié)假日
前言
此節(jié)假日為嚴(yán)格按照國(guó)家要求的雙休和法定節(jié)假日并且包含節(jié)假日的補(bǔ)班信息,大家可根據(jù)自己的需求自定義處理哦。
以下為Maven配置,是程序用到的依賴。版本的話,可以用最新的。
Maven配置
<!-- okhttp -->
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
Java程序
package com.uiotsoft.daily.task;
import com.alibaba.fastjson.JSONObject;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* <p>TestDate 此類用于:</p>
* <p>@author:hujm</p>
* <p>@date:2021年04月22日 17:43</p>
* <p>@remark:</p>
*/
public class TestDate {
public static void main(String[] args) {
System.out.println(getJjr(2021, 4));
System.out.println(getMonthWekDay(2021, 4));
System.out.println(JJR(2021, 4));
}
/**
* 獲取周末和節(jié)假日
*
* @param year
* @param month
* @return
*/
public static Set<String> JJR(int year, int month) {
//獲取所有的周末
Set<String> monthWekDay = getMonthWekDay(year, month);
//http://timor.tech/api/holiday api文檔地址
Map jjr = getJjr(year, month + 1);
Integer code = (Integer) jjr.get("code");
if (code != 0) {
return monthWekDay;
}
Map<String, Map<String, Object>> holiday = (Map<String, Map<String, Object>>) jjr.get("holiday");
Set<String> strings = holiday.keySet();
for (String str : strings) {
Map<String, Object> stringObjectMap = holiday.get(str);
Integer wage = (Integer) stringObjectMap.get("wage");
String date = (String) stringObjectMap.get("date");
//篩選掉補(bǔ)班
if (wage.equals(1)) {
monthWekDay.remove(date);
} else {
monthWekDay.add(date);
}
}
return monthWekDay;
}
/**
* 獲取節(jié)假日不含周末
*
* @param year
* @param month
* @return
*/
private static Map getJjr(int year, int month) {
String url = "http://timor.tech/api/holiday/year/";
OkHttpClient client = new OkHttpClient();
Response response;
//解密數(shù)據(jù)
String rsa = null;
Request request = new Request.Builder()
.url(url)
.get()
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.build();
try {
response = client.newCall(request).execute();
rsa = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return JSONObject.parseObject(rsa, Map.class);
}
/**
* 獲取周末 月從0開(kāi)始
*
* @param year
* @param mouth
* @return
*/
public static Set<String> getMonthWekDay(int year, int mouth) {
Set<String> dateList = new HashSet<>();
SimpleDateFormat simdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = new GregorianCalendar(year, mouth, 1);
Calendar endCalendar = new GregorianCalendar(year, mouth, 1);
endCalendar.add(Calendar.MONTH, 1);
while (true) {
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
if (weekday == 1 || weekday == 7) {
dateList.add(simdf.format(calendar.getTime()));
}
calendar.add(Calendar.DATE, 1);
if (calendar.getTimeInMillis() >= endCalendar.getTimeInMillis()) {
break;
}
}
return dateList;
}
}
以上方法可以拿來(lái)即用,當(dāng)然也可以根據(jù)自己的需求自定義。
以下是我自己業(yè)務(wù)需求,將調(diào)用API接口獲取的節(jié)假日信息保存到本地?cái)?shù)據(jù)庫(kù)中,如果不感興趣可以跳過(guò)以下內(nèi)容哦~~~~
package com.uiotsoft.daily.task;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.uiotsoft.daily.module.entity.DailyHolidayConfig;
import com.uiotsoft.daily.module.entity.HolidayRawInfo;
import com.uiotsoft.daily.module.service.DailyHolidayConfigService;
import com.uiotsoft.daily.module.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
/**
* <p>NoSubmitTask 此類用于:</p>
* <p>@author:hujm</p>
* <p>@date:2021年04月16日 17:10</p>
* <p>@remark:</p>
*/
@Slf4j
@Component
public class NoSubmitTask {
@Resource
private DailyHolidayConfigService holidayConfigService;
@Value("${syncAddress}")
private String syncAddress;
@Scheduled(cron = "${syncHolidayDeadline}")
public void syncHoliday() {
log.info("每年12月28凌晨1點(diǎn)定時(shí)同步下一年的節(jié)假日信息,同步節(jié)假日開(kāi)始時(shí)間 = {}", DateUtil.formatDateTime(new Date()));
String url = syncAddress;
OkHttpClient client = new OkHttpClient();
Response response;
//解密數(shù)據(jù)
String rsa = null;
Request request = new Request.Builder().url(url).get()
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.build();
try {
response = client.newCall(request).execute();
rsa = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
Map map = JSONObject.parseObject(rsa, Map.class);
if (map != null) {
Integer code = (Integer) map.get("code");
if (code == 0) {
JSONObject holidayJson = (JSONObject) map.get("holiday");
String jsonString = holidayJson.toJSONString();
log.info("獲取節(jié)假日數(shù)據(jù)內(nèi)容為 jsonString = 【{}】", jsonString);
Set<Map.Entry<String, Object>> entrySet = holidayJson.entrySet();
List<HolidayRawInfo> rawInfoList = new ArrayList<>();
for (Map.Entry<String, Object> entry : entrySet) {
String key = entry.getKey();
Object value = entry.getValue();
cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(value);
HolidayRawInfo holidayRawInfo = JSONUtil.toBean(jsonObject, HolidayRawInfo.class);
rawInfoList.add(holidayRawInfo);
}
// 定義節(jié)假日集合
List<DailyHolidayConfig> holidayConfigList = new ArrayList<>();
for (HolidayRawInfo holidayRawInfo : rawInfoList) {
DailyHolidayConfig holidayConfig = new DailyHolidayConfig();
holidayConfig.setHolidayTarget(holidayRawInfo.getTarget());
holidayConfig.setHolidayAfter(holidayRawInfo.getAfter());
holidayConfig.setHolidayDate(holidayRawInfo.getDate());
holidayConfig.setHolidayName(holidayRawInfo.getName());
holidayConfig.setHolidayRest(holidayRawInfo.getRest());
holidayConfig.setHolidayWage(holidayRawInfo.getWage());
holidayConfig.setCreateTime(new Date());
holidayConfigList.add(holidayConfig);
}
// 根據(jù)日期排序升序
List<DailyHolidayConfig> collect = holidayConfigList.stream().sorted(Comparator.comparing(DailyHolidayConfig::getHolidayDate)).collect(Collectors.toList());
// 批量插入節(jié)假日表中
holidayConfigService.insertBatch(collect);
} else {
log.error("E|NoSubmitTask|syncHoliday()|同步節(jié)假日信息時(shí),調(diào)用節(jié)假日網(wǎng)站服務(wù)出錯(cuò)!");
}
}
log.info("每年12月28凌晨1點(diǎn)定時(shí)同步下一年的節(jié)假日信息,同步節(jié)假日結(jié)束時(shí)間 = {}", DateUtil.formatDateTime(new Date()));
}
}
到此這篇關(guān)于教你怎么用Java獲取國(guó)家法定節(jié)假日的文章就介紹到這了,更多相關(guān)java獲取國(guó)家法定節(jié)假日內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)的順時(shí)針/逆時(shí)針打印矩陣操作示例
這篇文章主要介紹了java實(shí)現(xiàn)的順時(shí)針/逆時(shí)針打印矩陣操作,涉及java基于數(shù)組的矩陣存儲(chǔ)、遍歷、打印輸出等相關(guān)操作技巧,需要的朋友可以參考下2019-12-12
Java中split根據(jù)"."分割字符串問(wèn)題舉例
split表達(dá)式其實(shí)就是一個(gè)正則表達(dá)式,* | . ^ 等符號(hào)在正則表達(dá)式中屬于一種有特殊含義的字符,下面這篇文章主要給大家介紹了關(guān)于Java中split根據(jù)“.“分割字符串問(wèn)題的相關(guān)資料,需要的朋友可以參考下2022-10-10
Java語(yǔ)言實(shí)現(xiàn)基數(shù)排序代碼分享
這篇文章主要介紹了Java語(yǔ)言實(shí)現(xiàn)基數(shù)排序代碼分享,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
MyBatis批量插入的五種方式小結(jié)(MyBatis以集合方式批量新增)
本文主要介紹了MyBatis批量插入的五種方式小結(jié)(MyBatis以集合方式批量新增),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
解讀RabbitMQ和kafka的相同點(diǎn)和不同點(diǎn)是什么
RabbitMQ和Kafka都是消息中間件,支持分布式系統(tǒng)、高可用性和可靠性,RabbitMQ使用隊(duì)列模型,適合復(fù)雜路由場(chǎng)景;Kafka使用主題-分區(qū)模型,適合大規(guī)模數(shù)據(jù)流處理,RabbitMQ在低延遲方面表現(xiàn)更好,Kafka在高吞吐量方面表現(xiàn)更好2024-12-12
MyBatis框架關(guān)聯(lián)映射實(shí)例詳解
這篇文章主要介紹了MyBatis框架關(guān)聯(lián)映射,關(guān)系映射主要處理復(fù)雜的SQl查詢,如子查詢,多表聯(lián)查等復(fù)雜查詢,應(yīng)用此種需求時(shí)可以考慮使用,需要的朋友可以參考下2022-11-11

