forked from chenssy89/jutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateUtils.java
More file actions
738 lines (662 loc) · 19.8 KB
/
DateUtils.java
File metadata and controls
738 lines (662 loc) · 19.8 KB
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
package com.JUtils.date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @desc:时间处理工具类
*
* @Author:chenssy
* @date:2014年8月4日
*/
public class DateUtils {
private static final String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
/**
* 根据指定格式获取当前时间
* @author chenssy
* @date Dec 27, 2013
* @param format
* @return String
*/
public static String getCurrentTime(String format){
SimpleDateFormat sdf = DateFormatUtils.getFormat(format);
Date date = new Date();
return sdf.format(date);
}
/**
* 获取当前时间,格式为:yyyy-MM-dd HH:mm:ss
* @author chenssy
* @date Dec 27, 2013
* @return String
*/
public static String getCurrentTime(){
return getCurrentTime(DateFormatUtils.DATE_FORMAT2);
}
/**
* 获取指定格式的当前时间:为空时格式为yyyy-mm-dd HH:mm:ss
* @author chenssy
* @date Dec 30, 2013
* @param format
* @return Date
*/
public static Date getCurrentDate(String format){
SimpleDateFormat sdf = DateFormatUtils.getFormat(format);
String dateS = getCurrentTime(format);
Date date = null;
try {
date = sdf.parse(dateS);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 获取当前时间,格式为yyyy-MM-dd HH:mm:ss
* @author chenssy
* @date Dec 30, 2013
* @return Date
*/
public static Date getCurrentDate(){
return getCurrentDate(DateFormatUtils.DATE_FORMAT2);
}
/**
* 给指定日期加入年份,为空时默认当前时间
* @author chenssy
* @date Dec 30, 2013
* @param year 年份 正数相加、负数相减
* @param date 为空时,默认为当前时间
* @param format 默认格式为:yyyy-MM-dd HH:mm:ss
* @return String
*/
public static String addYearToDate(int year,Date date,String format){
Calendar calender = getCalendar(date,format);
SimpleDateFormat sdf = DateFormatUtils.getFormat(format);
calender.add(Calendar.YEAR, year);
return sdf.format(calender.getTime());
}
/**
* 给指定日期加入年份,为空时默认当前时间
* @author chenssy
* @date Dec 30, 2013
* @param year 年份 正数相加、负数相减
* @param date 为空时,默认为当前时间
* @param format 默认格式为:yyyy-MM-dd HH:mm:ss
* @return String
*/
public static String addYearToDate(int year,String date,String format){
Date newDate = new Date();
if(null != date && !"".equals(date)){
newDate = string2Date(date, format);
}
return addYearToDate(year, newDate, format);
}
/**
* 给指定日期增加月份 为空时默认当前时间
* @author chenssy
* @date Dec 30, 2013
* @param month 增加月份 正数相加、负数相减
* @param date 指定时间
* @param format 指定格式 为空默认 yyyy-mm-dd HH:mm:ss
* @return String
*/
public static String addMothToDate(int month,Date date,String format) {
Calendar calender = getCalendar(date,format);
SimpleDateFormat sdf = DateFormatUtils.getFormat(format);
calender.add(Calendar.MONTH, month);
return sdf.format(calender.getTime());
}
/**
* 给指定日期增加月份 为空时默认当前时间
* @author chenssy
* @date Dec 30, 2013
* @param month 增加月份 正数相加、负数相减
* @param date 指定时间
* @param format 指定格式 为空默认 yyyy-mm-dd HH:mm:ss
* @return String
*/
public static String addMothToDate(int month,String date,String format) {
Date newDate = new Date();
if(null != date && !"".equals(date)){
newDate = string2Date(date, format);
}
return addMothToDate(month, newDate, format);
}
/**
* 给指定日期增加天数,为空时默认当前时间
* @author chenssy
* @date Dec 31, 2013
* @param day 增加天数 正数相加、负数相减
* @param date 指定日期
* @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
* @return String
*/
public static String addDayToDate(int day,Date date,String format) {
Calendar calendar = getCalendar(date, format);
SimpleDateFormat sdf = DateFormatUtils.getFormat(format);
calendar.add(Calendar.DATE, day);
return sdf.format(calendar.getTime());
}
/**
* 给指定日期增加天数,为空时默认当前时间
* @author chenssy
* @date Dec 31, 2013
* @param day 增加天数 正数相加、负数相减
* @param date 指定日期
* @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
* @return String
*/
public static String addDayToDate(int day,String date,String format) {
Date newDate = new Date();
if(null != date && !"".equals(date)){
newDate = string2Date(date, format);
}
return addDayToDate(day, newDate, format);
}
/**
* 给指定日期增加小时,为空时默认当前时间
* @author chenssy
* @date Dec 31, 2013
* @param hour 增加小时 正数相加、负数相减
* @param date 指定日期
* @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
* @return String
*/
public static String addHourToDate(int hour,Date date,String format) {
Calendar calendar = getCalendar(date, format);
SimpleDateFormat sdf = DateFormatUtils.getFormat(format);
calendar.add(Calendar.HOUR, hour);
return sdf.format(calendar.getTime());
}
/**
* 给指定日期增加小时,为空时默认当前时间
* @author chenssy
* @date Dec 31, 2013
* @param hour 增加小时 正数相加、负数相减
* @param date 指定日期
* @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
* @return String
*/
public static String addHourToDate(int hour,String date,String format) {
Date newDate = new Date();
if(null != date && !"".equals(date)){
newDate = string2Date(date, format);
}
return addHourToDate(hour, newDate, format);
}
/**
* 给指定的日期增加分钟,为空时默认当前时间
* @author chenssy
* @date Dec 31, 2013
* @param minute 增加分钟 正数相加、负数相减
* @param date 指定日期
* @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
* @return String
*/
public static String addMinuteToDate(int minute,Date date,String format) {
Calendar calendar = getCalendar(date, format);
SimpleDateFormat sdf = DateFormatUtils.getFormat(format);
calendar.add(Calendar.MINUTE, minute);
return sdf.format(calendar.getTime());
}
/**
* 给指定的日期增加分钟,为空时默认当前时间
* @author chenssy
* @date Dec 31, 2013
* @param minute 增加分钟 正数相加、负数相减
* @param date 指定日期
* @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
* @return String
*/
public static String addMinuteToDate(int minute,String date,String format){
Date newDate = new Date();
if(null != date && !"".equals(date)){
newDate = string2Date(date, format);
}
return addMinuteToDate(minute, newDate, format);
}
/**
* 给指定日期增加秒,为空时默认当前时间
* @author chenssy
* @date Dec 31, 2013
* @param second 增加秒 正数相加、负数相减
* @param date 指定日期
* @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
* @return String
*/
public static String addSecondToDate(int second,Date date,String format){
Calendar calendar = getCalendar(date, format);
SimpleDateFormat sdf = DateFormatUtils.getFormat(format);
calendar.add(Calendar.SECOND, second);
return sdf.format(calendar.getTime());
}
/**
* 给指定日期增加秒,为空时默认当前时间
* @author chenssy
* @date Dec 31, 2013
* @param second 增加秒 正数相加、负数相减
* @param date 指定日期
* @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
* @return String
* @throws Exception
*/
public static String addSecondToDate(int second,String date,String format){
Date newDate = new Date();
if(null != date && !"".equals(date)){
newDate = string2Date(date, format);
}
return addSecondToDate(second, newDate, format);
}
/**
* 获取指定格式指定时间的日历
* @author chenssy
* @date Dec 30, 2013
* @param date 时间
* @param format 格式
* @return Calendar
*/
public static Calendar getCalendar(Date date,String format){
if(date == null){
date = getCurrentDate(format);
}
Calendar calender = Calendar.getInstance();
calender.setTime(date);
return calender;
}
/**
* 字符串转换为日期,日期格式为
*
* @author : chenssy
* @date : 2016年5月31日 下午5:20:22
*
* @param value
* @return
*/
public static Date string2Date(String value){
if(value == null || "".equals(value)){
return null;
}
SimpleDateFormat sdf = DateFormatUtils.getFormat(DateFormatUtils.DATE_FORMAT2);
Date date = null;
try {
value = DateFormatUtils.formatDate(value, DateFormatUtils.DATE_FORMAT2);
date = sdf.parse(value);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
/**
* 将字符串(格式符合规范)转换成Date
* @author chenssy
* @date Dec 31, 2013
* @param value 需要转换的字符串
* @param format 日期格式
* @return Date
*/
public static Date string2Date(String value,String format){
if(value == null || "".equals(value)){
return null;
}
SimpleDateFormat sdf = DateFormatUtils.getFormat(format);
Date date = null;
try {
value = DateFormatUtils.formatDate(value, format);
date = sdf.parse(value);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
/**
* 将日期格式转换成String
* @author chenssy
* @date Dec 31, 2013
*
* @param value 需要转换的日期
* @param format 日期格式
* @return String
*/
public static String date2String(Date value,String format){
if(value == null){
return null;
}
SimpleDateFormat sdf = DateFormatUtils.getFormat(format);
return sdf.format(value);
}
/**
* 日期转换为字符串
*
* @author : chenssy
* @date : 2016年5月31日 下午5:21:38
*
* @param value
* @return
*/
public static String date2String(Date value){
if(value == null){
return null;
}
SimpleDateFormat sdf = DateFormatUtils.getFormat(DateFormatUtils.DATE_FORMAT2);
return sdf.format(value);
}
/**
* 获取指定日期的年份
* @author chenssy
* @date Dec 31, 2013
* @param value 日期
* @return int
*/
public static int getCurrentYear(Date value){
String date = date2String(value, DateFormatUtils.DATE_YEAR);
return Integer.valueOf(date);
}
/**
* 获取指定日期的年份
* @author chenssy
* @date Dec 31, 2013
* @param value 日期
* @return int
*/
public static int getCurrentYear(String value) {
Date date = string2Date(value, DateFormatUtils.DATE_YEAR);
Calendar calendar = getCalendar(date, DateFormatUtils.DATE_YEAR);
return calendar.get(Calendar.YEAR);
}
/**
* 获取指定日期的月份
* @author chenssy
* @date Dec 31, 2013
* @param value 日期
* @return int
*/
public static int getCurrentMonth(Date value){
String date = date2String(value, DateFormatUtils.DATE_MONTH);
return Integer.valueOf(date);
}
/**
* 获取指定日期的月份
* @author chenssy
* @date Dec 31, 2013
* @param value 日期
* @return int
*/
public static int getCurrentMonth(String value) {
Date date = string2Date(value, DateFormatUtils.DATE_MONTH);
Calendar calendar = getCalendar(date, DateFormatUtils.DATE_MONTH);
return calendar.get(Calendar.MONTH);
}
/**
* 获取指定日期的天份
* @author chenssy
* @date Dec 31, 2013
* @param value 日期
* @return int
*/
public static int getCurrentDay(Date value){
String date = date2String(value, DateFormatUtils.DATE_DAY);
return Integer.valueOf(date);
}
/**
* 获取指定日期的天份
* @author chenssy
* @date Dec 31, 2013
* @param value 日期
* @return int
*/
public static int getCurrentDay(String value){
Date date = string2Date(value, DateFormatUtils.DATE_DAY);
Calendar calendar = getCalendar(date, DateFormatUtils.DATE_DAY);
return calendar.get(Calendar.DATE);
}
/**
* 获取当前日期为星期几
* @author chenssy
* @date Dec 31, 2013
* @param value 日期
* @return String
*/
public static String getCurrentWeek(Date value) {
Calendar calendar = getCalendar(value, DateFormatUtils.DATE_FORMAT1);
int weekIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1 < 0 ? 0 : calendar.get(Calendar.DAY_OF_WEEK) - 1;
return weeks[weekIndex];
}
/**
* 获取当前日期为星期几
* @author chenssy
* @date Dec 31, 2013
* @param value 日期
* @return String
*/
public static String getCurrentWeek(String value) {
Date date = string2Date(value, DateFormatUtils.DATE_FORMAT1);
return getCurrentWeek(date);
}
/**
* 获取指定日期的小时
* @author chenssy
* @date Dec 31, 2013
* @param value 日期
* @return int
*/
public static int getCurrentHour(Date value){
String date = date2String(value, DateFormatUtils.DATE_HOUR);
return Integer.valueOf(date);
}
/**
* 获取指定日期的小时
* @author chenssy
* @date Dec 31, 2013
* @param value 日期
* @return
* @return int
*/
public static int getCurrentHour(String value) {
Date date = string2Date(value, DateFormatUtils.DATE_HOUR);
Calendar calendar = getCalendar(date, DateFormatUtils.DATE_HOUR);
return calendar.get(Calendar.DATE);
}
/**
* 获取指定日期的分钟
* @author chenssy
* @date Dec 31, 2013
* @param value 日期
* @return int
*/
public static int getCurrentMinute(Date value){
String date = date2String(value, DateFormatUtils.DATE_MINUTE);
return Integer.valueOf(date);
}
/**
* 获取指定日期的分钟
* @author chenssy
* @date Dec 31, 2013
* @param value 日期
* @return int
*/
public static int getCurrentMinute(String value){
Date date = string2Date(value, DateFormatUtils.DATE_MINUTE);
Calendar calendar = getCalendar(date, DateFormatUtils.DATE_MINUTE);
return calendar.get(Calendar.MINUTE);
}
/**
* 比较两个日期相隔多少天(月、年) <br>
* 例:<br>
* compareDate("2009-09-12", null, 0);//比较天 <br>
* compareDate("2009-09-12", null, 1);//比较月 <br>
* compareDate("2009-09-12", null, 2);//比较年 <br>
*
* @author chenssy
* @date Dec 31, 2013
* @param startDay 需要比较的时间 不能为空(null),需要正确的日期格式 ,如:2009-09-12
* @param endDay 被比较的时间 为空(null)则为当前时间
* @param stype 返回值类型 0为多少天,1为多少个月,2为多少年
* @return int
*/
public static int compareDate(String startDay,String endDay,int stype) {
int n = 0;
startDay = DateFormatUtils.formatDate(startDay, "yyyy-MM-dd");
endDay = DateFormatUtils.formatDate(endDay, "yyyy-MM-dd");
String formatStyle = "yyyy-MM-dd";
if(1 == stype){
formatStyle = "yyyy-MM";
}else if(2 == stype){
formatStyle = "yyyy";
}
endDay = endDay==null ? getCurrentTime("yyyy-MM-dd") : endDay;
DateFormat df = new SimpleDateFormat(formatStyle);
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
try {
c1.setTime(df.parse(startDay));
c2.setTime(df.parse(endDay));
} catch (Exception e) {
e.printStackTrace();
}
while (!c1.after(c2)) { // 循环对比,直到相等,n 就是所要的结果
n++;
if(stype==1){
c1.add(Calendar.MONTH, 1); // 比较月份,月份+1
}
else{
c1.add(Calendar.DATE, 1); // 比较天数,日期+1
}
}
n = n-1;
if(stype==2){
n = (int)n/365;
}
return n;
}
/**
* 比较两个时间相差多少小时(分钟、秒)
* @author chenssy
* @date Jan 2, 2014
* @param startTime 需要比较的时间 不能为空,且必须符合正确格式:2012-12-12 12:12:
* @param endTime 需要被比较的时间 若为空则默认当前时间
* @param type 1:小时 2:分钟 3:秒
* @return int
*/
public static int compareTime(String startTime , String endTime , int type) {
//endTime是否为空,为空默认当前时间
if(endTime == null || "".equals(endTime)){
endTime = getCurrentTime();
}
SimpleDateFormat sdf = DateFormatUtils.getFormat("");
int value = 0;
try {
Date begin = sdf.parse(startTime);
Date end = sdf.parse(endTime);
long between = (end.getTime() - begin.getTime()) / 1000; //除以1000转换成豪秒
if(type == 1){ //小时
value = (int) (between % (24 * 36000) / 3600);
}
else if(type == 2){
value = (int) (between % 3600 / 60);
}
else if(type == 3){
value = (int) (between % 60 / 60);
}
} catch (ParseException e) {
e.printStackTrace();
}
return value;
}
/**
* 比较两个日期的大小。<br>
* 若date1 > date2 则返回 1<br>
* 若date1 = date2 则返回 0<br>
* 若date1 < date2 则返回-1
* @autor:chenssy
* @date:2014年9月9日
*
* @param date1
* @param date2
* @param format 待转换的格式
* @return 比较结果
*/
public static int compare(String date1, String date2,String format) {
DateFormat df = DateFormatUtils.getFormat(format);
try {
Date dt1 = df.parse(date1);
Date dt2 = df.parse(date2);
if (dt1.getTime() > dt2.getTime()) {
return 1;
} else if (dt1.getTime() < dt2.getTime()) {
return -1;
} else {
return 0;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return 0;
}
/**
* 获取指定月份的第一天
*
* @author : chenssy
* @date : 2016年5月31日 下午5:31:10
*
* @param date
* @return
*/
public static String getMonthFirstDate(String date){
date = DateFormatUtils.formatDate(date);
return DateFormatUtils.formatDate(date, "yyyy-MM") + "-01";
}
/**
* 获取指定月份的最后一天
*
* @author : chenssy
* @date : 2016年5月31日 下午5:32:09
*
* @param strdate
* @return
*/
public static String getMonthLastDate(String date) {
Date strDate = DateUtils.string2Date(getMonthFirstDate(date));
Calendar calendar = Calendar.getInstance();
calendar.setTime(strDate);
calendar.add(Calendar.MONTH, 1);
calendar.add(Calendar.DAY_OF_YEAR, -1);
return DateFormatUtils.formatDate(calendar.getTime());
}
/**
* 获取所在星期的第一天
*
* @author : chenssy
* @date : 2016年6月1日 下午12:38:53
*
* @param date
* @return
*/
@SuppressWarnings("static-access")
public static Date getWeekFirstDate(Date date) {
Calendar now = Calendar.getInstance();
now.setTime(date);
int today = now.get(Calendar.DAY_OF_WEEK);
int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一
now.set(now.DATE, first_day_of_week);
return now.getTime();
}
/**
* 获取所在星期的最后一天
*
* @author : chenssy
* @date : 2016年6月1日 下午12:40:31
*
* @param date
* @return
*/
@SuppressWarnings("static-access")
public static Date geWeektLastDate(Date date) {
Calendar now = Calendar.getInstance();
now.setTime(date);
int today = now.get(Calendar.DAY_OF_WEEK);
int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一
int last_day_of_week = first_day_of_week + 6; // 星期日
now.set(now.DATE, last_day_of_week);
return now.getTime();
}
}