时间操作工具类

最近实现一个需求,做个人脸识别考勤报表的功能,接收apk人脸的数据,我的编写主体还是在报表格式和迟到早退旷课这些难点。后续又提需求要加功能,可把我头给整烂。其中很多对日期的操作。。

主要还是去使用Date、SimpleDateFormat、Calendar这些操作时间。

关于操作时间的类https://www.cnblogs.com/mike-mei/p/9782346.html

报表我是整个格式都是自己弄出来摸索,其实jxl是有个方法可以直接吧某一个文件内容给取出来,这样就是这么个逻辑,一个模板文件,需要生成取这个文件。。这样弄更好。只是我的变化数据太多,就没考虑这个方案了。

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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
package com.vshow.control.peopleface.attendance;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.time.DateUtils;
import org.apache.poi.ss.formula.eval.StringValueEval;

import jxl.CellView;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;

import jxl.write.Alignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionSupport;
import com.vshow.control.data.AttendanceRecord;
import com.vshow.control.data.AttendanceUser;
import com.vshow.control.data.Client;
import com.vshow.control.data.User;
import com.vshow.control.tool.Constant;
import com.vshow.control.tool.Local;
import com.vshow.control.tool.SqlConnect;

public class ExportattendAction extends ActionSupport {

private String fname;
private String id;
private String btime;
private String etime;

public String getFname() {
return fname;
}

public void setFname(String fname) {
this.fname = fname;
}

@Override
public String execute() throws Exception {

SqlMapClient sm = SqlConnect.getSqlMapInstance();
ActionContext ctx = ActionContext.getContext();
Map session = ctx.getSession();
Local local = (Local) session.get("Local");
// 获取当前登录的用户
User user = (User) sm.queryForObject("sel_user_all_id",
session.get("urid"));
/* // 动态获得上个月30天到这个月1号
Calendar calendar = Calendar.getInstance();

int year = calendar.get(Calendar.YEAR);
int currentMoth = calendar.get(Calendar.MONTH) + 1;// 当前月份
// 获得上个月份
int beforeMoth;
if (currentMoth - 1 == 0) {
beforeMoth = 12;
} else {
beforeMoth = currentMoth - 1;
}
String btime = getFirstDay(year, beforeMoth);
String etime = gettwoDay(year, currentMoth);
// 获得上个月总天数201
*/ // 获取数据
List<AttendanceUser> acuserList;
if (id != null&&!id.equals("")) {
acuserList = (List<AttendanceUser>) sm.queryForList(
"sel_attendanceuser_id", id);
} else {
acuserList = (List<AttendanceUser>) sm
.queryForList("sel_attendanceuser_all",user.getId());
}

// *************************************构造报表模板****************************************************
//制作开始结束时间名字
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
Calendar calendarbtime = Calendar.getInstance();
Calendar calendaretime = Calendar.getInstance();
calendarbtime.setTime(sf.parse(btime));
calendaretime.setTime(sf.parse(etime));
String mothview =getDDTimeHandle(calendarbtime.get(Calendar.MONTH)+1)
+getDDTimeHandle(calendarbtime.get(Calendar.DAY_OF_MONTH))
+"_"+getDDTimeHandle(calendaretime.get(Calendar.MONTH)+1)
+getDDTimeHandle(calendaretime.get(Calendar.DAY_OF_MONTH));
int currentMothCount = caculateTotalTime(etime,btime)+1;//需要统计总天数
List<Date> currentMothList=findDates(btime,etime);//需要统计的日期
if (id == null||id.equals("")) {
fname = calendarbtime.get(Calendar.YEAR) + "_" + mothview + "_all" + ".xls";
} else {
fname = calendarbtime.get(Calendar.YEAR) + "_" + mothview + "_"
+ acuserList.get(0).getUserName() + ".xls";
}

String filename = Constant.SEE + File.separator + fname;
SimpleDateFormat formatfiles = new SimpleDateFormat("yyyy-MM-dd");
File f = new File(filename);
WritableWorkbook wwb = Workbook.createWorkbook(f);
WritableSheet ws = wwb.createSheet(Constant.LOCAL.getB00120(), 0); // 用户列表

// 设置单元格样式
WritableFont wf = new WritableFont(WritableFont.createFont("宋体"), 12,
WritableFont.NO_BOLD, false);
WritableFont zw = new WritableFont(WritableFont.createFont("宋体"), 11,
WritableFont.NO_BOLD, false);
WritableFont one = new WritableFont(WritableFont.createFont("宋体"), 20,
WritableFont.BOLD, false);
WritableCellFormat oneF = new WritableCellFormat(one);
WritableCellFormat wcfF = new WritableCellFormat(wf);

WritableCellFormat zwF = new WritableCellFormat(zw);

oneF.setAlignment(Alignment.CENTRE);// 设置左右对其

zwF.setVerticalAlignment(VerticalAlignment.CENTRE);

ws.addCell(new Label(0, 0, Constant.LOCAL.getD00081(), oneF)); // 第一行标题
ws.mergeCells(0, 0, currentMothCount+5, 0);
ws.addCell(new Label(0, 1, Constant.LOCAL.getD00082() + " " + btime + " ~ " + etime
+" "+Constant.LOCAL.getD00083() + " " + formatfiles.format(new Date()), zwF)); // 第二行信息
ws.mergeCells(0, 1, currentMothCount+5, 1);

WritableFont zwDate = new WritableFont(WritableFont.createFont("宋体"),
11, WritableFont.NO_BOLD, false);
WritableCellFormat zwfDateF = new WritableCellFormat(zwDate);
zwfDateF.setAlignment(Alignment.CENTRE);// 设置左右对其
// 动态生成日期行

CellView cv = new CellView();
cv.setSize(2200);
//获得俩个时间是否属于同一月份
boolean Thesamemonth=true;
if(calendarbtime.get(Calendar.MONTH)!=calendaretime.get(Calendar.MONTH))
{
Thesamemonth=false;
}

//获得时间差
String dateview="";
int maxnow;

//System.out.println(currentMothCount);
int maxdaybtime=getMaxDayByYearMonth(calendarbtime.get(Calendar.YEAR), calendarbtime.get(Calendar.MONTH));
String btimeMonthView=String.valueOf(calendarbtime.get(Calendar.MONTH)+1);
String etimeMonthView=String.valueOf(calendaretime.get(Calendar.MONTH)+1);
ws.addCell(new Label(0, 2, dateview, zwfDateF));
ws.setColumnView(0, cv);
for (int i = 0; i < currentMothCount; i++) {
maxnow=calendarbtime.get(Calendar.DATE)+i;
if(Thesamemonth){
dateview=btimeMonthView+"-"+getDDTimeHandle(calendarbtime.get(Calendar.DATE)+i);
}else
{
if(maxnow>maxdaybtime){
dateview=etimeMonthView+"-"+getDDTimeHandle(maxnow-maxdaybtime);
}else
{
dateview=btimeMonthView+"-"+getDDTimeHandle(calendarbtime.get(Calendar.DATE)+i);
}
}
ws.addCell(new Label(i+1, 2, dateview, zwfDateF)); // 第三行信息
ws.setColumnView(i+1, cv);
}

ws.addCell(new Label(currentMothCount+1, 2,Constant.LOCAL.getD00084(), zwfDateF));
ws.addCell(new Label(currentMothCount+3, 2, Constant.LOCAL.getD00085(), zwfDateF));
ws.mergeCells(currentMothCount+1,2,currentMothCount+2, 0);
ws.mergeCells(currentMothCount+3,2,currentMothCount+4, 0);
ws.setColumnView(currentMothCount, cv);

// ***********************************************循环获得筛选过滤数据****************************
//获得当前账号设置的考勤周期
String cycle=(String)sm.queryForObject("sel_userAttendanceCycle_userid",user.getId());

Long cdTime=Long.valueOf(cycle.split("-")[0].split(",")[0].replace(":",""));
Long ztTime=Long.valueOf(cycle.split("-")[0].split(",")[1].replace(":",""));
Long cdTime1=0L;
Long ztTime1=0L;
boolean cycleflay=true;
if(cycle.split("-").length==2)
{
cycleflay=false;
cdTime1=Long.valueOf(cycle.split("-")[1].split(",")[0].replace(":",""));
ztTime1=Long.valueOf(cycle.split("-")[1].split(",")[1].replace(":",""));
}
//String zwxbdate=cycle.split("-")[1].split(",")[0]+":00:00"
SimpleDateFormat formatfile = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat formatlast = new SimpleDateFormat("yyyy-MM-dd HH:mm");
SimpleDateFormat formatwudi = new SimpleDateFormat("HH:mm");
String zwxb;
String xwsb;
Calendar zwxbcalendar = Calendar.getInstance();
for (int k = 0; k < acuserList.size(); k++) {

AttendanceRecord adr = new AttendanceRecord();
adr.setPersonid(acuserList.get(k).getPersonid());
adr.setBtime(btime);
adr.setEtime(addcalendarone(etime));
List<AttendanceRecord> adrList = (List<AttendanceRecord>) sm
.queryForList("sel_attendanceRecord_personid", adr);
AttendanceRecord ar;
Map adrmap = new HashMap<String, String>();


for (int i = 0; i < adrList.size(); i++) {
ar = adrList.get(i);
Date date = formatfile.parse(ar.getAttendanceTime());
String dateviewshow=sf.format(date);
List<Long> numbers = new ArrayList<Long>();
// 得到该天准确数据
List<String> adrNowList = (List<String>) sm
.queryForList("sel_attendanceRecord_time",
ar);
for (int j = 0; j < adrNowList.size(); j++) {
numbers.add(formatfile.parse(adrNowList.get(j)).getTime());
}
String minNum;
String maxNum;
if (adrNowList.size() == 1) {
minNum = transferLongToDate("HH:mm",
Collections.min(numbers));
maxNum = " ";
} else if (adrNowList.size() == 0) {
minNum = " ";
maxNum = " ";

} else {

minNum = transferLongToDate("HH:mm",
Collections.min(numbers));
maxNum = transferLongToDate("HH:mm",
Collections.max(numbers));
}
if(cycleflay){
adrmap.put(dateviewshow, minNum + "_" + maxNum);
}else
{
zwxbcalendar.setTime(date);

zwxb=cycle.split("-")[0].split(",")[1];
xwsb=cycle.split("-")[1].split(",")[0];
Date datezwxb=DateRetuurnAppoint(date,zwxb);
Date datexwsb=DateRetuurnAppoint(date,xwsb);

Date datezwzt=DateRetuurnAppoint(date,cycle.split("-")[0].split(",")[0]);
Date datezwcd=DateRetuurnAppoint(date,cycle.split("-")[1].split(",")[1]);
List<Long> zwdklist= new ArrayList<Long>();//中午打卡标准数据
List<Long> zwdklistzt= new ArrayList<Long>();//中午下班早退数据
List<Long> zwdklistcd= new ArrayList<Long>();//中午下班迟到数据
if(numbers.size()>2){
for (int nb = 0; nb < numbers.size(); nb++) {
//System.out.println("*******: "+formatfile.format(datexwsb));
//System.out.println(dateviewshow+":"+formatfile.format(numbers.get(nb)));
//System.out.println(formatfile.format(datexwsb).compareTo(formatfile.format(numbers.get(nb)));
//System.out.println(formatfile.format(datexwsb).compareTo(formatfile.format(numbers.get(nb)));
if(formatfile.format(datexwsb).compareTo(formatfile.format(numbers.get(nb)))>=0&&
formatfile.format(datezwxb).compareTo(formatfile.format(numbers.get(nb)))<=0)
{
zwdklist.add(numbers.get(nb));
}else if(formatfile.format(datezwcd).compareTo(formatfile.format(numbers.get(nb)))>=0&&
formatfile.format(datexwsb).compareTo(formatfile.format(numbers.get(nb)))<=0)
{
zwdklistcd.add(numbers.get(nb));
}else if(formatfile.format(datexwsb).compareTo(formatfile.format(numbers.get(nb)))>=0&&
formatfile.format(datezwzt).compareTo(formatfile.format(numbers.get(nb)))<=0)
{
zwdklistzt.add(numbers.get(nb));
}
}
}
String zwminNum = " ";
String zwmaxNum = " ";
Long differencesw,differencexw=0L;
//System.out.println("这里判断集合的长度 : "+zwdklist.size());
//System.out.println("这里集合的内容 : "+formatfile.parse(formatfile.format(zwdklist.get(0))));
if (zwdklist.size() == 1) {
Long tk=formatwudi.parse(xwsb).getTime();
Long differ=formatwudi.parse(formatwudi.format(new Date(zwdklist.get(0)))).getTime();
differencesw=DateNumericaldifference(differ,formatwudi.parse(zwxb).getTime());
differencexw=DateNumericaldifference(formatwudi.parse(xwsb).getTime(),differ);
if(differencesw<differencexw)
{

zwminNum = transferLongToDate("HH:mm",
zwdklist.get(0));
if(zwdklistcd.size()!=0){
zwmaxNum = transferLongToDate("HH:mm",
Collections.min(zwdklistcd));
}

}else if(differencesw>differencexw)
{

zwmaxNum = transferLongToDate("HH:mm",
zwdklist.get(0));
if(zwdklistzt.size()!=0){
zwminNum = transferLongToDate("HH:mm",
Collections.min(zwdklistzt));
}

}

} else if (zwdklist.size() == 0) {
if(zwdklistcd.size()!=0){
zwmaxNum = transferLongToDate("HH:mm",
Collections.min(zwdklistcd));
}
if(zwdklistzt.size()!=0){
zwmaxNum = transferLongToDate("HH:mm",
Collections.max(zwdklistzt));
}

} else {

zwminNum = transferLongToDate("HH:mm",
Collections.min(zwdklist));
zwmaxNum = transferLongToDate("HH:mm",
Collections.max(zwdklist));
}
adrmap.put(dateviewshow,minNum + "_" + zwminNum +"_"+ zwmaxNum+ "_" + maxNum);
}



}

// 循环构建员工打卡记录
if(!cycleflay){

ws.addCell(new Label(0, k * 5+3 ,Constant.LOCAL.getD00067()+": "
+ acuserList.get(k).getUserName(), wcfF));
ws.mergeCells(0, 3 + k * 5, currentMothCount+4, 0);
}else
{
ws.addCell(new Label(0, 3 + k * 3,Constant.LOCAL.getD00067()+": "
+ acuserList.get(k).getUserName(), wcfF));
ws.mergeCells(0, 3 + k * 3, currentMothCount+4, 0);
}

WritableFont zwDateatr = new WritableFont(
WritableFont.createFont("宋体"), 8, WritableFont.NO_BOLD,
false);
WritableCellFormat zwfDateFatr = new WritableCellFormat(zwDateatr);
zwfDateFatr.setAlignment(Alignment.CENTRE);// 设置左右对其
WritableFont zwDateatrtj = new WritableFont(
WritableFont.createFont("宋体"), 10, WritableFont.NO_BOLD,
false);
WritableCellFormat zwfDateFatrtj = new WritableCellFormat(zwDateatrtj);
zwfDateFatrtj.setAlignment(Alignment.CENTRE);// 设置左右对其
int cdCount=0;//迟到统计
int ztCount=0;//早退统计
int kgCount=0;//旷工统计

if(!cycleflay)
{
ws.addCell(new Label(0, 3 + k * 5 + 1, Constant.LOCAL.getE00003(), zwfDateFatr));
ws.addCell(new Label(0, 3 + k * 5 + 2, Constant.LOCAL.getE00004(), zwfDateFatr));
ws.addCell(new Label(0, 3 + k * 5 + 3, Constant.LOCAL.getE00003(), zwfDateFatr));
ws.addCell(new Label(0, 3 + k * 5 + 4, Constant.LOCAL.getE00004(), zwfDateFatr));
}else
{
ws.addCell(new Label(0, 3 + k * 3 + 1, Constant.LOCAL.getE00003(), zwfDateFatr));
ws.addCell(new Label(0, 3 + k * 3 + 2, Constant.LOCAL.getE00004(), zwfDateFatr));
}
String towork,offwork,towork1,offwork1;
for (int i2 = 0; i2 <currentMothList.size(); i2++) {

String currentMoth=sf.format(currentMothList.get(i2));
if(cycleflay)
{

if(adrmap.get(currentMoth)==null||adrmap.get(currentMoth)=="")
{
towork="";
offwork="";
kgCount++;
}else{
towork= adrmap.get(currentMoth).toString().split("_")[0];
offwork=adrmap.get(currentMoth).toString().split("_")[1];
if(Long.valueOf(towork.replace(":",""))>cdTime)
{
++cdCount;

}
if(offwork.equals(" ")||Long.valueOf(offwork.replace(":",""))<ztTime)
{
++ztCount;
}
}

//上班 下班
ws.addCell(new Label(i2+1, 3 + k * 3 + 1, towork, zwfDateFatr));
ws.addCell(new Label(i2+1, 3 + k * 3 + 2, offwork, zwfDateFatr));
ws.addCell(new Label(currentMothCount+1, 3 + k * 3 + 1, Constant.LOCAL.getD00086()+":"+cdCount+Constant.LOCAL.getD00089(), zwfDateFatrtj));
ws.mergeCells(currentMothCount+1,3 + k * 3 + 1,currentMothCount+2, 0);


ws.addCell(new Label(currentMothCount+1, 3 + k * 3 + 2, Constant.LOCAL.getD00087()+":"+ztCount+Constant.LOCAL.getD00089(), zwfDateFatrtj));
ws.mergeCells(currentMothCount+1,3 + k * 3 + 2,currentMothCount+2, 0);


ws.addCell(new Label(currentMothCount+3, 3 + k * 3 + 1, Constant.LOCAL.getD00088()+":"+kgCount+Constant.LOCAL.getD00089(), zwfDateFatrtj));
ws.mergeCells(currentMothCount+3,3 + k * 3 + 1,currentMothCount+4, 0);
}else
{
if(adrmap.get(currentMoth)==null||adrmap.get(currentMoth)=="")
{
towork="";
offwork="";
offwork1= "";
towork1="";
kgCount++;
}else{
towork= adrmap.get(currentMoth).toString().split("_")[0];
offwork1= adrmap.get(currentMoth).toString().split("_")[1];
towork1=adrmap.get(currentMoth).toString().split("_")[2];
offwork=adrmap.get(currentMoth).toString().split("_")[3];

if(towork.equals(" ")||Long.valueOf(towork.replace(":",""))>cdTime)
{
++cdCount;

}
if(offwork.equals(" ")||Long.valueOf(offwork.replace(":",""))<ztTime1)
{
++ztCount;
}

if(towork1.equals(" ")||Long.valueOf(towork1.replace(":",""))>cdTime1)
{
++cdCount;
}
if(offwork1.equals(" ")||Long.valueOf(offwork1.replace(":",""))<ztTime)
{
++ztCount;
}
}

ws.addCell(new Label(i2+1, 3 + k * 5 + 1, towork, zwfDateFatr));
ws.addCell(new Label(i2+1, 3 + k * 5 + 2, offwork1, zwfDateFatr));
ws.addCell(new Label(i2+1, 3 + k * 5 + 3, towork1, zwfDateFatr));
ws.addCell(new Label(i2+1, 3 + k * 5 + 4, offwork, zwfDateFatr));

ws.addCell(new Label(currentMothCount+1, 3 + k * 5 + 1, Constant.LOCAL.getD00086()+":"+cdCount+Constant.LOCAL.getD00089(), zwfDateFatrtj));
ws.mergeCells(currentMothCount+1,3 + k * 5 + 1,currentMothCount+2, 0);


ws.addCell(new Label(currentMothCount+1, 3 + k * 5 + 2, Constant.LOCAL.getD00087()+":"+ztCount+Constant.LOCAL.getD00089(), zwfDateFatrtj));
ws.mergeCells(currentMothCount+1,3 + k * 5 + 2,currentMothCount+2, 0);


ws.addCell(new Label(currentMothCount+3, 3 + k * 5 + 1, Constant.LOCAL.getD00088()+":"+kgCount+Constant.LOCAL.getD00089(), zwfDateFatrtj));
ws.mergeCells(currentMothCount+3,3 + k * 5 + 1,currentMothCount+4, 0);
}
}

}
wwb.write();
wwb.close();
return SUCCESS;
}

public static int CaculateWeekDay(int y, int m, int d) {
if (m == 1)
m = 13;
if (m == 2)
m = 14;
int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;

return week;
}

public static int getMaxDayByYearMonth(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year - 1);
calendar.set(Calendar.MONTH, month);
return calendar.getActualMaximum(Calendar.DATE);
}

public String transferLongToDate(String dateFormat, Long millSec) {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
// Date date = new Date(millSec);
return sdf.format(millSec);
}

// 获得指定月份第一天
public static String getFirstDay(int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
SimpleDateFormat formatfile = new SimpleDateFormat("yyyy-MM-dd");
return formatfile.format(cal.getTime()).toString();
}

// 获得指定月份第二天
public static String gettwoDay(int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE) + 1);
SimpleDateFormat formatfile = new SimpleDateFormat("yyyy-MM-dd");
return formatfile.format(cal.getTime()).toString();
}
//日加0
public static String getDDTimeHandle(int Ddate)
{
String data="";
if(String.valueOf(Ddate).length()==1)
{
data="0"+Ddate;
}else
{
data=String.valueOf(Ddate);
}
return data;
}

/**
* 计算时间
* @param startTime : 开始时间
* @param endTime : 结束时间
* @return
*/
public static int caculateTotalTime(String startTime,String endTime) {
SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd");
Date date1=null;
Date date = null;
Long l = 0L;
try {
date = formatter.parse(startTime);
long ts = date.getTime();
date1 = formatter.parse(endTime);
long ts1 = date1.getTime();

l = (ts - ts1) / (1000 * 60 * 60 * 24);

} catch (Exception e) {
e.printStackTrace();
}
return l.intValue();
}
/**
* 获得某个时间段所有时间
* @param 开始时间
* @param 结束时间
* @return
* @throws ParseException
*/
public static List<Date> findDates(String startTime,String endTime) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date dBegin=formatter.parse(startTime);
Date dEnd=formatter.parse(endTime);
List lDate = new ArrayList();
lDate.add(dBegin);
Calendar calBegin = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calBegin.setTime(dBegin);
Calendar calEnd = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calEnd.setTime(dEnd);
// 测试此日期是否在指定日期之后
while (dEnd.after(calBegin.getTime())) {
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
calBegin.add(Calendar.DAY_OF_MONTH, 1);
lDate.add(calBegin.getTime());
}
return lDate;
}
/**
* 判断时间集合里是否存在指定日期
* @return
* @throws ParseException
*/
public static int listbooleandate(List<Date> dateList,String date) throws ParseException
{
int result=0;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date nowDate = formatter.parse(date);
for (Date data : dateList) {
if(DateUtils.isSameDay(nowDate, data))
{
result=1;
}
}
return result;
}
/**
* @param data
* @return 往后推一天
* @throws ParseException
*/
public static String addcalendarone(String data) throws ParseException
{
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date nowDate = formatter.parse(data);
Calendar c = Calendar.getInstance();
c.setTime(nowDate);
c.add(Calendar.DAY_OF_MONTH, 1);
return formatter.format(c.getTime());

}
/**
*
* @param date
* @param hourminte
* @return
* @throws ParseException
*/
public static Date DateRetuurnAppoint(Date date,String hourminte) throws ParseException
{
Calendar c = Calendar.getInstance();
c.setTime(date);
Calendar g = Calendar.getInstance();
g.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE),Integer.parseInt(hourminte.split(":")[0]) ,Integer.parseInt(hourminte.split(":")[1]),c.get(Calendar.SECOND));

return g.getTime();
}

/**
* 判断俩个数值差
* @param a
* @param b
* @return
*/
public static Long DateNumericaldifference(Long a,Long b)
{
return a-b;
}
public String getBtime() {
return btime;
}

public void setBtime(String btime) {
this.btime = btime;
}

public String getEtime() {
return etime;
}

public void setEtime(String etime) {
this.etime = etime;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

}

大概效果如图1567880022506

1567880055322


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!