forked from AlEinstein/javaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateUtil.java
More file actions
1109 lines (933 loc) · 30.7 KB
/
Copy pathDateUtil.java
File metadata and controls
1109 lines (933 loc) · 30.7 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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package common.utils;
import common.Exception.IncorrectFormatException;
import common.enumeration.dateStyleEnum.DateStyle;
import common.enumeration.dateStyleEnum.MonthStyle;
import common.enumeration.dateStyleEnum.WeekStyle;
import common.enumeration.errorMessageEnum.ErrorMessage;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* create by mulin on 2018/08/26
*
* The tool class function is to be improved,
* If you have any questions or suggestions, please contact OA:linmu.
*
* The methods designed in this class include the following categories:
* 1. Tool method used in this class. (line: 49)
* 2. Some common judgment methods about time. (line: 113)
* 3. Get the current time. (line:174)
* 4. Get the specified time. (line: 373)
* 5. Date format conversion. (line: 480)
* 6. Addition and subtraction of dates. (line: 691)
* 7. Operation on time difference. (line: 959)
* 8. Date assembly. (line:1009)
*/
public class DateUtil {
private static final Logger logger = LoggerFactory.getLogger(DateUtil.class);
private static String PARAMETER_EMPTY_ERROR = ErrorMessage.PARAMETER_EMPTY_ERROR.getErrorInfo();
private static String FORMAT_INCORRECT_ERROR = ErrorMessage.FORMAT_INCORRECT_ERROR.getErrorInfo();
private static long oneSecond = 1000;
private static long oneMinute = oneSecond*60;
private static long oneHour = oneMinute*60;
private static long oneDay = oneHour*24;
/*
Tool method used in this class. -------------------------------
*/
/**
* 存放不同的日期模板格式的sdf的Map
*/
private static ThreadLocal<Map<String, SimpleDateFormat>> sdfMap = new ThreadLocal<Map<String, SimpleDateFormat>>() {
@Override
protected Map<String, SimpleDateFormat> initialValue() {
logger.info(Thread.currentThread().getName() + " init pattern: " + Thread.currentThread());
return new HashMap<>();
}
};
/**
* 获取传入的日期格式对应的SimpleDateFormat对象
* 每个线程只会new一次pattern对应的sdf
*
* @param pattern
* @return SimpleDateFormat
*/
public static SimpleDateFormat getSimpleDateFormat(String pattern) {
Map<String, SimpleDateFormat> tl = sdfMap.get();
SimpleDateFormat sdf = tl.get(pattern);
if (sdf == null) {
logger.info(Thread.currentThread().getName()+" put new sdf of pattern " + pattern + " to map.");
sdf = new SimpleDateFormat(pattern);
tl.put(pattern, sdf);
}
return sdf;
}
/**
* 获取日期字符串的日期风格。
*
* @param date 日期字符串
* @return 日期风格
* @throws Exception
*/
public static DateStyle getDateStyle(String date) {
if (StringUtils.isEmpty(date)) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
for (DateStyle style : DateStyle.values()) {
try {
SimpleDateFormat format = getSimpleDateFormat(style.getValue());
format.setLenient(false); //若lenient为true,则SimpleDateFormat会比较宽松地验证日期,如2018/02/29会被接受,并转换成2018/03/01
format.parse(date);
} catch (Exception e) {
continue;
}
return style;
}
return null;
}
/*
Some common judgment methods about time. -------------------------------
*/
/**
* 判断字符串是否为日期字符串
*
* @param date 日期字符串
* @return true or false
*/
public static boolean isDate(String date) throws Exception {
if (getDateStyle(date) != null) {
return true;
}
return false;
}
/**
* 判断目标时间是否在startDate和endDate之间
*
* @param startDate
* @param endDate
* @param targetDate
* @return boolean
* @throws Exception
*/
public static boolean isContainTargetDate(Date startDate, Date endDate, Date targetDate) {
if (startDate == null || endDate == null || targetDate == null) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return targetDate.after(startDate) && targetDate.before(endDate) ? true:false;
}
/**
* 判断两组时间段是否存在交集
*
* @param leftStartDate
* @param leftEndDate
* @param rightStartDate
* @param rightEndDate
* @return boolean
* @throws Exception
*/
public static boolean isIntersection(Date leftStartDate, Date leftEndDate, Date rightStartDate, Date rightEndDate) {
if (leftStartDate == null || leftEndDate == null || rightStartDate == null || rightEndDate == null) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return
((leftStartDate.getTime() >= rightStartDate.getTime()) && leftStartDate.getTime() < rightEndDate.getTime())
|| ((leftStartDate.getTime() > rightStartDate.getTime()) && leftStartDate.getTime() <= rightEndDate.getTime())
|| ((rightStartDate.getTime() >= leftStartDate.getTime()) && rightStartDate.getTime() < leftEndDate.getTime())
|| ((rightStartDate.getTime() > leftStartDate.getTime()) && rightStartDate.getTime() <= leftEndDate.getTime());
}
/*
Get the current time. -------------------------------
*/
/**
* 获得服务器时间 yyyy-MM-dd HH:mm:ss
*
* @return String
* @throws Exception
*/
public static String getCurrentDate() {
return format(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS.getValue());
}
/**
* 获得服务器时间 yyyy/MM/dd HH:mm:ss
*
* @return String
* @throws Exception
*/
public static String getCurrentDateEN() {
return format(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS_EN.getValue());
}
/**
* 获得服务器时间 yyyy年MM月dd日 HH:mm:ss
*
* @return String
* @throws Exception
*/
public static String getCurrentDateCN() {
return format(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS_CN.getValue());
}
/**
* 获取当前年月日 2018-09-01
*
* @return String
* @throws Exception
*/
public static String getCurrentDateYMD() {
return format(new Date(), DateStyle.YYYY_MM_DD.getValue());
}
/**
* 获取当前年月日 2018/09/01
*
* @return String
* @throws Exception
*/
public static String getCurrentDateYMDEN() {
return format(new Date(), DateStyle.YYYY_MM_DD_EN.getValue());
}
/**
* 获取当前年月日 2018年09月01日
*
* @return String
* @throws Exception
*/
public static String getCurrentDateYMDCN() {
return format(new Date(), DateStyle.YYYY_MM_DD_CN.getValue());
}
/**
* 获得当前时分秒 16:52:12
*
* @return String
* @throws Exception
*/
public static String getCurrentDateHMS() {
return format(new Date(), DateStyle.HH_MM_SS.getValue());
}
/**
* 获得当前几点几分 16:52
*
* @return String
* @throws Exception
*/
public static String getCurrentDateHM() {
return format(new Date(), DateStyle.HH_MM.getValue());
}
/**
* 获取当前年份
*
* @return int
* @throws Exception
*/
public static int getCurrentYear() {
return Calendar.getInstance().get(Calendar.YEAR);
}
/**
* 获取当前月份
*
* @return int
* @throws Exception
*/
public static int getCurrentMonth() {
return Calendar.getInstance().get(Calendar.MONTH)+1;
}
/**
* 获取当前月
*
* @return MonthStyle
* @throws Exception
*/
public static MonthStyle getCurrentMonthInfo() throws Exception {
return MonthStyle.getByNumber(getCurrentMonth());
}
/**
* 获取当前星期
*
* @return int
* @throws Exception
*/
public static int getCurrentWeek() {
return Calendar.getInstance().get(Calendar.WEEK_OF_MONTH);
}
/**
* 获取当前星期
*
* @return MonthStyle
* @throws Exception
*/
public static WeekStyle getCurrentWeekInfo() throws Exception {
return WeekStyle.getByNumber(getCurrentWeek());
}
/**
* 获取当前日号
*
* @return int
* @throws Exception
*/
public static int getCurrentDay() {
return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
}
/**
* 获取当前时间的时间戳
*
* @return long 时间戳
* @throws Exception
*/
public static long getTimeString() {
return System.currentTimeMillis();
}
/**
* 根据自定义的样式获得当前时间
*
* @param format 样式
* @return String
* @throws Exception
*/
public static String getDateByFormat(Format format) {
if (format == null) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return format.format(new Date());
}
/**
* 根据自定义的样式获得当前时间
*
* @param dateStyle
* @return String
* @throws Exception
*/
public static String getDateByDateStyle(DateStyle dateStyle) {
if (dateStyle.equals(null)) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return format(new Date(), dateStyle.getValue());
}
/*
Get the specified time. -------------------------------
*/
/**
* 将时间转换为自定义的格式
* 每个线程只会有一个SimpleDateFormat
*
* @param date
* @param pattern
* @return String
*/
public static String format(Date date, String pattern) {
return getSimpleDateFormat(pattern).format(date);
}
/**
* 将时间转换为自定义的格式
*
* @param dateStr
* @param pattern
* @return Date
* @throws ParseException
*/
public static Date parse(String dateStr, String pattern) throws ParseException {
return getSimpleDateFormat(pattern).parse(dateStr);
}
/**
* 根据date获取日期年份
*
* @param date 日期
* @return String 年份
* @throws Exception
*/
public static String getYearString(Date date) {
if (date.equals(null)) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return format(date, DateStyle.YYYY_MM_DD_EN.getValue()).substring(0, 4);
}
/**
* 根据date获取日期月份 2018/09/01 --> 09
*
* @param date 日期
* @return String 月份
* @throws Exception
*/
public static String getMonthString(Date date) {
if (date == null) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return format(date, DateStyle.YYYY_MM_DD_EN.getValue()).substring(5, 7);
}
/**
* 根据date获取日期月份 2018/09/01 --> 9
*
* @param date 日期
* @return int 月份
* @throws Exception
*/
public static int getMonthNumber(Date date) throws Exception {
if (date == null) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return Integer.parseInt(getMonthString(date));
}
/**
* 根据date获取日期日号 2018/09/01 --> 01
*
* @param date 日期
* @return String 日号
* @throws Exception
*/
public static String getDayString(Date date) {
if (date == null) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return format(date, DateStyle.YYYY_MM_DD_EN.getValue()).substring(8, 10);
}
/**
* 根据date获取日期日号 2018/09/01 --> 1
*
* @param date 日期
* @return int 日号
* @throws Exception
*/
public static int getDayNumber(Date date) throws Exception {
if (date == null) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return Integer.parseInt(getDayString(date));
}
/*
Date format conversion. -------------------------------
*/
/**
* 将日期字符串转化为日期。
*
* @param date 日期字符串
* @return 日期
* @throws Exception
*/
public static Date stringToDate(String date) throws Exception {
if (StringUtils.isEmpty(date)) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
DateStyle dateStyle = getDateStyle(date);
if (dateStyle == null) {
throw new IncorrectFormatException(FORMAT_INCORRECT_ERROR);
}
return stringToDate(date, dateStyle);
}
/**
* 将日期字符串转化为日期。
*
* @param date 日期字符串
* @param pattern 日期格式
* @return 日期
* @throws Exception
*/
public static Date stringToDate(String date, String pattern) throws Exception {
if (StringUtils.isEmpty(date) || StringUtils.isEmpty(pattern)) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return parse(date, pattern);
}
/**
* 将日期字符串转化为日期。
*
* @param date 日期字符串
* @param dateStyle 日期风格
* @return 日期
* @throws Exception
*/
public static Date stringToDate(String date, DateStyle dateStyle) throws Exception {
if (StringUtils.isEmpty(date) || dateStyle == null) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return stringToDate(date, dateStyle.getValue());
}
/**
* 将日期转化为日期字符串。
*
* @param date 日期
* @return String 日期字符串
* @throws Exception
*/
public static String dateToString(Date date) throws Exception {
if (date == null) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return dateToString(date, DateStyle.YYYY_MM_DD_HH_MM_SS_EN.getValue());
}
/**
* 将日期转化为日期字符串。
*
* @param date 日期
* @param pattern 日期格式
* @return 日期字符串
* @throws Exception
*/
public static String dateToString(Date date, String pattern) {
if (StringUtils.isEmpty(pattern) || date == null) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return getSimpleDateFormat(pattern).format(date);
}
/**
* 将日期转化为日期字符串。
*
* @param date 日期
* @param dateStyle 日期风格
* @return 日期字符串
* @throws Exception
*/
public static String dateToString(Date date, DateStyle dateStyle) throws Exception {
if (dateStyle == null || date == null) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return dateToString(date, dateStyle.getValue());
}
/**
* 将日期字符串转化为另一日期字符串。
*
* @param date 旧日期字符串
* @param newPattern 新日期格式
* @return 新日期字符串
* @throws Exception
*/
public static String stringToString(String date, String newPattern) throws Exception {
if (StringUtils.isEmpty(date) || StringUtils.isEmpty(newPattern)) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
DateStyle oldDateStyle = getDateStyle(date);
return stringToString(date, oldDateStyle, newPattern);
}
/**
* 将日期字符串转化为另一日期字符串。
*
* @param date 旧日期字符串
* @param newDateStyle 新日期风格
* @return 新日期字符串
* @throws Exception
*/
public static String stringToString(String date, DateStyle newDateStyle) throws Exception {
if (StringUtils.isEmpty(date) || newDateStyle == null) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
DateStyle oldDateStyle = getDateStyle(date);
return stringToString(date, oldDateStyle, newDateStyle);
}
/**
* 将日期字符串转化为另一日期字符串。
*
* @param date 旧日期字符串
* @param oldPattern 旧日期格式
* @param newPattern 新日期格式
* @return 新日期字符串
* @throws Exception
*/
public static String stringToString(String date, String oldPattern, String newPattern) throws Exception {
if (StringUtils.isEmpty(date) || StringUtils.isEmpty(oldPattern) || StringUtils.isEmpty(newPattern)) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return dateToString(stringToDate(date, oldPattern), newPattern);
}
/**
* 将日期字符串转化为另一日期字符串。
*
* @param date 旧日期字符串
* @param oldDteStyle 旧日期风格
* @param newPattern 新日期格式
* @return 新日期字符串
* @throws Exception
*/
public static String stringToString(String date, DateStyle oldDteStyle, String newPattern) throws Exception {
if (StringUtils.isEmpty(date) || StringUtils.isEmpty(newPattern) || oldDteStyle == null) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return stringToString(date, oldDteStyle.getValue(), newPattern);
}
/**
* 将日期字符串转化为另一日期字符串。
*
* @param date 旧日期字符串
* @param oldPattern 旧日期格式
* @param newDateStyle 新日期风格
* @return 新日期字符串
* @throws Exception
*/
public static String stringToString(String date, String oldPattern, DateStyle newDateStyle) throws Exception {
if (StringUtils.isEmpty(date) || StringUtils.isEmpty(oldPattern) || newDateStyle == null) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return stringToString(date, oldPattern, newDateStyle.getValue());
}
/**
* 将日期字符串转化为另一日期字符串。
*
* @param date 旧日期字符串
* @param oldPattern 旧日期风格
* @param newDateStyle 新日期风格
* @return 新日期字符串
* @throws Exception
*/
public static String stringToString(String date, DateStyle oldPattern, DateStyle newDateStyle) throws Exception {
if (StringUtils.isEmpty(date) || oldPattern == null || newDateStyle == null) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return stringToString(date, oldPattern.getValue(), newDateStyle.getValue());
}
/*
Addition and subtraction of dates. -------------------------------
*/
/**
* 获取前一天的Date
*
* @return String
* @throws Exception
*/
public static String getYesterdayString() throws Exception {
return dateToString(getYesterdayDate());
}
/**
* 获取前一天的Date
*
* @return Date
* @throws Exception
*/
public static Date getYesterdayDate() throws Exception {
return addDay(new Date(), -1);
}
/**
* 获取后一天的Date
*
* @return String
* @throws Exception
*/
public static String getTomorrowString() throws Exception {
return dateToString(getTomorrowDate());
}
/**
* 获取后一天的Date
*
* @return Date
* @throws Exception
*/
public static Date getTomorrowDate() throws Exception {
return addDay(new Date(), 1);
}
/**
* 增加日期的年份。
*
* @param date 日期
* @param yearAmount 增加数量。可为负数
* @return 增加年份后的日期字符串
*/
public static String addYear(String date, int yearAmount) throws Exception {
if (StringUtils.isEmpty(date)) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return addInteger(date, Calendar.YEAR, yearAmount);
}
/**
* 增加日期的年份。
*
* @param date 日期
* @param yearAmount 增加数量。可为负数
* @return 增加年份后的日期
*/
public static Date addYear(Date date, int yearAmount) throws Exception {
if (date == null) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return addInteger(date, Calendar.YEAR, yearAmount);
}
/**
* 增加日期的月份。
*
* @param date 日期
* @param monthAmount 增加数量。可为负数
* @return 增加月份后的日期字符串
*/
public static String addMonth(String date, int monthAmount) throws Exception {
if (StringUtils.isEmpty(date)) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return addInteger(date, Calendar.MONTH, monthAmount);
}
/**
* 增加日期的月份。
*
* @param date 日期
* @param monthAmount 增加数量。可为负数
* @return 增加月份后的日期
*/
public static Date addMonth(Date date, int monthAmount) throws Exception {
if (date == null) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return addInteger(date, Calendar.MONTH, monthAmount);
}
/**
* 增加日期的天数。
*
* @param date 日期字符串
* @param dayAmount 增加数量。可为负数
* @return 增加天数后的日期字符串
*/
public static String addDay(String date, int dayAmount) throws Exception {
if (StringUtils.isEmpty(date)) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return addInteger(date, Calendar.DATE, dayAmount);
}
/**
* 增加日期的天数。
*
* @param date 日期
* @param dayAmount 增加数量。可为负数
* @return 增加天数后的日期
*/
public static Date addDay(Date date, int dayAmount) throws Exception {
if (date == null) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return addInteger(date, Calendar.DATE, dayAmount);
}
/**
* 增加日期的小时。
*
* @param date 日期字符串
* @param hourAmount 增加数量。可为负数
* @return 增加小时后的日期字符串
*/
public static String addHour(String date, int hourAmount) throws Exception {
if (StringUtils.isEmpty(date)) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return addInteger(date, Calendar.HOUR_OF_DAY, hourAmount);
}
/**
* 增加日期的小时。
*
* @param date 日期
* @param hourAmount 增加数量。可为负数
* @return 增加小时后的日期
*/
public static Date addHour(Date date, int hourAmount) throws Exception {
if (date == null) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return addInteger(date, Calendar.HOUR_OF_DAY, hourAmount);
}
/**
* 增加日期的分钟。
*
* @param date 日期字符串
* @param minuteAmount 增加数量。可为负数
* @return 增加分钟后的日期字符串
*/
public static String addMinute(String date, int minuteAmount) throws Exception {
if (StringUtils.isEmpty(date)) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return addInteger(date, Calendar.MINUTE, minuteAmount);
}
/**
* 增加日期的分钟。
*
* @param date 日期
* @param minuteAmount 增加数量。可为负数
* @return 增加分钟后的日期
*/
public static Date addMinute(Date date, int minuteAmount) throws Exception {
if (date == null) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return addInteger(date, Calendar.MINUTE, minuteAmount);
}
/**
* 增加日期的秒钟。
*
* @param date 日期字符串
* @param secondAmount 增加数量。可为负数
* @return 增加秒钟后的日期字符串
*/
public static String addSecond(String date, int secondAmount) throws Exception {
if (StringUtils.isEmpty(date)) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return addInteger(date, Calendar.SECOND, secondAmount);
}
/**
* 增加日期的秒钟。
*
* @param date 日期
* @param secondAmount 增加数量。可为负数
* @return 增加秒钟后的日期
*/
public static Date addSecond(Date date, int secondAmount) throws Exception {
if (date == null) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
return addInteger(date, Calendar.SECOND, secondAmount);
}
/**
* 增加日期中某类型的某数值。如增加日期
*
* @param date 日期字符串
* @param dateType 类型
* @param amount 数值
* @return 计算后日期字符串
*/
private static String addInteger(String date, int dateType, int amount) throws Exception {
if (StringUtils.isEmpty(date)) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
DateStyle dateStyle = getDateStyle(date);
Date myDate = stringToDate(date, dateStyle);
myDate = addInteger(myDate, dateType, amount);
return dateToString(myDate, dateStyle);
}
/**
* 增加日期中某类型的某数值。如增加日期
*
* @param date 日期
* @param dateType 类型
* @param amount 数值
* @return 计算后日期
*/
private static Date addInteger(Date date, int dateType, int amount) {
if (date == null) {
throw new NullPointerException(PARAMETER_EMPTY_ERROR);
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(dateType, amount);
return calendar.getTime();
}
/*
Operation on time difference. -------------------------------
*/
/**
* 获取两个日期相差的天数
*
* @param date 日期字符串
* @param otherDate 另一个日期字符串
* @return 相差天数。
* @throws Exception
*/
public static long getIntervalDays(String date, String otherDate) throws Exception {
return getIntervalDays(stringToDate(date), stringToDate(otherDate));
}
/**
* 计算两个日期相隔的天数
*
* @param date
* @param otherDate
* @return int
* @throws Exception
*/
public static long getIntervalDays(Date date, Date otherDate) {
return (date.getTime()-otherDate.getTime())/oneDay;
}
/**
* 获取期间的年龄
*
* @param date
* @param otherDate
* @return String
* @return String
*/
/*public static String getAge(Date date, Date otherDate) throws Exception {
long dis = DateUtil.getIntervalDays(new Date(), otherDate);
long year = dis / 365;
long month = dis % 365 / 30;
long day = dis % 365 % 31;