package cn.cslg.pas.common.utils.commonUtils; import cn.cslg.pas.common.utils.RedisUtil; import cn.cslg.pas.domain.report.OralNotice; import cn.cslg.pas.exception.XiaoShiException; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; 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.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.TimeUnit; @Component public class HoliDayUtils { @Autowired private RedisUtil redisUtil; public String isWorkingDay(Date parse) throws ParseException { SimpleDateFormat getYearFormat = new SimpleDateFormat("yyyy-MM-dd"); String newDate = new SimpleDateFormat("yyyy").format(parse); String time = getYearFormat.format(parse); Date date = new SimpleDateFormat("yyyy-MM-dd").parse(time); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int weekday = calendar.get(Calendar.DAY_OF_WEEK); List jsonObjects = this.getYearHoliday(newDate); JSONObject temJsonObject = null; for (JSONObject jsonObject : jsonObjects) { String temDate = jsonObject.getString("date"); if (time.equals(temDate)) { temJsonObject = jsonObject; break; } } if (temJsonObject != null) { if (temJsonObject.getBoolean("holiday")) { return "2"; // 节假日 } else if (!temJsonObject.getBoolean("holiday")) { return "3"; // 调休日 } } else { if (weekday == Calendar.SATURDAY || weekday == Calendar.SUNDAY) { return "1"; // 周末 } else { return "0"; } } return "0"; } public List getYearHoliday(String year) { List jsonObjects = new ArrayList<>(); String key = "holiday_" + year; String json = redisUtil.get(key); if (json == null || json.trim().equals("")) { String re = this.getYearHolidayHttp(year); if (re == null || re.trim().equals("")) { throw new XiaoShiException("获取节假日信息异常"); } jsonObjects = this.formatHoliday(re); json = JSON.toJSONString(jsonObjects); redisUtil.set(key, json); } else { jsonObjects = JSON.parseArray(json, JSONObject.class); } return jsonObjects; } public String getYearHolidayHttp(String year) { String urlPath = "https://timor.tech/api/holiday/year/" + year; OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(60000, TimeUnit.SECONDS) .writeTimeout(60000, TimeUnit.SECONDS) .readTimeout(60000, TimeUnit.SECONDS) .build(); Request request = new Request.Builder() .url(urlPath) .get() .header("User-Agent", "Mozilla/4.76") .build(); Integer tryTime = 0; String re = null; while (tryTime < 3) { try { Response response = okHttpClient.newCall(request).execute(); // 判断请求是否成功 if (response.isSuccessful()) { // 打印服务端返回结果 re = Objects.requireNonNull(response.body()).string(); return re; } tryTime++; } catch (IOException e) { e.printStackTrace(); } } // StringBuilder re = new StringBuilder(); // try { // // // URL url = new URL(urlPath); // URLConnection connection = url.openConnection(); // connection.setRequestProperty("User-Agent", "Mozilla/4.76"); // BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); // String strRead; // while ((strRead = reader.readLine()) != null) { // re.append(strRead).append("\r\n"); // } // reader.close(); // } // catch (Exception e){ // // } return re; } public List formatHoliday(String json) { List jsonObjects = new ArrayList<>(); JSONObject jsonObject = JSONObject.parseObject(json); String holidayStr = jsonObject.getString("holiday"); JSONObject bodyJsonObject = JSONObject.parseObject(holidayStr); for (String key : bodyJsonObject.keySet()) { JSONObject mainObject = bodyJsonObject.getJSONObject(key); jsonObjects.add(mainObject); } return jsonObjects; } public Date getWorkingDay(Date date) throws ParseException { Boolean ifWorkingDate = false; while (!ifWorkingDate) { String re = this.isWorkingDay(date); if (re.equals("1") || re.equals("2")) { // 使用Calendar类加一天 Calendar calendar = Calendar.getInstance(); calendar.setTime(date); // 将Date对象设置到Calendar中 calendar.add(Calendar.DATE, 1); // 增加一天。使用 Calendar.DAY_OF_MONTH 效果相同 date= calendar.getTime(); // 转换回Date对象 } else { ifWorkingDate = true; break; } } return date; } }