教你怎么用Java获取国家法定节假日

网友投稿 1650 2022-10-27


教你怎么用Java获取国家法定节假日

前言

此节假日为严格按照国家要求的双休和法定节假日并且包含节假日的补班信息,大家可根据自己的需求自定义处理哦。

以下为Maven配置,是程序用到的依赖。版本的话,可以用最新的。

Maven配置

com.squareup.okhttp

okhttp

${okhttp.version}

com.alibaba

fastjson

${fastjson.version}

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.*;

/**

*

TestDate 此类用于:

*

@author:hujm

*

@date:2021年04月22日 17:43

*

@remark:

*/

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));

}

/**

* 获取周末和节假日

*

* @param year

* @param month

* @return

*/

public static Set JJR(int year, int month) {

//获取所有的周末

Set 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> holiday = (Map>) jjr.get("holiday");

Set strings = holiday.keySet();

for (String str : strings) {

Map stringObjectMap = holiday.get(str);

Integer wage = (Integer) stringObjectMap.get("wage");

String date = (String) stringObjectMap.get("date");

//筛选掉补班

if (wage.equals(1)) {

monthWekDay.remove(date);

} else {

monthWekDay.add(date);

}

}

return monthWekDay;

}

/**

* 获取节假日不含周末

*

* @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;

//解密数据

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开始

*

* @param year

* @param mouth

* @return

*/

public static Set getMonthWekDay(int year, int mouth) {

Set 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;

}

}

以上方法可以拿来即用,当然也可以根据自己的需求自定义。

以下是我自己业务需求,将调用API接口获取的节假日信息保存到本地数据库中,如果不感兴趣可以跳过以下内容哦~~~~

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;

/**

*

NoSubmitTask 此类用于:

*

@author:hujm

*

@date:2021年04月16日 17:10

*

@remark:

*/

@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点定时同步下一年的节假日信息,同步节假日开始时间 = {}", DateUtil.formatDateTime(new Date()));

String url = syncAddress;

OkHttpClient client = new OkHttpClient();

Response response;

//解密数据

String rsa = null;

Request request = new Request.Builder().url(url).get()

.addHeader("Content-Type", "application/x-www-form-urlencoded")

.build();

try {

response = client.newCall(rhttp://equest).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("获取节假日数据内容为 jsonString = 【{}】", jsonString);

Set> entrySet = holidayJson.entrySet();

List rawInfoList = new ArrayList<>();

for (Map.Entry 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);

}

// 定义节假日集合

List 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);

}

// 根据日期排序升序

List collect = holidayConfigList.stream().sorted(Comparator.comparing(DailyHolidayConfig::getHolidayDate)).collect(Collectors.toList());

// 批量插入节假日表中

holidayConfigService.insertBatch(collect);

} else {

log.error("E|NoSubmitTask|syncHoliday()|同步节假日信息时,调用节假日网站服务出错!");

}

}

log.info("每年12月28凌晨1点定时同步下一年的节假日信息,同步节假日结束时间 = {}", DateUtil.formatDateTime(new Date()));

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:硬件缓存策略
下一篇:ACL访问列表配置规则梳理
相关文章

 发表评论

暂时没有评论,来抢沙发吧~