JAVA判断当前时间是否为节假日、周末、工作日
需求
有这么个需求,需要判断传的这个日期是否为节假日,周末,工作日,然后做剩下的操作。
话不多说,上代码
1.首先需要拿到节假日api
- 其实这个api里有接口可以直接判断某天是否为周末,节假日,工作日;
- 但是这个接口访问多了会报一个403的错误,也就是请求太多导致的;
- 而我下面的内容是只请求一次全年节假日即可,放到一个集合里,第二次请求判断的时候就可以直接去集合里判断。速度很快!
2.拿到自己适用接口,如下:
参数:
1》year:格式“yyyy”,查询这一年的节假日
https://timor.tech/api/holiday/year
返回数据示例:
{ "code": 0, "holiday": { "01-01": { "holiday": true, "name": "元旦", "wage": 3, "date": "2023-01-01", "rest": 1 }, "01-02": { "holiday": true, "name": "元旦", "wage": 2, "date": "2023-01-02", "rest": 1 }, "01-21": { "holiday": true, "name": "除夕", "wage": 3, "date": "2023-01-21", "rest": 2 }, ..........
截图如下
3.拿到项目中,编写成一个HolidayUtil 工具类,代码如下:
import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.bh4q.dict.util.DateUtil;import org.apache.commons.lang.StringUtils;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.URL;import java.net.URLConnection;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.*;public class HolidayUtil { static Map> holiday =new HashMap<>();//假期 static Map> extraWorkDay =new HashMap<>();//调休日 //判断是否为节假日 public static String isWorkingDay(String time) throws ParseException { Date parse = null; //为空则返回当前时间 if (StringUtils.isNotBlank(time)){ SimpleDateFormat getYearFormat = new SimpleDateFormat("yyyy-MM-dd"); parse = getYearFormat.parse(time); }else { parse = new Date(); } String newDate = new SimpleDateFormat("yyyy").format(parse); //判断key是否有参数年份 if(!holiday.containsKey(newDate)){ getYearHoliday(newDate); } //得到日期是星期几 Date date = DateUtil.formatStringToDate(time, false); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int weekday = DateUtil.getDayOfWeek(calendar); //是否节假日 if(holiday.get(newDate).contains(time)){ return "2"; }else if(extraWorkDay.get(newDate).contains(time)){//是否为调休 return "0"; }else if(weekday == Calendar.SATURDAY || weekday == Calendar.FRIDAY){//是否为周末 return "1"; }else{ return "0"; } } public static String getYearHoliday(String date) throws ParseException { //获取免费api地址 String httpUrl="https://timor.tech/api/holiday/year/"+date; BufferedReader reader = null; String result = null; StringBuffer sbf = new StringBuffer(); try { URL url = new URL(httpUrl); URLConnection connection = url.openConnection(); //connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); connection.setRequestProperty("User-Agent", "Mozilla/4.76"); //使用Get方式请求数据 //connection.setRequestMethod("GET"); //connection.connect(); InputStream is = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); String strRead = null; while ((strRead = reader.readLine()) != null) { sbf.append(strRead); sbf.append("\r\n"); } reader.close(); //字符串转json JSONObject json = JSON.parseObject(sbf.toString()); //根据holiday字段获取jsonObject内容 JSONObject holiday = json.getJSONObject("holiday"); List hoList = new ArrayList<>(); List extraList = new ArrayList<>(); for (Map.Entry entry : holiday.entrySet()) { String value = entry.getValue().toString(); JSONObject jsonObject = JSONObject.parseObject(value); String hoBool = jsonObject.getString("holiday"); String extra = jsonObject.getString("date"); //判断不是假期后调休的班 if(hoBool.equals("true")){ hoList.add(extra); HolidayUtil.holiday.put(date,hoList); }else { extraList.add(extra); HolidayUtil.extraWorkDay.put(date,extraList); } } } catch (Exception e) { e.printStackTrace(); } return result; }}
4.调用工具类方法
public static void main(String[] args) { String date = "2023-07-12"; String yearHoliday = HolidayUtil.isWorkingDay(date); System.out.println(yearHoliday); }
打印结果:0
搞定收工
希望可以帮助到您
~打工人冲啊~
.
来源地址:https://blog.csdn.net/m0_50762431/article/details/131585664