利用百度万年历判断指定日期是否是节假日
程序员文章站
2022-05-17 20:33:49
...
利用百度万年历判断指定日期是否是节假日
对接第三方平台,遇到一个需求,非工作日进行提现操作手续费会更贵一些,客户要求必须在工作日才能进行提现操作,所以需要判断当前日期是否是节假日,代码如下。
package com.hclc.util;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.util.CollectionUtils;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.Set;
/**
* @ClassName PerpetualCalendarUtil
* @Author: yurj
* @Mail:[email protected]
* @Date: Create in 15:30 2020/6/10
* @version: 1.0
*/
@Slf4j
public class PerpetualCalendarUtil {
/**
* 当年国定节假日
*/
private static Set<String> holidaySet = new HashSet();
/**
* 百度日期api接口
*/
private static String PERPETUAL_CALENDAR_URL = "http://opendata.baidu.com/api.php?query=FORMAT_DATE&resource_id=6018&format=json";
/**
* 放假
*/
private static final String HOLIDAY = "1";
/**
* 补班
*/
private static final String MAKE_UP = "2";
/**
* 日期格式化规则
*/
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-M-d");
/**
* 正确响应状态
*/
private static String API_SUCCESS_STATUS = "0";
/**
* 获取当年节假日set集合
*
* @param
* @return java.util.Set<java.lang.String>
* @author yurj
* @mail [email protected]
* @version 1.0
* @date 2020/6/10 17:27
*/
public static Set<String> getCurrentYearHolidays(LocalDate localDate) {
if (!holidaySet.isEmpty()) {
LocalDate setLocalDate = LocalDate.parse(holidaySet.iterator().next(), formatter);
if (localDate.getYear() == setLocalDate.getYear()) {
return holidaySet;
}
}
holidaySet.clear();
//当年所有法定节假日(注:周六周日属于公休日,不是法定节假日)
PERPETUAL_CALENDAR_URL = PERPETUAL_CALENDAR_URL.replace("FORMAT_DATE", localDate.toString());
JSONObject resultData = JSONObject.fromObject(HttpUtils.doGet(PERPETUAL_CALENDAR_URL));
try {
JSONArray dataArr = API_SUCCESS_STATUS.equals(resultData.get("status")) ? ((JSONArray) JSONObject.fromObject(resultData).get("data")) : null;
if (!CollectionUtils.isEmpty(dataArr)) {
JSONObject holidayObject = (JSONObject) dataArr.get(0);
JSONArray holidayArr = (JSONArray) holidayObject.get("holiday");
holidayArr.stream().forEach(arr -> {
((JSONArray) ((JSONObject) arr).get("list")).stream().forEach(time -> {
JSONObject timeJson = (JSONObject) time;
if (HOLIDAY.equals(timeJson.get("status"))) {
holidaySet.add(LocalDate.parse(timeJson.getString("date"), formatter).toString());
}
});
});
}
} catch (Exception e) {
log.error("获取当年节假日set集合解析异常,url:{}", PERPETUAL_CALENDAR_URL, e);
}
return holidaySet;
}
/**
* 判断指定日期是否是周末
*
* @param localDate
* @return boolean
* @author yurj
* @mail [email protected]
* @version 1.0
* @date 2020/6/10 17:54
*/
public static boolean currentDayIsWeekend(LocalDate localDate) {
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
return dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY;
}
/**
* 判断指定日期是否是工作日
*
* @param
* @return boolean
* @author yurj
* @mail [email protected]
* @version 1.0
* @date 2020/6/10 17:48
*/
public static boolean currentDayIsWorkingDay() {
LocalDate localDate = LocalDate.now();
return !(currentDayIsWeekend(localDate) || getCurrentYearHolidays(localDate).contains(localDate.toString()));
}
public static void main(String[] args) {
//示例
System.out.println(currentDayIsWorkingDay());
System.out.println(getCurrentYearHolidays(LocalDate.now()));
}
}
对应json格式如下:
{
"status":"0",
"t":"",
"set_cache_time":"",
"data":[
{
"StdStg":6018,
"StdStl":8,
"_update_time":"1580102250",
"cambrian_appid":"0",
"almanac":[
{
"avoid":"诸事不宜.",
"date":"2020-6-1",
"suit":"祭祀.解除.破屋.坏垣.余事勿取."
},
{
"avoid":"",
"date":"2020-6-2",
"suit":"嫁娶.祭祀.祈福.求嗣.开光.出行.拆卸.动土.上梁.出火.进人口.入宅.移徙.安床.栽种.纳畜.牧养.竖柱.安门.修造.解除.会亲友."
},
{
"avoid":"入宅.移徙.理发.出火.嫁娶.出行.",
"date":"2020-6-3",
"suit":"开市.交易.立券.祭祀.祈福.开光.伐木.进人口.安床.拆卸.修造.动土.栽种.破土.移柩.安葬."
},
{
"avoid":"诸事不宜.",
"date":"2020-6-4",
"suit":"结网.解除.余事勿取."
},
{
"avoid":"开市.安葬.破土.修坟.掘井.",
"date":"2020-6-5",
"suit":"祭祀.作灶.余事勿取."
},
{
"avoid":"合帐.上梁.经络.安葬.入殓.",
"date":"2020-6-6",
"suit":"祭祀.祈福.求嗣.斋醮.安香.解除.移徙.入宅.会亲友.求医.治病.动土.破土.开生坟.合寿木."
},
{
"avoid":"祈福.开光.掘井.开市.安葬.",
"date":"2020-6-7",
"suit":"嫁娶.冠笄.修造.动土.作灶.移徙.入宅.补垣.塞穴.纳畜.牧养.架马.修造.动土.起基.定磉.开池.造船."
},
{
"avoid":"斋醮.开渠.上梁.动土.破土.",
"date":"2020-6-8",
"suit":"祭祀.交易.纳财."
},
{
"avoid":"行丧.伐木.作梁.作灶.",
"date":"2020-6-9",
"suit":"嫁娶.订盟.纳采.冠笄.会亲友.安机械.造车器.祭祀.出行.纳财.入宅.安香.出火.入学.塑绘.开光.拆卸.起基.修造.动土.牧养.栽种.安门.作厕."
},
{
"avoid":"安床.出货财.作灶.动土.破土.",
"date":"2020-6-10",
"suit":"开光.求嗣.出行.冠笄.嫁娶.伐木.架马.开柱眼.修造.移徙.入宅.开市.交易.立券.出行.安香.出火.挂匾.起基.修造.开生坟.合寿木.入殓.除服.成服.移柩.安葬."
},
{
"avoid":"斋醮.出行.治病.合寿木.",
"date":"2020-6-11",
"suit":"祭祀.沐浴.理发.嫁娶.作灶.整手足甲.扫舍.修饰垣墙.平治道涂."
},
{
"avoid":"开市.伐木.作梁.作灶.",
"date":"2020-6-12",
"suit":"安机械.移徙.入宅.出行.祭祀.祈福.斋醮.纳采.订盟.安香.出火.解除.会亲友.修造.动土.拆卸.起基.定磉.移徙.入宅.造屋.安床.修造.破土.安葬.入殓.立碑."
},
{
"avoid":"开市.交易.嫁娶.安葬.行丧.",
"date":"2020-6-13",
"suit":"祭祀.沐浴.捕捉.结网.畋猎.取渔.余事勿取."
},
{
"avoid":"嫁娶.入宅.",
"date":"2020-6-14",
"suit":"破屋.坏垣.求医.治病.畋猎.余事勿取."
},
{
"avoid":"开市.安门.掘井.作灶.",
"date":"2020-6-15",
"suit":"嫁娶.出行.安机械.祭祀.塑绘.开光.治病.经络.安床.结网.塞穴.破土.入殓."
},
{
"avoid":"",
"date":"2020-6-16",
"suit":"订盟.纳采.会亲友.进人口.雕刻.拆卸.修造.动土.起基.开市.栽种.纳畜.牧养.入殓.除服.成服.移柩.破土.安葬."
},
{
"avoid":"诸事不宜.",
"date":"2020-6-17",
"suit":"祭祀.捕捉.取渔.修饰垣墙.余事勿取."
},
{
"avoid":"开市.安门.",
"date":"2020-6-18",
"suit":"嫁娶.纳采.祭祀.祈福.求医.治病.出行.动土.移徙.入宅."
},
{
"avoid":"嫁娶.安葬.",
"date":"2020-6-19",
"suit":"裁衣.作灶.移徙.入宅.纳畜."
},
{
"avoid":"上梁.动土.破土.",
"date":"2020-6-20",
"suit":"祭祀.入殓.移柩.启攒.安葬."
},
{
"avoid":"移徙.入宅.安葬.",
"date":"2020-6-21",
"suit":"订盟.纳采.出行.祈福.斋醮.安床.会亲友."
},
{
"avoid":"动土.破土.",
"date":"2020-6-22",
"suit":"嫁娶.纳采.出行.求医.治病.开市.移徙.入宅.启攒.安葬."
},
{
"avoid":"行丧.安葬.",
"date":"2020-6-23",
"suit":"嫁娶.祭祀.沐浴.扫舍.修饰垣墙."
},
{
"avoid":"作灶.",
"date":"2020-6-24",
"suit":"嫁娶.订盟.纳采.出行.开市.祭祀.祈福.动土.移徙.入宅.破土.安葬."
},
{
"avoid":"开市.安葬.",
"date":"2020-6-25",
"suit":"订盟.纳采.出行.祭祀.祈福.修造.动土.移徙.入宅."
},
{
"avoid":"诸事不宜.",
"date":"2020-6-26",
"suit":"诸事不宜."
},
{
"avoid":"开光.开市.",
"date":"2020-6-27",
"suit":"嫁娶.订盟.纳采.祭祀.祈福.入殓.破土.安葬."
},
{
"avoid":"嫁娶.开光.",
"date":"2020-6-28",
"suit":"开光.求医.治病.动土.上梁.入殓.破土.安葬."
},
{
"avoid":"诸事不宜.",
"date":"2020-6-29",
"suit":"祭祀.栽种.余事勿取."
},
{
"avoid":"安葬.开市.交易.立券.",
"date":"2020-6-30",
"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年6月10日",
"selectday":"2020-6-10",
"url":"http:\/\/nourl.baidu.com\/6018",
"loc":"http:\/\/open.baidu.com\/q?r=2002753&k=2020%E5%B9%B46%E6%9C%8810%E6%97%A5",
"SiteId":2002753,
"_version":2767,
"_select_time":1580102077,
"clicklimit":"1-3",
"ExtendedLocation":"",
"OriginQuery":"2020-06-10",
"tplt":"calendar_new",
"resourceid":"6018",
"fetchkey":"6018_2020年6月10日",
"role_id":10,
"disp_type":0,
"appinfo":""
}]
}