Java计算每月工作天数

本代码经测试完全可以使用,仅提供demo做参考



import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

	public static void main(String[] args) {

        System.out.println("2020年01月上班天数:" + queryMonthDay("2020-01-10"));
        System.out.println("2020年02月上班天数:" + queryMonthDay("2020-02-10"));
        System.out.println("2020年03月上班天数:" + queryMonthDay("2020-03-10"));
        System.out.println("2020年04月上班天数:" + queryMonthDay("2020-04-10"));
        System.out.println("2020年05月上班天数:" + queryMonthDay("2020-05-10"));
        System.out.println("2020年06月上班天数:" + queryMonthDay("2020-06-10"));
        System.out.println("2020年07月上班天数:" + queryMonthDay("2020-07-10"));
        System.out.println("2020年08月上班天数:" + queryMonthDay("2020-08-10"));
        System.out.println("2020年09月上班天数:" + queryMonthDay("2020-09-10"));
        System.out.println("2020年10月上班天数:" + queryMonthDay("2020-10-10"));
        System.out.println("2020年11月上班天数:" + queryMonthDay("2020-11-10"));
        System.out.println("2020年12月上班天数:" + queryMonthDay("2020-12-10"));

    }


	/**
     * 字符串转时间
     *
     * @param dateStr
     * @param index
     * @return
     */
    public static Date StringToDate(String dateStr, int index) {
        DateFormat df = null;
        try {
            df = new SimpleDateFormat(dateFormat[index]);
            return df.parse(dateStr);
        } catch (Exception aioe) {
            return null;
        }
    }

    /**
     * 定义常见的时间格式
     */
    private static String[] dateFormat = {
            "yyyy-MM-dd HH:mm:ss", // 0
            "yyyy/MM/dd HH:mm:ss", // 1
            "yyyy年MM月dd日HH时mm分ss秒", // 2
            "yyyy-MM-dd", // 3
            "yyyy/MM/dd", // 4
            "yy-MM-dd", // 5
            "yy/MM/dd", // 6
            "yyyy年MM月dd日", // 7
            "HH:mm:ss", // 8
            "yyyyMMddHHmmss", // 9
            "yyyyMMdd", // 10
            "yyyy.MM.dd", // 11
            "yy.MM.dd", // 12
            "MM月dd日HH时mm分", // 13
            "yyyy年MM月dd日 HH:mm:ss", // 14
            "yyyy-MM-dd HH:mm", // 15
            "yyMMdd" // 16
    };


    /**
     * 获取法定节假日或者调休
     *
     * @param num
     * @return
     */
    public static List<String> holiday(int num) {
        if (num == 2) {
            return Arrays.asList(holiday2);
        } else {
            return Arrays.asList(holiday1);
        }
    }

    /**
     * 去重
     *
     * @param str
     * @return
     */
    public static List<String> removal(List<String> str) {
        Set<String> s = new HashSet<String>(str);
        str.clear();
        str.addAll(s);
        return str;
    }


    // 假期
    public static String[] holiday1 = {
            "2020-01-01", // 元旦
            "2020-01-24", // 春节
            "2020-01-25", // 春节
            "2020-01-26", // 春节
            "2020-01-27", // 春节
            "2020-01-28", // 春节
            "2020-01-29", // 春节
            "2020-01-30", // 春节
            "2020-04-04", // 清明节
            "2020-04-05", // 清明节
            "2020-04-06", // 清明节
            "2020-05-01", // 劳动节
            "2020-05-02", // 劳动节
            "2020-05-03", // 劳动节
            "2020-05-04", // 端午节
            "2020-05-05", // 端午节
            "2020-06-25", // 端午节
            "2020-06-26", // 中秋节
            "2020-06-27", // 中秋节
            "2020-10-08", // 中秋节
            "2020-10-01", // 国庆节
            "2020-10-02", // 国庆节
            "2020-10-03", // 国庆节
            "2020-10-04", // 国庆节
            "2020-10-05", // 国庆节
            "2020-10-06", // 国庆节
            "2020-10-07", // 国庆节
            "2021-01-01" // 元旦
    };

    // 调休
    public static String[] holiday2 = {
            "2020-01-19", // 春节_调休
            "2020-02-01", // 春节_调休
            "2020-04-26", // 清明_节调休
            "2020-05-09", // 劳动_节调休
            "2020-06-28", // 国庆节_调休
            "2020-09-27", // 国庆节_调休
            "2020-10-10"  // 元旦_调休
    };


    /**
     * 查询每个月的工作时间(天)
     *
     * @return
     */
    public static int queryMonthDay(String day) {
        List<String> result = new ArrayList<String>();
        Calendar Day = Calendar.getInstance();
        Day.setTime(StringToDate(day, 3));
        // 获取该月天数
        int dayNum = Day.getActualMaximum(Calendar.DAY_OF_MONTH);
        // 月开始
        String st = day.substring(0, 8) + "01";
        // 月结束
        String en = day.substring(0, 8) + dayNum;
        // 月份数据集合
        for (int i = 1; i < dayNum + 1; i++) {
            String d = day.substring(0, 8) + ((i < 10) ? "0" + i : "" + i);
            // 去掉周末
            Calendar startDay = Calendar.getInstance();
            startDay.setTime(StringToDate(d, 3));
            int week = startDay.get(Calendar.DAY_OF_WEEK);
            if (7 != week && 1 != week) {
                result.add(d);
            }
        }

        // 获取法定节假日
        List<String> fdList = holiday(1);
        // 添加时间段中间应该上班的时间
        // 去除中间所有的法定假期
        result.removeAll(fdList);
        // 去重
        result = removal(result);
        return result.size();
    }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值