栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

判断工作日的工具类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

判断工作日的工具类

可以判断给定日期是否是工作日,以及两个日期间的工作日天数;该工具类依赖于Redis数据库,需要将整个年份的假期时间表存放到Redis中;假期时间表依赖于第三方接口。

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.beicai.compliance.util.redis.RedisUtil;
import org.elasticsearch.common.Strings;
import org.springframework.util.CollectionUtils;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.*;


public class HolidayUtil {

    // redis key格式前缀 -> calendar:workOrHolidays:year:month:day: + 1/2   1工作日 2假日
    private static final String CALENDAR_WORKORHOLIDAYS = "calendar:workOrHolidays:";

    private HolidayUtil() {
    }

    
    public static boolean isWeekend() {
        return isWeekend(LocalDate.now());
    }

    
    public static boolean isWeekend(LocalDate date) {
        DayOfWeek dayOfWeek = date.getDayOfWeek();
        return DayOfWeek.SATURDAY.equals(dayOfWeek) || DayOfWeek.SUNDAY.equals(dayOfWeek);
    }

    
    public static boolean isHolidaysAndFestivals() {
        return !isWorkday();
    }

    
    public static boolean isHolidaysAndFestivals(LocalDate date) {
        return !isWorkday(date);
    }

    
    public static boolean isWorkday() {
        return isWorkday(LocalDate.now());
    }

    
    public static boolean isWorkday(LocalDate date) {
        if (date == null) {
            return false;
        }
        String[] split = date.toString().split("-");
        String s = RedisUtil.get(CALENDAR_WORKORHOLIDAYS + split[0] + ":" + split[1] + ":" + split[2]);
        if (s == null) {
            saveCalendarToRedis(date);
            s = RedisUtil.get(CALENDAR_WORKORHOLIDAYS + date.getYear() + ":" + date.getMonth() + ":" + date.getDayOfMonth());
        }
        return Objects.equals(s, "1");
    }

    // 拉取第三方假期时间表
    private static void saveCalendarToRedis(LocalDate date) {
        String url = "https://api.apihubs.cn/holiday/get?field=date,workday&year=" + date.getYear() + "&size=366";
        String body = HttpRequest.get(url).execute().body();
        HashMap bodyResult =
                JSON.parseObject(body, new TypeReference>() {});
        HashMap dataResult =
                JSON.parseObject(bodyResult.get("data"), new TypeReference>() {});
        List> result =
                JSON.parseObject(dataResult.get("list"), new TypeReference>>() {});
        if (!CollectionUtils.isEmpty(result)) {
            for (Map map : result) {
                String key = CALENDAR_WORKORHOLIDAYS + Strings.substring(map.get("date"), 0, 4) + ":" +
                        Strings.substring(map.get("date"), 4, 6) + ":" + Strings.substring(map.get("date"), 6, 8);
                RedisUtil.put(key, map.get("workday"));
            }
        }
    }

    
    public static Integer getDifference(LocalDate before, LocalDate after) {
        if (before == null || after == null) {
            return null;
        }
        Integer difference = 0;
        while (!before.isAfter(after)) {
            if (isWorkday(before)) {
                ++ difference;
            }
            before = before.plusDays(1);
        }
        return difference;
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/825569.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号