前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >判断当前时间是否是法定节假日或工作日

判断当前时间是否是法定节假日或工作日

作者头像
时间静止不是简史
发布2022-05-06 16:08:17
5.9K0
发布2022-05-06 16:08:17
举报
文章被收录于专栏:Java探索之路

判断当前时间是否是法定节假日或工作日

一、介绍

  • 采用语言: Java
  • 基于内网下采取配置文件的方式, 来判断当前是否是节假日(包括周末和调休上班日)
  • 如果基于外网取请见 这里
  • 工具类实现思路: 将国家法定节假日和调休上班日的日期写入文本中, 然后读取该文本, 将加班日和节假日分别放入一个list中 然后将当前毫秒数转成当前日期(年月日), 然后依次判断是否是法定节假日, 调休加班日和周末
  • 计算逻辑: 法定工作日= 调休加班日 + 非法定节假日 + 平时工作日(周1~5) 法定休息日= 非法定工作日 法定节假日= 不在法定节假日list中

二、实现

采取文件进行配置的初衷是: 解耦. 与其将节假日等相关日期的写入放入代码中, 不如将日期相关配置独立出来, 方便配置的同时对代码进行解耦

resources 目录下(与application.properties配置文件同级)新建一个文件 holiday.txt

存放当年的法定节假日和调休加班日等日期, 例如今年的配置

代码语言:javascript
复制
#######法定节假日日期#######
2022-01-01
2022-01-02
2022-01-03
2022-01-31
2022-02-01
2022-02-02
2022-02-03
2022-02-04
2022-02-05
2022-02-06
2022-04-03
2022-04-04
2022-04-05
2022-04-30
2022-05-01
2022-05-02
2022-05-03
2022-05-04
2022-06-03
2022-06-04
2022-06-05
2022-09-10
2022-09-11
2022-09-12
2022-10-01
2022-10-02
2022-10-03
2022-10-04
2022-10-05
2022-10-06
2022-10-07
2023-01-01
2023-01-02
#######调休加班日期#######
2022-01-29
2022-01-30
2022-04-02
2022-04-24
2022-05-07
2022-10-08
2022-10-09
  1. 每年大概10月份左右会出现第二年的法定节假日公告 点击 这里 查看当年法定节假日安排
  2. 工具类代码
代码语言:javascript
复制
import org.apache.commons.io.FileUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * info: 调用API接口判断日期是否是工作日 周末还是节假日
 *
 * @Author caoHaiYang
 * {@Location 配置文件位于: /resources/holiday.txt}
 */
public class HolidayUtils {
    /**
     * 法定节假日
     */
    static List<String> holiday = new ArrayList<>();
    /**
     * 调休上班日
     */
    static List<String> extraWorkDay = new ArrayList<>();
    /**
     * 法定节假日标识字符串
     */
    private static final String HOLIDAY_FLAG_STRING = "#######法定节假日日期#######";
    /**
     * 调休加班标识字符串
     */
    private static final String EXTRAWORKDAY_FLAG_STRING = "#######调休加班日期#######";

    /**
     * 初始化节假日和调休
     */
    public static void initHolidayAndExtraWorkDay() throws IOException {
        //方式二 利用FileUtils将ClassPathResource.getInputStream 得到的输入流复制到临时文件中
        Resource resource = new ClassPathResource("holiday.txt");
        InputStream inputStream = resource.getInputStream();
        File tempFile = File.createTempFile("temp", ".txt");
        FileUtils.copyInputStreamToFile(inputStream, tempFile);
        String s = FileUtils.readFileToString(tempFile, StandardCharsets.UTF_8);

        String[] split = s.split("\r\n");
        //方式一, 手动编写文件读取方法
        //List<String> list = readTxtFile("holiday.txt");
        List<String> list = Arrays.asList(split);
        //定义起始下标
        int startIndex = 0;
        //定义结束下标
        int endIndex = 0;
        startIndex = list.indexOf(HOLIDAY_FLAG_STRING);
        endIndex = list.indexOf(EXTRAWORKDAY_FLAG_STRING);
        List<String> holidayList = list.subList(startIndex + 1, endIndex);
        List<String> initExtraWorkDay = list.subList(endIndex + 1, list.size());
        //初始化节假日
        holiday.addAll(holidayList);
        //初始化额外加班日
        extraWorkDay.addAll(initExtraWorkDay);
    }

    /**
     * 判断是否是工作日
     * 法定工作日: 调休加班日 + 非法定节假日 + 平时工作日(周1~5)
     *
     * @param time 当前时间(毫秒数)
     * @return true: 工作日, false: 节假日
     * @throws IOException
     */
    public static Boolean isWorkingDay(long time) throws IOException {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneOffset.of("+8"));
        String formatTime = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        initHolidayAndExtraWorkDay();
        //是否加班日
        if (extraWorkDay.contains(formatTime)) {
            return true;
        }
        //是否节假日
        if (holiday.contains(formatTime)) {
            return false;
        }
        //如果是1-5表示周一到周五  是工作日
        DayOfWeek week = dateTime.getDayOfWeek();
        if (week == DayOfWeek.SATURDAY || week == DayOfWeek.SUNDAY) {
            return false;
        }
        return true;
    }

    /**
     * 判断是否是法定休息日
     * 法定休息日:非法定工作日
     *
     * @param time 当前时间(毫秒数)
     * @return true: 休息日, false: 工作日
     * @throws IOException
     */
    public static Boolean isOffDay(long time) throws IOException {
        return !isWorkingDay(time);
    }

    /**
     * 判断是否是法定节假日
     *
     * @param time 当前时间(毫秒数)
     * @return true: 节假日, false: 非节假日
     * @throws IOException
     */
    public static Boolean isHoliday(long time) throws IOException {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneOffset.of("+8"));
        String formatTime = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        initHolidayAndExtraWorkDay();
        //是否节假日
        if (holiday.contains(formatTime)) {
            return false;
        }
        return true;
    }

    /**
     * Java读取txt文件的内容
     *
     * @param fileName resources目录下文件名称
     * @return 转为String
     */

    public static List<String> readTxtFile(String fileName) {
        List<String> listContent = new ArrayList<>();
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        String encoding = "utf-8";
        try {
            Resource resource = new ClassPathResource(fileName);
            is = resource.getInputStream();
            isr = new InputStreamReader(is, encoding);
            br = new BufferedReader(isr);
            String lineTxt = null;
            while ((lineTxt = br.readLine()) != null) {
                listContent.add(lineTxt);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
                isr.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return listContent;
    }


    //测试工具类方法
    public static void main(String[] args) throws IOException, ParseException {
        //当前时间
        long currentTimeStamp = LocalDateTime.now().toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        Boolean workingDay = isWorkingDay(currentTimeStamp);

        if (workingDay) {
            System.out.println("工作日,加油,打工人");
        } else {
            System.out.println("休息日, 放假了, 兄弟萌");
        }

    }
}	

ps: 如果使用当前方式进行配置后, 如果在打包后运行的jar 仍提示 File 'holiday.txt' does not exist , 可以参照 这里 解决

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-04-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 判断当前时间是否是法定节假日或工作日
  • 一、介绍
  • 二、实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档