JAVA判断当前时间是否为节假日、周末、工作日 需求 有这么个需求,需要判断传的这个日期是否为节假日,周末,工作日,然后做剩下的操作。 话不多说,上代码 1.首先需要拿到节假日api 节假日API地址 其实这个api里有接口可以直接判断某
有这么个需求,需要判断传的这个日期是否为节假日,周末,工作日,然后做剩下的操作。
参数:
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 }, ..........
截图如下
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; }}
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
--结束END--
本文标题: JAVA判断当前时间是否为节假日、周末、工作日,调休日,不报错:IOException!
本文链接: https://lsjlt.com/news/372640.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-04-01
2024-04-03
2024-04-03
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0