百度的接口没有更新2021年节假日 ,文章下方再补一个第三方接口
百度日历Json接口
只能解析当年及以前年份的节假日,因为下一年的节假日需要等国务院发布节假日,想要实时获取,可以自定义定时任务每年的第一天去查询百度的接口https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=2020&resource_id=6018&format=json
数据中的状态是2 做了下判断 只有1的状态是节假日

根据年份解析百度接口的节假日,然后封装到集合中
public static Map<Date, DayStatusEnum> holidayByList(String year){
String httpUrl = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query="+year+"&resource_id=6018&format=json";
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
Map<Date,DayStatusEnum> dateMap = new HashMap<Date,DayStatusEnum>();
try {
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close

本文介绍了如何使用Java解析百度日历API获取当年及以前的节假日信息,由于百度接口不更新未来年份数据,建议通过定时任务每年初查询。文章提供了数据状态判断和节假日枚举,并使用DateUtils工具类进行日期转换。由于接口稳定性问题,推荐将数据存储到数据库中。此外,还提及了一个第三方节假日接口供参考。
最低0.47元/天 解锁文章
1412





