1821349743@qq.com
2023-02-20 146b951e36b7529f4aa9671035a657f651762edc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package com.product.administration.util;
 
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
/**
 *     考勤工具类
* Copyright  PRODUCT-BASE
* @Title: PRODUCT-BASE-
* @Project: WorkDayUtil
* @Date: 2021年8月9日 下午5:58:48
* @Author: 杜洪波
* @Description:
 */
public class WorkDayUtil {
 
    /**
     *     获取每月的天数
     * @param yearNum
     * @param monthNum
     * @return
     */
    public static Integer getTotalDays(Integer yearNum, Integer monthNum) {
        Integer totalDays;
        switch (monthNum) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            totalDays=31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            totalDays=30;
            break;
        default:
            if (yearNum/4==0) {
                totalDays=29;
            }else
                totalDays=28;
            break;
        }
        return totalDays;
    }
    
    /**
     * 字符串转时间
     *
     * @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);
        // 月份数据集合
        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();
    }
}