Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR,1);
int year = calendar.get(Calendar.YEAR);
Map<Integer, List<String>> holidayList =new HashMap<>(); //假期
Map<Integer,List<String>> extraWorkDay =new HashMap<>(); //调休日
//获取免费api地址
String httpUrl="https://timor.tech/api/holiday/year/"+year+"/";
BufferedReader reader = null;
// 用于存储返回的年份节假日数据\
StringBuffer sbf = new StringBuffer();
try {
URL url = new URL(httpUrl);
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/4.76");
connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead); // 将每行内容追加到sbf字符串变量中
sbf.append("\r\n"); // 手动添加换行符
}
reader.close();
//字符串转json
JSONObject json = JSON.parseObject(sbf.toString());
//根据holiday字段获取jsonObject内容
JSONObject holiday = json.getJSONObject("holiday");
if (!holiday.isEmpty()){
List<String> hoList = new ArrayList<>();
List<String> extraList = new ArrayList<>();
for (Map.Entry<String, Object> entry : holiday.entrySet()) {
// 读取字符的值,并转换成字符串
String value = entry.getValue().toString();
// 将字符串改为json格式
JSONObject jsonObject = JSONObject.parseObject(value);
// 获取是否为假日(是为true,不是为false)
String hoBool = jsonObject.getString("holiday");
// 获取时间
String extra = jsonObject.getString("date");
//判断不是假期后调休的班
if(hoBool.equals("true")){
hoList.add(extra);
// 添加假期
holidayList.put(year,hoList);
}else {
extraList.add(extra);
// 添加调休日
extraWorkDay.put(year,extraList);
}
}
}
if(CollectionUtils.isNotEmpty(holidayList.get(year))){
List<String> list = holidayList.get(year);
List<RestDateInfo> needList = Lists.newArrayList();
for (String s : list) {
RestDateInfo restDateInfo = new RestDateInfo();
restDateInfo.setRestDate(s);
restDateInfo.setYear(String.valueOf(year));
needList.add(restDateInfo);
}
scheduledMapper.insertRestDateInfo(needList);
}
} catch (Exception e) {
logger.error("获取节假日日期错误{}",e.getMessage());
}
然后存盘,地址的话看情况自己换