java获取法定节假日

状态1:法定节假日
状态2:周末,但是因为调休而变更为工作日

如果只是想获取法定节日,可以用这个接口轻松获取2050年之前的信息
https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=%E6%B3%95%E5%AE%9A%E8%8A%82%E5%81%87%E6%97%A5&co=&resource_id=39042&t=1617089118959&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=wisetpl&cb=jQuery110203576901702188473_1617089118772&_=1617089118773
在这里插入图片描述
很简单,就不上代码了。但是这里只能获取法定节,无法获取法定休息日与调休。

法定休息日与调休一般是国务院在年末公布下一年的安排,所以下面的代码最多也只能获取下一年之前的信息

使用百度日历的接口

在这里插入图片描述
查看工作台可以发现数据是从这个接口中获取的
在这里插入图片描述
在这里插入图片描述
比较可疑的是status这个参数,继续查看百度搜索页面源代码,大概在6000行左右可以发现处理日历的js源代码
在这里插入图片描述
格式化一下
在这里插入图片描述
很明显,status为1时为节假日,为2时就是改为工作日的休息日。通过观察得知这里面的参数与实际值是差一天的。剩下的代码就很容易了。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.alibaba.fastjson.JSONObject;
 
public class HttpUtil2 {
	
	public static String sendGet(String url, String param) {
		String result = "";
		BufferedReader in = null;
		try {
			String urlNameString = url + "/" + param;
			URL realUrl = new URL(urlNameString);
			URLConnection connection = realUrl.openConnection();
			connection.setRequestProperty("accept", "*/*");
			connection.setRequestProperty("connection", "Keep-Alive");
			connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			connection.connect();
			in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String line;
			int cot = 0;
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
			System.out.println("发送GET请求出现异常!" + e);
			e.printStackTrace();
		}
		finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return result;
	}
 
	static Map totalMap = new TreeMap();
	
	public static void main(String[] args) {
		
		int startYear = 2019;//开始的年份
		int year = 3;		 //统计的年份
		
		for(int i = 0; i <= (year - 1); i++){
			for(int j = 1; j <= 12; j++) {
				loop(startYear + i + "", j + "");
			}
		}
		
		Iterator it = totalMap.keySet().iterator();
		while(it.hasNext()) {
			String temp = (String) it.next();
			System.out.println(temp + "  " + totalMap.get(temp));
		}
		
	}
	
	public static void loop(String year, String month) {
		String s = HttpUtil2.sendGet("https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query="+year+"%E5%B9%B4"+month+"%E6%9C%88&co=&resource_id=39043&t=1617089428269&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=wisetpl&cb=jQuery110203576901702188473_1617089118772&_=1617089118776", "");
		
		s = s.substring(s.indexOf("("));
		s = s.substring(1, s.length() - 2);
		Map<String, Object> map = (Map<String, Object>) JSONObject.parse(s);
		
		List list = (List) map.get("data");
		Map data = (Map) list.get(0);
		List<Map> almanac = (List<Map>) data.get("almanac");
		if(almanac == null || almanac.size() == 0) {
			return;
		}
		
		for(int i = 1; i < almanac.size(); i++) {
			String key = ((String)almanac.get(i).get("oDate")).substring(0, 10);
			String status = (String)almanac.get(i - 1).get("status");
			if("1".equals(status) || "2".equals(status)) {
				totalMap.put(key, status);
			}
		}
		
	}
}

返回示例

2018-12-29  2
2018-12-30  1
2018-12-31  1
2019-01-01  1
2019-02-02  2
2019-02-03  2
2019-02-04  1
2019-02-05  1
2019-02-06  1
2019-02-07  1
2019-02-08  1
2019-02-09  1
2019-02-10  1
2019-04-05  1
2019-04-06  1
2019-04-07  1
2019-04-28  2
2019-05-01  1
2019-05-02  1
2019-05-03  1
2019-05-04  1
2019-05-05  2
2019-06-07  1
2019-06-08  1
2019-06-09  1
2019-09-13  1
2019-09-14  1
2019-09-15  1
2019-09-29  2
2019-10-01  1
2019-10-02  1
2019-10-03  1
2019-10-04  1
2019-10-05  1
2019-10-06  1
2019-10-07  1
2019-10-12  2
2020-01-01  1
2020-01-19  2
2020-01-24  1
2020-01-25  1
2020-01-26  1
2020-01-27  1
2020-01-28  1
2020-01-29  1
2020-01-30  1
2020-01-31  1
2020-02-01  1
2020-02-02  1
2020-04-04  1
2020-04-05  1
2020-04-06  1
2020-04-26  2
2020-05-01  1
2020-05-02  1
2020-05-03  1
2020-05-04  1
2020-05-05  1
2020-05-09  2
2020-06-25  1
2020-06-26  1
2020-06-27  1
2020-06-28  2
2020-09-27  2
2020-10-01  1
2020-10-02  1
2020-10-03  1
2020-10-04  1
2020-10-05  1
2020-10-06  1
2020-10-07  1
2020-10-08  1
2020-10-10  2
2021-01-01  1
2021-01-02  1
2021-01-03  1
2021-02-07  2
2021-02-11  1
2021-02-12  1
2021-02-13  1
2021-02-14  1
2021-02-15  1
2021-02-16  1
2021-02-17  1
2021-02-20  2
2021-04-03  1
2021-04-04  1
2021-04-05  1
2021-04-25  2
2021-05-01  1
2021-05-02  1
2021-05-03  1
2021-05-04  1
2021-05-05  1
2021-05-08  2
2021-06-12  1
2021-06-13  1
2021-06-14  1
2021-09-18  2
2021-09-19  1
2021-09-20  1
2021-09-21  1
2021-09-26  2
2021-10-01  1
2021-10-02  1
2021-10-03  1
2021-10-04  1
2021-10-05  1
2021-10-06  1
2021-10-07  1
2021-10-09  2

使用的是百度的开放接口。与百度搜索日历后的页面数据一致。已作废

该方法获取2021年之后的数据为空,已作废

在这里插入图片描述

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.alibaba.fastjson.JSONObject;
 
public class HttpUtil {
	
	public static String sendGet(String url, String param) {
		String result = "";
		BufferedReader in = null;
		try {
			String urlNameString = url + "/" + param;
			URL realUrl = new URL(urlNameString);
			URLConnection connection = realUrl.openConnection();
			connection.setRequestProperty("accept", "*/*");
			connection.setRequestProperty("connection", "Keep-Alive");
			connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			connection.connect();
			in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String line;
			int cot = 0;
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
			System.out.println("发送GET请求出现异常!" + e);
			e.printStackTrace();
		}
		finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return result;
	}
 
	static Map totalMap = new TreeMap();
	
	public static void main(String[] args) {
		
		int startYear = 2019;//开始的年份
		int year = 2;		 //统计的年份
		
		for(int i = 0; i <= (year - 1); i++){
			for(int j = 1; j <= 12; j++) {
				loop(startYear + i + "", j + "");
			}
		}
		
		Iterator it = totalMap.keySet().iterator();
		while(it.hasNext()) {
			System.out.println(totalMap.get(it.next()));
		}
		
	}
	
	public static void loop(String year, String month) {
		String s = HttpUtil.sendGet("http://opendata.baidu.com/api.php?query="+year+"%E5%B9%B4"+month+"%E6%9C%88&resource_id=6018&format=json", "");
		
		Map<String, Object> map = (Map<String, Object>) JSONObject.parse(s);
		
		List list = (List) map.get("data");
		if(list == null || list.size() == 0) {
			return;
		}
		
		Map<String, Object> innerMap = (Map<String, Object>) ( list.get(0));
		if(innerMap.get("holiday") == null) {
			return;
		}
		
		List holidayList = new ArrayList();
		Object obj = innerMap.get("holiday");
		if(obj instanceof List) {
			holidayList = (List) obj;
		}else {
			holidayList.add((Map)obj);
		}
		 
		for(int i = 0; i < holidayList.size(); i++) {
			List innerHolidayList = (List) ((Map)holidayList.get(i)).get("list");
			for(int j = 0; j < innerHolidayList.size(); j++) {
				Map el = (Map) innerHolidayList.get(j);
				totalMap.put(el.get("date"), el.get("date") + "\t" + el.get("status"));
			}
		}
	}
}

输出示例

2018-12-29	2
2018-12-31	1
2019-1-1	1
2018-12-30	1
2019-2-4	1
2019-2-2	2
2019-2-3	2
2019-2-5	1
2019-2-6	1
2019-2-7	1
2019-2-8	1
2019-2-9	1
2019-2-10	1
2019-4-5	1
2019-4-6	1
2019-4-7	1
2019-5-1	1
2019-5-2	1
2019-5-3	1
2019-5-4	1
2019-4-28	2
2019-5-5	2
2019-6-7	1
2019-6-8	1
2019-6-9	1
2019-9-13	1
2019-9-14	1
2019-9-15	1
2019-10-1	1
2019-9-29	2
2019-10-2	1
2019-10-3	1
2019-10-4	1
2019-10-7	1
2019-10-12	2
2019-10-5	1
2019-10-6	1
2020-1-1	1
2020-1-24	1
2020-1-25	1
2020-1-26	1
2020-1-27	1
2020-1-28	1
2020-1-29	1
2020-1-30	1
2020-1-19	2
2020-1-31	1
2020-2-1	1
2020-2-2	1
2020-4-4	1
2020-4-5	1
2020-4-6	1
2020-5-1	1
2020-5-2	1
2020-5-3	1
2020-5-4	1
2020-5-5	1
2020-4-26	2
2020-5-9	2
2020-6-25	1
2020-6-26	1
2020-6-27	1
2020-6-28	2
2020-10-1	1
2020-10-2	1
2020-10-3	1
2020-10-4	1
2020-10-5	1
2020-10-6	1
2020-10-7	1
2020-10-8	1
2020-9-27	2
2020-10-10	2

  • 8
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值