| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- 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<JSONObject> 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<JSONObject> getYearHoliday(String year) {
- List<JSONObject> 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<JSONObject> formatHoliday(String json) {
- List<JSONObject> 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;
- }
- }
|