这篇文章主要介绍了如何使用Java获取国家法定节假日,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。Java是什么Java是一门面向对象编程语言,可以编写桌面应用程序、WEB应
这篇文章主要介绍了如何使用Java获取国家法定节假日,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
Java是一门面向对象编程语言,可以编写桌面应用程序、WEB应用程序、分布式系统和嵌入式系统应用程序。
<!-- 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>
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.*; 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)); } 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"); //筛选掉补班 if (wage.equals(1)) { monthWekDay.remove(date); } else { monthWekDay.add(date); } } return monthWekDay; } 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); } 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; } }
以上方法可以拿来即用,当然也可以根据自己的需求自定义。
以下是我自己业务需求,将调用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; @Slf4j@Componentpublic 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(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("获取节假日数据内容为 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); } // 定义节假日集合 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); } // 根据日期排序升序 List<DailyHolidayConfig> 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())); }}
感谢你能够认真阅读完这篇文章,希望小编分享的“如何使用Java获取国家法定节假日”这篇文章对大家有帮助,同时也希望大家多多支持编程网,关注编程网精选频道,更多相关知识等着你来学习!
--结束END--
本文标题: 如何使用Java获取国家法定节假日
本文链接: https://lsjlt.com/news/274561.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0