springboot框架调取百度api获取节假日期,并获取某年某月的工作日天数(除去国家法定节假日)
统计考勤的业务需求,写了一个接口,将一年的节假日保存入库,以便获取某月的工作日天数的时候,可以去掉节假日,加上是工作日的周六周天日。精准的获取某月的应到天数。该接口是将假期保存入库。状态为1的是放假日,状态为2的是工作日。
@RestController public class HolidayController { @Autowired private IHolidayService iHolidayService; static String BAIDU_URL = "http://opendata.baidu.com/api.php?resource_id=6018&format=json&query="; @PostMapping("/saveHoliday") @ResponseBody public ResultData saveHoliday(@RequestParam(value = "month",required = false) String month) throws ParseException { // System.out.println("holidayExist==="+ WorkDayUtils.getWorkDay()); int a = 0; SimpleDateFormat format = new SimpleDateFormat("yyyy-M-d"); SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd"); List<String> list = new ArrayList<String>(); //String month = "2020-10"; Map<String,Object> map = new HashMap<String,Object>(); String responseStr = ""; try{ responseStr = HttpUtil.post(BAIDU_URL+month,map); }catch (Exception e){ e.printStackTrace(); // throw new Exception("第三方接口异常"); } // JSONObject objects = JSONObject.fromObject(responseStr); com.alibaba.fastjson.JSONObject jsonTokenStr = JSON.parseObject(responseStr); // System.out.println("responseStr"+JSON.toJSONString(jsonTokenStr)); if(jsonTokenStr.getString("status").equalsIgnoreCase("0")){ JSONArray data = jsonTokenStr.getJSONArray("data"); System.out.println("data"+data); if(data.size()>0){ JSONObject item = data.getJSONObject(0); JSONArray holiday = item.getJSONArray("holiday"); System.out.println("holiday"+holiday); if(holiday.size()>0){ for(int i = 0 ; i < holiday.size(); i ++){ JSONObject h = holiday.getJSONObject(i); JSONArray hh = h.getJSONArray("list"); if(hh.size()>0){ for(int j = 0; j < hh.size(); j++){ JSONObject hhh = hh.getJSONObject(j); String dateStr = hhh.getString("date"); dateStr = format1.format(format.parse(dateStr)); // if(hhh.getString("status").equals("1")){ // list.add(dateStr); // } Holiday holidays = new Holiday(); holidays.setDate(dateStr); List<Holiday> holidayExist = iHolidayService.selectHolidayList(holidays); System.out.println("holidayExist==="+holidayExist); if(holidayExist.size()==0){//已经有数据 Holiday holiday1 = new Holiday(); holiday1.setDate(dateStr); holiday1.setWorkstate(hhh.getString("status")); holiday1.setValid("Y"); a= iHolidayService.insertHoliday(holiday1); } } } } } } } return ResultData.toresult(a); } }百度接口地址http://opendata.baidu.com/api.php?resource_id=6018&format=json&query=2020-12 返回的数据格式为:
: { "status": "0", "t": "", "set_cache_time": "", "data": [ { "StdStg": 6018, "StdStl": 8, "_update_time": "1580102250", "cambrian_appid": "0", "almanac": [ { "avoid": "栽种.破土.置产.祭祀.嫁娶.动土.作灶.祈福.", "date": "2020-12-1", "suit": "开市.交易.立券.挂匾.纳财.开光.出行.入宅.移徙.安床.纳畜.入殓.移柩.安葬." }, { "avoid": "", "date": "2020-12-2", "suit": "嫁娶.祭祀.祈福.求嗣.开光.出行.解除.出火.出行.拆卸.进人口.入宅.移徙.安床.栽种.动土.修造.纳畜.入殓.安葬.立碑.除服.成服." }, { "avoid": "嫁娶.开市.出火.栽种.破土.动土.入宅.移徙.安香.分居.掘井.作灶.", "date": "2020-12-3", "suit": "开光.解除.拆卸.修造.动土.安床.纳畜.安葬.启攒.入殓." }, { "avoid": "诸事不宜.", "date": "2020-12-4", "suit": "破屋.坏垣.余事勿取." }, { "avoid": "探病.安葬.", "date": "2020-12-5", "suit": "嫁娶.祭祀.开光.出火.出行.拆卸.修造.动土.解除.开市.交易.立券.挂匾.纳财.入宅.移徙.安床.栽种.纳畜." }, { "avoid": "入宅.作灶.词讼.移徙.出行.赴任.", "date": "2020-12-6", "suit": "祭祀.祈福.求嗣.开光.解除.理发.会亲友.栽种.纳畜.牧养.安葬.修坟.立碑.启攒." }, { "avoid": "嫁娶.动土.安床.造桥.掘井.", "date": "2020-12-7", "suit": "纳采.订盟.移徙.入宅.出行.安机械.会亲友.祭祀.祈福.斋醮.开光.安香.出火.解除.求医.针灸.治病.造屋.起基.修造.安门.造船.纳畜.牧养.移柩.入殓.启攒.谢土.修坟.立碑." }, { "avoid": "开市.斋醮.破土.安葬.", "date": "2020-12-8", "suit": "祭祀.沐浴.作灶.纳财.捕捉.畋猎.安床.扫舍." }, { "avoid": "移徙.开市.入宅.安葬.", "date": "2020-12-9", "suit": "祈福.斋醮.纳采.订盟.解除.架马.开柱眼.修造.动土.起基.上梁.归岫.造屋.合脊.掘井.除服.成服.破土.栽种." }, { "avoid": "嫁娶.祈福.开光.掘井.安葬.行丧.", "date": "2020-12-10", "suit": "纳采.订盟.祭祀.沐浴.冠笄.合帐.裁衣.修造.动土.拆卸.移徙.入宅.安门.开仓.筑堤.作厕.栽种.纳畜.补垣.塞穴." }, { "avoid": "入宅.动土.破土.嫁娶.作灶.造船.", "date": "2020-12-11", "suit": "合帐.裁衣.教牛马.余事勿取." }, { "avoid": "斋醮.作梁.掘井.行丧.安葬.", "date": "2020-12-12", "suit": "纳采.订盟.嫁娶.祭祀.祈福.安香.出火.出行.会亲友.经络.求医.治病.解除.拆卸.起基.修造.动土.定磉.扫舍.栽种.牧养.造畜椆栖." }, { "avoid": "嫁娶.祈福.出火.移徙.入宅.", "date": "2020-12-13", "suit": "纳财.开市.交易.立券.会亲友.进人口.经络.祭祀.祈福.安香.出火.求医.治病.修造.动土.拆卸.扫舍.安床.栽种.牧养.开生坟.合寿木.入殓.安葬.启攒." }, { "avoid": "入宅.修造.动土.破土.安门.上梁.", "date": "2020-12-14", "suit": "祭祀.入殓.移柩.余事勿取." }, { "avoid": "掘井.伐木.斋醮.作灶.", "date": "2020-12-15", "suit": "塑绘.开光.订盟.纳采.裁衣.冠笄.拆卸.修造.安床.入宅.出火.安葬.谢土.赴任." }, { "avoid": "出行.安葬.修坟.开市.", "date": "2020-12-16", "suit": "祭祀.塑绘.开光.裁衣.冠笄.嫁娶.纳采.拆卸.修造.动土.竖柱.上梁.安床.移徙.入宅.安香.结网.捕捉.畋猎.伐木.进人口.放水." }, { "avoid": "诸事不宜.", "date": "2020-12-17", "suit": "祭祀.求医.破屋.坏垣.余事勿取." }, { "avoid": "开光.栽种.治病.安门.作灶.", "date": "2020-12-18", "suit": "祭祀.祈福.斋醮.出行.冠笄.安机械.移徙.入宅.安香.安床.除服.成服.移柩.启攒." }, { "avoid": "", "date": "2020-12-19", "suit": "塑绘.斋醮.出行.拆卸.解除.修造.移徙.造船.入殓.除服.成服.移柩.启攒.修坟.立碑.谢土." }, { "avoid": "开市.破土.", "date": "2020-12-20", "suit": "祭祀.沐浴.安床.纳财.畋猎.捕捉." }, { "avoid": "嫁娶.作灶.", "date": "2020-12-21", "suit": "订盟.纳采.祭祀.祈福.修造.动土.上梁.破土." }, { "avoid": "入宅.安葬.", "date": "2020-12-22", "suit": "出行.沐浴.理发.补垣.塞穴." }, { "avoid": "入宅.动土.破土.余事勿取.", "date": "2020-12-23", "suit": "教牛马.余事勿取." }, { "avoid": "开市.安葬.", "date": "2020-12-24", "suit": "嫁娶.出行.求医.治病.祭祀.祈福.上梁.纳畜." }, { "avoid": "嫁娶.祈福.", "date": "2020-12-25", "suit": "开市.立券.开光.解除.安机械.上梁.启攒.安葬." }, { "avoid": "诸事不宜.", "date": "2020-12-26", "suit": "平治道涂.余事勿取." }, { "avoid": "开市.嫁娶.", "date": "2020-12-27", "suit": "求嗣.斋醮.塑绘.订盟.纳采.出火.拆卸.修造.动土.造桥.安机械.栽种.纳畜.牧养.入殓.除服.成服.移柩.破土.安葬." }, { "avoid": "开市.安葬.", "date": "2020-12-28", "suit": "嫁娶.订盟.纳采.祭祀.祈福.修造.动土.移徙.入宅." }, { "avoid": "诸事不宜.", "date": "2020-12-29", "suit": "治病.破屋.坏垣.余事勿取." }, { "avoid": "造桥.安门.理发.造庙.栽种.作灶.", "date": "2020-12-30", "suit": "祭祀.祈福.求嗣.斋醮.开光.入学.订盟.冠笄.伐木.修造.动土.起基.放水.交易.开池." }, { "avoid": "诸事不宜.", "date": "2020-12-31", "suit": "解除.坏垣.余事勿取." } ], "holiday": [ { "desc": "1月1日放假一天", "festival": "2020-1-1", "list": [ { "date": "2020-1-1", "status": "1" } ], "list#num#baidu": 1, "name": "元旦", "rest": "2019年12月30日和2019年12月31日请假两天,与周末连休可拼5天小长假。" }, { "desc": "1月24日放假一天", "festival": "2020-1-24", "list": [ { "date": "2020-1-24", "status": "1" } ], "list#num#baidu": 1, "name": "除夕", "rest": "农历腊月最后一天为除夕,即大年初一前夜,又称为年三十。" }, { "desc": "1月24日(除夕)至1月30日放假7天,1月19日,2月1日上班", "festival": "2020-1-25", "list": [ { "date": "2020-1-24", "status": "1" }, { "date": "2020-1-25", "status": "1" }, { "date": "2020-1-26", "status": "1" }, { "date": "2020-1-27", "status": "1" }, { "date": "2020-1-28", "status": "1" }, { "date": "2020-1-29", "status": "1" }, { "date": "2020-1-30", "status": "1" }, { "date": "2020-1-19", "status": "2" }, { "date": "2020-1-31", "status": "1" }, { "date": "2020-2-1", "status": "1" }, { "date": "2020-2-2", "status": "1" } ], "list#num#baidu": 11, "name": "春节", "rest": "2020年1月19日至2020年1月23日请假5天,与周末连休可拼13天长假。" }, { "desc": "4月4日至4月6日放假3天", "festival": "2020-4-4", "list": [ { "date": "2020-4-4", "status": "1" }, { "date": "2020-4-5", "status": "1" }, { "date": "2020-4-6", "status": "1" } ], "list#num#baidu": 3, "name": "清明节", "rest": "2020年4月7日至2020年4月10日请假4天,与周末连休可拼9天长假。" }, { "desc": "5月1日至5月5日放假5天,4月26日,5月9日上班", "festival": "2020-5-1", "list": [ { "date": "2020-5-1", "status": "1" }, { "date": "2020-5-2", "status": "1" }, { "date": "2020-5-3", "status": "1" }, { "date": "2020-5-4", "status": "1" }, { "date": "2020-5-5", "status": "1" }, { "date": "2020-4-26", "status": "2" }, { "date": "2020-5-9", "status": "2" } ], "list#num#baidu": 7, "name": "劳动节", "rest": "2020年4月26日至2020年4月30日请假5天,与周末连休可拼11天长假。" }, { "desc": "6月25日至6月27日放假3天,6月28日上班", "festival": "2020-6-25", "list": [ { "date": "2020-6-25", "status": "1" }, { "date": "2020-6-26", "status": "1" }, { "date": "2020-6-27", "status": "1" }, { "date": "2020-6-28", "status": "2" } ], "list#num#baidu": 4, "name": "端午节", "rest": "2020年6月22日至2020年6月24日请假3天,与周末连休可拼8天长假。" }, { "desc": "10月1日至10月8日放假8天,9月27日,10月10日上班", "festival": "2020-10-1", "list": [ { "date": "2020-10-1", "status": "1" }, { "date": "2020-10-2", "status": "1" }, { "date": "2020-10-3", "status": "1" }, { "date": "2020-10-4", "status": "1" }, { "date": "2020-10-5", "status": "1" }, { "date": "2020-10-6", "status": "1" }, { "date": "2020-10-7", "status": "1" }, { "date": "2020-10-8", "status": "1" }, { "date": "2020-9-27", "status": "2" }, { "date": "2020-10-10", "status": "2" } ], "list#num#baidu": 10, "name": "中秋节", "rest": "10月9日至10月10日请假2天,与周末连休可拼11天长假。" }, { "desc": "10月1日至10月8日放假8天,9月27日,10月10日上班", "festival": "2020-10-1", "list": [ { "date": "2020-10-1", "status": "1" }, { "date": "2020-10-2", "status": "1" }, { "date": "2020-10-3", "status": "1" }, { "date": "2020-10-4", "status": "1" }, { "date": "2020-10-5", "status": "1" }, { "date": "2020-10-6", "status": "1" }, { "date": "2020-10-7", "status": "1" }, { "date": "2020-10-8", "status": "1" }, { "date": "2020-9-27", "status": "2" }, { "date": "2020-10-10", "status": "2" } ], "list#num#baidu": 10, "name": "国庆节", "rest": "10月9日至10月10日请假2天,与周末连休可拼11天长假。" } ], "holidaylist": [ { "name": "元旦", "startday": "2020-1-1" }, { "name": "除夕", "startday": "2020-1-24" }, { "name": "春节", "startday": "2020-1-25" }, { "name": "清明节", "startday": "2020-4-4" }, { "name": "劳动节", "startday": "2020-5-1" }, { "name": "端午节", "startday": "2020-6-25" }, { "name": "中秋节", "startday": "2020-10-1" }, { "name": "国庆节", "startday": "2020-10-1" } ], "key": "2020年12月", "selectday": "2020-12-1", "url": "http://nourl.baidu.com/6018", "loc": "http://open.baidu.com/q?r=2002753&k=2020%E5%B9%B412%E6%9C%88", "SiteId": 2002753, "_version": 2767, "_select_time": 1580102077, "clicklimit": "1-3", "ExtendedLocation": "", "OriginQuery": "2020-12", "tplt": "calendar_new", "resourceid": "6018", "fetchkey": "6018_2020年12月", "role_id": 10, "disp_type": 0, "appinfo": "" } ] }
然后通过公共方法获取某月工作日的天数 // 返回工作日集合,只排除周末 public static List<Date> getWorkDays(int year, int month){ // 用于储存每月工作日 List<Date> dates = new ArrayList<Date>(); Calendar cal = Calendar.getInstance(); //设置月份 cal.set(Calendar.YEAR, year); // 设置月份 cal.set(Calendar.MONTH, month - 1); // 设置为当月第一天 cal.set(Calendar.DATE, 1); while(cal.get(Calendar.YEAR) == year && cal.get(Calendar.MONTH) < month){ // 判断当前天为本周的第几天 int day = cal.get(Calendar.DAY_OF_WEEK); // 如果不为周六或者周天,将日期进行储存 if(!(day == Calendar.SUNDAY || day == Calendar.SATURDAY)){ dates.add((Date)cal.getTime().clone()); } // 将当前日期增加一天 cal.add(Calendar.DATE, 1); } // 返回当前月工作日集合 return dates; } /** * * @param dateList * @return 返回日期字符串集合 */ public static List<String> getDateString(List<Date> dateList){ // 储存日期字符串 List<String> dateString = new ArrayList<>(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); for (Date date : dateList){ String date2 = simpleDateFormat.format(date); dateString.add(date2); } return dateString; } public static int getWorkDay(String month){ List<Date> workDays = getWorkDays(Integer.parseInt(month.split("-")[0]), Integer.parseInt(month.split("-")[1])); // 获取工作日字符串集合 List<String> dateString = getDateString(workDays); // 获取法定节假日集合 List list = staticMapper.selectHolidayByDate(month); System.out.println("list==="+list.size()); List holidayList= new ArrayList(); List workdayList= new ArrayList(); for(int i =0;i<list.size();i++){ Holiday holiday11 = (Holiday) list.get(i); if(holiday11.getWorkstate().equals("1")){ holidayList.add(holiday11.getDate()); }else if(holiday11.getWorkstate().equals("2")){ workdayList.add(holiday11.getDate()); } } // List<String> holidays = HolidayUtils.getList(month); // System.out.println("holidays==="+holidays); // 从工作日中除去法定节假日 dateString.removeAll(holidayList); // 从工作日中增加周六日工作日 dateString.addAll(workdayList); System.out.println(dateString.size()); // 遍历纯工作日集合 for (String date : dateString){ System.out.println(date); } return dateString.size(); }