package org.jeecg.modules.utils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.DateUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.util.*;
/**
* @version 1.0
* @Author zhaozhiqiang
* @Date 2023/2/23 12:26
* @Description //TODO 短信发送工具类 参照 https://blog.csdn.net/nayi_224/article/details/109068470
*/
@Slf4j
public class HttpUtil2 {
static Map totalMap = new TreeMap();
public static String sendGet(String url, String param) throws Exception {
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) {
log.debug("发送GET请求出现异常!" + e.getMessage());
e.printStackTrace();
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
throw e;
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
public static void main(String[] args) throws Exception {
int startYear = 2023;//开始的年份
int year = 02; //统计的年份
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));
}
// String hoday = getHoday();
// System.out.println(hoday);
}
/**
* status为1时为节假日,为2时就是改为工作日的休息日
*
* @throws ParseException 获取当天否节假日
*/
public static String getHoday() throws Exception {
String currentStrTime = DateUtils.formatDate(new Date());
//获取当前年份
String year = currentStrTime.substring(0, 4);
String month = currentStrTime.substring(5, 7);
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);
if (null != map && map.size() > 0) {
List list = (List) map.get("data");
if (null != list && list.size() > 0) {
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 (StringUtils.equals(key, currentStrTime) && "1".equals(status)) {
return key + "," + status;
}
}
}
}
return "";
}
public static void loop(String year, String month) throws Exception {
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);
}
}
}
}
判断法定日/节假日/修休息日
最新推荐文章于 2024-03-19 11:01:03 发布