欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

获取当月工作日数

程序员文章站 2022-05-17 21:40:00
...

项目中要计算当月工作日数,看了别人的代码,觉得不太好,所以稍加改动,没有太多技术含量,难点在于找免费好用的api,所以记录一下

代码中用到了hutool包、lombok,另外api 接口限额为 10000次/IP,所以用了redis缓存


package cn.edu.oa;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import cn.micro.framework.spring.SpringContextUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

@Slf4j
@Component
public class HolidayUtil {


    /**
     * todo 待优化
     * 判断该日期是否为节假日
     *
     * @param date 1,正常工作日  2,节假日 3,节假日后补班 4,周末
     * @return
     */
    public static int getIfHoliday(String date){
        int flag=1;
        try {
            String post = HttpUtil.get("http://timor.tech/api/holiday/ " + date);
            Map map = JSON.parseObject(JSON.toJSONString(post), Map.class);
            if (map.get("holiday") == null) {
                flag = 1;
            } else {
                Map<String, Object> holidayDO = (Map) map.get("holiday");
                Boolean object = (Boolean) holidayDO.get("holiday");
                if (object) {
                    flag = 2;//节假日
                } else {
                    flag = 3;//节假日补班
                }
            }
            LocalDate localDate = LocalDate.parse(date);
            int value = localDate.getDayOfWeek().getValue();
            if(value==6 ||value==7){
                if(flag!=3){//如果不是节假日补班情况,则返回周末
                    flag=4;//周末
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            log.info("获取是否工作日异常.",e);
        }
        return flag;
    }



    /**
     * 获取当月的所有工作日
     * @param year
     * @param month
     * @return
     */
    public static int getWorkdayInMonth(String year, String month) {
        SimpleDateFormat mm_dd_sdf = new SimpleDateFormat("MM-dd");
        SimpleDateFormat yyyy_mm_sdf = new SimpleDateFormat("yyyy-MM");

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, Integer.parseInt(year));// 不设置的话默认为当年
        calendar.set(Calendar.MONTH, Integer.parseInt(month) - 1);// 设置月份
        calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为当月第一天
        int actualMaximum = calendar.getActualMaximum(Calendar.DATE);//当月天数

        //获取年月,传入api
        String yyyy_mm = yyyy_mm_sdf.format(calendar.getTime());
        //当月休假情况
        HolidayBean bean = SpringContextUtils.getApplicationContext().getBean(HolidayBean.class);
        JSONObject holidayByMonth = bean.getHolidayByMonth(yyyy_mm);

        //当月首日
        DateTime beginOfMonth = DateUtil.beginOfMonth(calendar.getTime());

        int workDayNum = 0;
        for (int i = 0; i < actualMaximum; i++) {
            Date date = DateUtils.addDays(beginOfMonth, i);
            String mm_dd = mm_dd_sdf.format(date);

            JSONObject jsonObject = holidayByMonth.getJSONObject(mm_dd);
            if (jsonObject != null) {
                if (Boolean.FALSE.equals(jsonObject.getBoolean("holiday"))) {
                    workDayNum += 1;
                }
                continue;
            }

            Calendar instance = Calendar.getInstance();
            instance.setTime(date);
            int dayOfWeek = instance.get(Calendar.DAY_OF_WEEK);

            if (dayOfWeek > 1 && dayOfWeek < 7) {
                workDayNum += 1;
            }
        }
        return workDayNum;

    }


    @Component
    public class HolidayBean{

        /**
         * 调用api获取当月节假日情况,接口限额10000次,所以缓存起来
         * @param month
         * @return
         */
        @Cacheable(value = "d_oa_holiday", key = "#month")
        public JSONObject getHolidayByMonth(String month) {
            String post = HttpUtil.get("http://timor.tech/api/holiday/year/" + month);
            JSONObject jsonObject = JSON.parseObject(post);
            JSONObject holiday = jsonObject.getJSONObject("holiday");
            return holiday;
        }
    }


}