java动态获取节假日工具
方式一:api版
https://www.kancloud.cn/xiaoggvip/holiday_free/1683537
https://gitee.com/web/holidays_api
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.jtsmartway.zhgsgl.common.redis.service.RedisService;
import com.jtsmartway.zhgsgl.warning.constant.BusinessConstants;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* 节假日工具
*
* @version 1.0
* @date 2022/6/7 15:10
* @since : JDK 11
*/
public class Holiday {
private static final String API_URL = "http://tool.bitefu.net/jiari/?d=";
private static final String WORKDAY = "0";
private static final String DAY_OFF = "1";
private static final String HOLIDAY = "2";
/**
* 节假日api
*
* @param dateStr 日期字符串
* @return
*/
private static String get(String dateStr) {
return HttpUtil.get(API_URL + dateStr);
}
/**
* 是否处于节假日
*
* @param dateTimeStr 时间字符串 yyyy-MM-dd
* @return
*/
public static boolean isHoliday(String dateTimeStr) {
return get(dateTimeStr).equals(HOLIDAY);
}
/**
* 获取某年所有节假日信息
*/
public static List<String> allHoliday(String yearStr) {
String res = get(yearStr);
JSONObject jsonObject = JSONUtil.parseObj(res).getJSONObject(yearStr);
List<String> all = new ArrayList<>();
Set<String> daySet = jsonObject.keySet();
for (String s : daySet) {
all.add(DateUtil.parse(yearStr + s).toString("yyyy-MM-dd"));
}
return all;
}
/**
* 缓存某年节假日信息到redis中间件中
*/
public static void cacheToMiddleware(RedisService service, String year) {
if (null == service) {
return;
}
List<String> l = allHoliday(year);
service.setCacheList(BusinessConstants.CacheKey.HOLIDAY + DateUtil.thisYear(), l);
}
/**
* 缓存本年节日信息到redis中间件中
*/
public static void cacheToMiddleware(RedisService service) {
cacheToMiddleware(service, String.valueOf(DateUtil.thisYear()));
}
/**
* 根据节日判断是否发送邮件
*
* @param flag 是否跳过标志
* @return 是否发送信息
*/
public static Boolean isSend(boolean flag, RedisService redisService) {
if (flag) {
List<String> holidayList = redisService.getCacheList(BusinessConstants.CacheKey.HOLIDAY + DateUtil.thisYear());
if (CollectionUtils.isEmpty(holidayList)) {
Holiday.cacheToMiddleware(redisService);
}
boolean b = holidayList.stream().anyMatch(a -> a.equals(DateUtil.today()));
return !b;
}
return true;
}
}
方式二:手动维护版
https://gitee.com/kanen/weekday-util/blob/master/years/2022.txt