本文實(shí)例為大家分享了java計(jì)算工作時(shí)間的具體代碼,不包括節(jié)假日、雙休日,供大家參考,具體內(nèi)容如下
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" ); //這里的格式可以自己設(shè)置 //設(shè)置上班時(shí)間:該處時(shí)間可以根據(jù)實(shí)際情況進(jìn)行調(diào)整 int abh = 9 ; //上午上班時(shí)間,小時(shí) int abm = 00 ; //上午上班時(shí)間,分鐘 int aeh = 12 ; //上午下班時(shí)間,小時(shí) int aem = 0 ; //上午下班時(shí)間,分鐘 int pbh = 13 ; //下午上班時(shí)間,小時(shí) int pbm = 00 ; //下午上班時(shí)間,分鐘 int peh = 18 ; //下午下班時(shí)間,小時(shí) int pem = 0 ; //下午下班時(shí)間,分鐘 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); //每天上班小時(shí)數(shù) int daysperweek = 5 ; //每周工作天數(shù) long milsecperday = 1000 * 60 * 60 * 24 ; //每天的毫秒數(shù) float hoursperweek = hoursperday*daysperweek; //每星期小時(shí)數(shù) public float calculatehours(string begintime, string endtime){ //對(duì)輸入的字符串形式的時(shí)間進(jìn)行轉(zhuǎn)換 date t1 = stringtodate(begintime); //真實(shí)開始時(shí)間 date t2 = stringtodate(endtime); //真實(shí)結(jié)束時(shí)間 //對(duì)時(shí)間進(jìn)行預(yù)處理 t1 = processbegintime(t1); t2 = processendtime(t2); //若開始時(shí)間晚于結(jié)束時(shí)間,返回0 if (t1.gettime()>t2.gettime()){ return 0 ; } //開始時(shí)間到結(jié)束時(shí)間的完整星期數(shù) int weekcount = ( int ) ((t2.gettime()-t1.gettime())/(milsecperday* 7 )); float totalhours = 0 ; totalhours += weekcount * hoursperweek; //調(diào)整結(jié)束時(shí)間,使開始時(shí)間和結(jié)束時(shí)間在一個(gè)星期的周期之內(nèi) t2.settime(t2.gettime()-weekcount* 7 *milsecperday); int daycounts = 0 ; //記錄開始時(shí)間和結(jié)束時(shí)間之間工作日天數(shù) //調(diào)整開始時(shí)間,使得開始時(shí)間和結(jié)束時(shí)間在同一天,或者相鄰的工作日內(nèi)。 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 ; //處理開始結(jié)束是同一天的情況 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 { //處理開始結(jié)束不是同一天的情況 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; } /** * 格式化輸出時(shí)間: 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; } /** * 對(duì)結(jié)束時(shí)間進(jìn)行預(yù)處理,使其處于工作日內(nèi)的工作時(shí)間段內(nèi) * @param t * @return */ private date processendtime(date t) { float h = t.gethours() + ( float )t.getminutes()/ 60 ; //若結(jié)束時(shí)間晚于下午下班時(shí)間,將其設(shè)置為下午下班時(shí)間 if (h>=h4){ t.sethours(peh); t.setminutes(pem); } else { //若結(jié)束時(shí)間介于中午休息時(shí)間,那么設(shè)置為上午下班時(shí)間 if (h>=h2&&h<=h3){ t.sethours(aeh); t.setminutes(aem); } else { //若結(jié)束時(shí)間早于上午上班時(shí)間,日期向前推一天,并將時(shí)間設(shè)置為下午下班時(shí)間 if (t.gethours()<=h1){ t.settime(t.gettime()-milsecperday); t.sethours(peh); t.setminutes(pem); } } } //若結(jié)束時(shí)間是周末,那么將結(jié)束時(shí)間向前推移到最近的工作日的下午下班時(shí)間 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; } /** * 對(duì)開始時(shí)間進(jìn)行預(yù)處理 * @param t1 * @return */ private date processbegintime(date t) { float h = t.gethours() + ( float )t.getminutes()/ 60 ; //若開始時(shí)間晚于下午下班時(shí)間,將開始時(shí)間向后推一天 if (h>=h4){ t.settime(t.gettime()+milsecperday); t.sethours(abh); t.setminutes(abm); } else { //若開始時(shí)間介于中午休息時(shí)間,那么設(shè)置為下午上班時(shí)間 if (h>=h2&&h<=h3){ t.sethours(pbh); t.setminutes(pbm); } else { //若開始時(shí)間早于上午上班時(shí)間,將hour設(shè)置為上午上班時(shí)間 if (t.gethours()<=h1){ t.sethours(abh); t.setminutes(abm); } } } //若開始時(shí)間是周末,那么將開始時(shí)間向后推移到最近的工作日的上午上班時(shí)間 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; } /** * 將字符串形式的時(shí)間轉(zhuǎn)換成date形式的時(shí)間 * @param time * @return */ private date stringtodate(string time){ try { return format.parse(time); } catch (parseexception e) { e.printstacktrace(); return null ; } } /** * 去除周末節(jié)假日工作小時(shí) * @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); //獲取當(dāng)前年份 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); } /** * 去除數(shù)組中相同日期 * @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) { //處理第一個(gè)數(shù)組,list里面的值為1,2,3,4 if (!list.contains(str)) { list.add(str); } } for (string str : arr2) { //如果第二個(gè)數(shù)組存在和第一個(gè)數(shù)組相同的值,就刪除 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>(); // 給出的日期開始日比終了日大則不執(zhí)行打印 if (startday.compareto(endday) >= 0 ) { return new string[]{}; } // 現(xiàn)在打印中的日期 calendar currentprintday = startday; while ( true ) { // 日期加一 currentprintday.add(calendar.date, 1 ); // 日期加一后判斷是否達(dá)到終了日,達(dá)到則終止打印 if (currentprintday.compareto(endday) == 0 ) { break ; } list.add(formatter.format(currentprintday.gettime())); } string[] arr = list.toarray( new string[ 0 ]); return arr; } } |
main方法中的執(zhí)行結(jié)果為:
代碼中都有注釋,可自行根據(jù)需要進(jìn)行調(diào)節(jié)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/apbbbbb/article/details/80570053