服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - java计算工作时间除去节假日以及双休日

java计算工作时间除去节假日以及双休日

2021-05-07 13:05旋转的钢笔 Java教程

这篇文章主要为大家详细介绍了java计算工作时间除去节假日以及双休日的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java计算工作时间的具体代码,不包括节假日、双休日,供大家参考,具体内容如下

?
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package common.util;
import java.text.dateformat;
import java.text.parseexception;
import java.text.simpledateformat;
import java.util.arraylist;
import java.util.calendar;
import java.util.date;
import java.util.linkedlist;
import java.util.list;
 
public class calculatehours {
  simpledateformat format = new simpledateformat( "yyyy-mm-dd hh:mm:ss"); //这里的格式可以自己设置 
  //设置上班时间:该处时间可以根据实际情况进行调整
  int abh = 9;//上午上班时间,小时
  int abm = 00;//上午上班时间,分钟
  int aeh = 12;//上午下班时间,小时
  int aem = 0;//上午下班时间,分钟
  int pbh = 13;//下午上班时间,小时
  int pbm = 00;//下午上班时间,分钟
  int peh = 18;//下午下班时间,小时
  int pem = 0;//下午下班时间,分钟
  float h1 = abh+(float)abm/60;
  float h2 = aeh+(float)aem/60;
  float h3 = pbh+(float)pbm/60;
  float h4 = peh+(float)pem/60;
  float hoursperday = h2-h1+(h4-h3);//每天上班小时数
  int daysperweek = 5;//每周工作天数
  long milsecperday = 1000*60*60*24;//每天的毫秒数
  float hoursperweek = hoursperday*daysperweek;//每星期小时数
  public float calculatehours(string begintime, string endtime){
    //对输入的字符串形式的时间进行转换
    date t1 = stringtodate(begintime);//真实开始时间
    date t2 = stringtodate(endtime);//真实结束时间
    //对时间进行预处理
    t1 = processbegintime(t1);
    t2 = processendtime(t2);
    //若开始时间晚于结束时间,返回0
    if(t1.gettime()>t2.gettime()){
      return 0;
    }
    //开始时间到结束时间的完整星期数
    int weekcount = (int) ((t2.gettime()-t1.gettime())/(milsecperday*7));
    float totalhours = 0;
    totalhours += weekcount * hoursperweek;
    //调整结束时间,使开始时间和结束时间在一个星期的周期之内
    t2.settime(t2.gettime()-weekcount*7*milsecperday);
    int daycounts = 0;//记录开始时间和结束时间之间工作日天数
    //调整开始时间,使得开始时间和结束时间在同一天,或者相邻的工作日内。
    while(t1.gettime()<=t2.gettime()){
      date temp = new date(t1.gettime()+milsecperday);
      temp = processbegintime(temp);
      temp.sethours(t1.gethours());
      temp.setminutes(t1.getminutes());
      if(temp.gettime()>t2.gettime()){
        break;
      }else{
        t1 = temp;
        daycounts++;
      }
    }
    totalhours += daycounts * hoursperday;
    float hh1 = t1.gethours() + (float)t1.getminutes()/60;
    float hh2 = t2.gethours() + (float)t2.getminutes()/60;
    //处理开始结束是同一天的情况
    if(t1.getday()==t2.getday()){
      float tt = 0;
      tt = hh2 - hh1;
      if(hh1>=h1&&hh1<=h2&&hh2>=h3){
        tt = tt - (h3-h2);
      }
      totalhours += tt;
    }else{
      //处理开始结束不是同一天的情况
      float tt1 = h4 - hh1;
      float tt2 = hh2 - h1;
      if(hh1<=h2){
        tt1 = tt1 - (h3-h2);
      }
      if(hh2>=h3){
        tt2 = tt2 - (h3-h2);
      }
      totalhours += (tt1 + tt2);
    }
    return totalhours;
  }
 
  /**
   * 格式化输出时间: yyyy-mm-dd hh:mm:ss 星期x
   * @param t
   * @return
   */
  private string printdate(date t) {
    string str;
    string xingqi = null;
    switch (t.getday()) {
    case 0:
      xingqi = "星期天";
      break;
    case 1:
      xingqi = "星期一";
      break;
    case 2:
      xingqi = "星期二";
      break;
    case 3:
      xingqi = "星期三";
      break;
    case 4:
      xingqi = "星期四";
      break;
    case 5:
      xingqi = "星期五";
      break;
    case 6:
      xingqi = "星期六";
      break;
    default:
      break;
    }
    str = format.format(t)+" "+xingqi;
    return str;
  }
 
  /**
   * 对结束时间进行预处理,使其处于工作日内的工作时间段内
   * @param t
   * @return
   */
  private date processendtime(date t) {
    float h = t.gethours() + (float)t.getminutes()/60;
    //若结束时间晚于下午下班时间,将其设置为下午下班时间
    if(h>=h4){
      t.sethours(peh);
      t.setminutes(pem);
    }else {
      //若结束时间介于中午休息时间,那么设置为上午下班时间
      if(h>=h2&&h<=h3){
        t.sethours(aeh);
        t.setminutes(aem);
      }else{
        //若结束时间早于上午上班时间,日期向前推一天,并将时间设置为下午下班时间
        if(t.gethours()<=h1){
          t.settime(t.gettime()-milsecperday);
          t.sethours(peh);
          t.setminutes(pem);
        }
      }
    }
    //若结束时间是周末,那么将结束时间向前推移到最近的工作日的下午下班时间
    if(t.getday()==0){
      t.settime(t.gettime()-milsecperday*(t.getday()==6?1:2));
      t.sethours(peh);
      t.setminutes(pem);
    }
    if(t.getday()==6){
      t.settime(t.gettime()-milsecperday*(t.getday()==6?1:2));
      t.sethours(peh);
      t.setminutes(pem);
    }
    return t;
  }
  /**
   * 对开始时间进行预处理
   * @param t1
   * @return
   */
  private date processbegintime(date t) {
    float h = t.gethours() + (float)t.getminutes()/60;
    //若开始时间晚于下午下班时间,将开始时间向后推一天
    if(h>=h4){
      t.settime(t.gettime()+milsecperday);
      t.sethours(abh);
      t.setminutes(abm);
    }else {
      //若开始时间介于中午休息时间,那么设置为下午上班时间
      if(h>=h2&&h<=h3){
        t.sethours(pbh);
        t.setminutes(pbm);
      }else{
        //若开始时间早于上午上班时间,将hour设置为上午上班时间
        if(t.gethours()<=h1){
          t.sethours(abh);
          t.setminutes(abm);
        }
      }
    }
    //若开始时间是周末,那么将开始时间向后推移到最近的工作日的上午上班时间
    if(t.getday()==0){
      t.settime(t.gettime()+milsecperday*(t.getday()==6?2:1));
      t.sethours(abh);
      t.setminutes(abm);
    }if(t.getday()==6){
      t.settime(t.gettime()+milsecperday*(t.getday()==6?2:1));
      t.sethours(abh);
      t.setminutes(abm);
    }
    return t;
  }
 
  /**
   * 将字符串形式的时间转换成date形式的时间
   * @param time
   * @return
   */
  private date stringtodate(string time){
    try {
      return format.parse(time);
    } catch (parseexception e) {
      e.printstacktrace();
      return null;
    }
  }
  /**
   * 去除周末节假日工作小时
   * @param begintime
   * @param endtime
   * @return
   * @throws parseexception
   */
  public static float calculatehour(string begintime,string endtime) throws parseexception{
    calculatehours ch = new calculatehours();
    float a=ch.calculatehours(begintime, endtime); 
    calendar startday = calendar.getinstance();
     calendar endday = calendar.getinstance();
     startday.settime(formatter.parse(begintime));
     endday.settime(formatter.parse(endtime));
    string[] workday=printday(startday, endday);
    string[] holiday = new string[]{"01-01","01-02","01-03","05-01","05-02","05-03","10-01","10-02",
        "10-03","10-04","10-05","10-06","02-08","02-09","02-10"};
    calendar now = calendar.getinstance(); 
    int year=now.get(calendar.year); //获取当前年份
    list<string> list = new arraylist<string>();
    for (string string : holiday) {
      string=year+"-"+string;
      list.add(string);
    }
    string[] arr = list.toarray(new string[0]);
    int holidays = arrcontrast(workday, arr); 
    int holidayhous=holidays*8;
    float b  = (float)(math.round(a*10))/10;
    float workhours=b-holidayhous;
    return workhours;
  }
  public static void main(string[] args) throws parseexception {
    string begintime = "2018-6-1 9:00:00"
    string endtime = "2018-6-4 10:10:00"
    calculatehours ch = new calculatehours();
    float b=ch.calculatehours(begintime, endtime);
    system.out.println(b);
    float a=calculatehours.calculatehour(begintime, endtime);
    system.out.println(a);
  }
  /**
   * 去除数组中相同日期
   * @param arr1
   * @param arr2
   * @return
   */
  private static int arrcontrast(string[] arr1, string[] arr2){
    int count=0;
    list<string> list = new linkedlist<string>();
    for (string str : arr1) { //处理第一个数组,list里面的值为1,2,3,4
      if (!list.contains(str)) {
      list.add(str);
      }
    }
    for (string str : arr2) { //如果第二个数组存在和第一个数组相同的值,就删除
      if(list.contains(str)){
      list.remove(str);
      ++count;
      }
    }
    return count;
  }
  private static final dateformat formatter = new simpledateformat("yyyy-mm-dd");
  private static string[] printday(calendar startday, calendar endday) {
     list<string> list = new arraylist<string>();
     // 给出的日期开始日比终了日大则不执行打印
     if (startday.compareto(endday) >= 0) {
      return new string[]{};
     }
   // 现在打印中的日期
   calendar currentprintday = startday;
   while (true) {
    // 日期加一
    currentprintday.add(calendar.date, 1);
    // 日期加一后判断是否达到终了日,达到则终止打印
    if (currentprintday.compareto(endday) == 0) {
    break;
    }
    list.add(formatter.format(currentprintday.gettime()));
   }
    string[] arr = list.toarray(new string[0]);
    return arr;
   }
}

main方法中的执行结果为:

java计算工作时间除去节假日以及双休日

代码中都有注释,可自行根据需要进行调节。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/apbbbbb/article/details/80570053

延伸 · 阅读

精彩推荐