DateUtils.java
29.5 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
package com.pashanhoo.common.minio;
import org.springframework.util.StringUtils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.*;
/**
* @author CAIYONGSONG
* @commpany www.pashanhoo.com
* @date 2022/7/20
*/
public class DateUtils {
//获取当天的开始时间
public static Date getDayBegin() {
Calendar cal = new GregorianCalendar();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
//获取当天的结束时间
public static Date getDayEnd() {
Calendar cal = new GregorianCalendar();
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
return cal.getTime();
}
//获取昨天的开始时间
public static Date getBeginDayOfYesterday() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayBegin());
cal.add(Calendar.DAY_OF_MONTH, -1);
return cal.getTime();
}
//获取昨天的结束时间
public static Date getEndDayOfYesterDay() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayEnd());
cal.add(Calendar.DAY_OF_MONTH, -1);
return cal.getTime();
}
//获取明天的开始时间
public static Date getBeginDayOfTomorrow() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayBegin());
cal.add(Calendar.DAY_OF_MONTH, 1);
return cal.getTime();
}
//获取明天的结束时间
public static Date getEndDayOfTomorrow() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayEnd());
cal.add(Calendar.DAY_OF_MONTH, 1);
return cal.getTime();
}
//获取本周的开始时间
@SuppressWarnings("unused")
public static Date getBeginDayOfWeek() {
Date date = new Date();
if (date == null) {
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
if (dayofweek == 1) {
dayofweek += 7;
}
cal.add(Calendar.DATE, 2 - dayofweek);
return getDayStartTime(cal.getTime());
}
//获取本周的结束时间
public static Date getEndDayOfWeek(){
Calendar cal = Calendar.getInstance();
cal.setTime(getBeginDayOfWeek());
cal.add(Calendar.DAY_OF_WEEK, 6);
Date weekEndSta = cal.getTime();
return getDayEndTime(weekEndSta);
}
//获取上周的开始时间
@SuppressWarnings("unused")
public static Date getBeginDayOfLastWeek() {
Date date = new Date();
if (date == null) {
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
if (dayofweek == 1) {
dayofweek += 7;
}
cal.add(Calendar.DATE, 2 - dayofweek - 7);
return getDayStartTime(cal.getTime());
}
//获取上周的结束时间
public static Date getEndDayOfLastWeek(){
Calendar cal = Calendar.getInstance();
cal.setTime(getBeginDayOfLastWeek());
cal.add(Calendar.DAY_OF_WEEK, 6);
Date weekEndSta = cal.getTime();
return getDayEndTime(weekEndSta);
}
//获取本月的开始时间
public static Date getBeginDayOfMonth() {
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 1, 1);
return getDayStartTime(calendar.getTime());
}
//获取本月的结束时间
public static Date getEndDayOfMonth() {
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 1, 1);
int day = calendar.getActualMaximum(5);
calendar.set(getNowYear(), getNowMonth() - 1, day);
return getDayEndTime(calendar.getTime());
}
//获取上月的开始时间
public static Date getBeginDayOfLastMonth() {
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 2, 1);
return getDayStartTime(calendar.getTime());
}
//获取上月的结束时间
public static Date getEndDayOfLastMonth() {
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 2, 1);
int day = calendar.getActualMaximum(5);
calendar.set(getNowYear(), getNowMonth() - 2, day);
return getDayEndTime(calendar.getTime());
}
//获取本年的开始时间
public static Date getBeginDayOfYear() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, getNowYear());
// cal.set
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DATE, 1);
return getDayStartTime(cal.getTime());
}
//获取本年的结束时间
public static Date getEndDayOfYear() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, getNowYear());
cal.set(Calendar.MONTH, Calendar.DECEMBER);
cal.set(Calendar.DATE, 31);
return getDayEndTime(cal.getTime());
}
//获取某个日期的开始时间
public static Date getDayStartTime(Date d) {
Calendar calendar = Calendar.getInstance();
if(null != d) calendar.setTime(d);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
//获取某个日期的结束时间
public static Date getDayEndTime(Date d) {
Calendar calendar = Calendar.getInstance();
if(null != d) calendar.setTime(d);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
calendar.set(Calendar.MILLISECOND, 999);
return calendar.getTime();
}
//获取今年是哪一年
public static Integer getNowYear() {
Date date = new Date();
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
return Integer.valueOf(gc.get(1));
}
//获取本月是哪一月
public static int getNowMonth() {
Date date = new Date();
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
return gc.get(2) + 1;
}
//两个日期相减得到的天数
public static int getDiffDays(Date beginDate, Date endDate) {
if (beginDate == null || endDate == null) {
throw new IllegalArgumentException("getDiffDays param is null!");
}
long diff = (endDate.getTime() - beginDate.getTime())
/ (1000 * 60 * 60 * 24);
int days = new Long(diff).intValue();
return days;
}
//两个日期相减得到的毫秒数
public static long dateDiff(Date beginDate, Date endDate) {
long date1ms = beginDate.getTime();
long date2ms = endDate.getTime();
return date2ms - date1ms;
}
//获取两个日期中的最大日期
public static Date max(Date beginDate, Date endDate) {
if (beginDate == null) {
return endDate;
}
if (endDate == null) {
return beginDate;
}
if (beginDate.after(endDate)) {
return beginDate;
}
return endDate;
}
public static final String DATEFORMAT = "yyyyMMdd";
public static final String DATE_FORMAT = "yyyy-MM-dd";
public static final String DATA_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
/**
* 日期转换为"yyyy-MM-dd"格式字符串
*
* @param date 需要转换的日期
* @return String 转换后的日期串
*/
public static String dateToStr(Date date) {
DateFormat df = new SimpleDateFormat(DATEFORMAT);
return df.format(date);
}
/**
* 日期转换为"yyyy-MM-dd"格式字符串
*
* @param date 需要转换的日期
* @return String 转换后的日期串
*/
public static String dateToString(Date date) {
DateFormat df = new SimpleDateFormat(DATE_FORMAT);
return df.format(date);
}
/**
* 日期转换为"yyyy-MM-dd HH:mm:ss"格式字符串
*
* @param date
* 需要转换的日期
* @return 转换后的日期串
*/
public static String dateToStringAll(Date date) {
DateFormat df = new SimpleDateFormat(DATA_TIME_FORMAT);
return df.format(date);
}
//获取两个日期中的最小日期
public static Date min(Date beginDate, Date endDate) {
if (beginDate == null) {
return endDate;
}
if (endDate == null) {
return beginDate;
}
if (beginDate.after(endDate)) {
return endDate;
}
return beginDate;
}
//返回某月该季度的第一个月
public static Date getFirstSeasonDate(Date date) {
final int[] SEASON = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 };
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int sean = SEASON[cal.get(Calendar.MONTH)];
cal.set(Calendar.MONTH, sean * 3 - 3);
return cal.getTime();
}
//返回某个日期下几天的日期
public static Date getNextDay(Date date, int i) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i);
return cal.getTime();
}
//返回某个日期前几天的日期
public static Date getFrontDay(Date date, int i) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i);
return cal.getTime();
}
//获取某年某月到某年某月按天的切片日期集合(间隔天数的集合)
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List getTimeList(int beginYear, int beginMonth, int endYear,
int endMonth, int k) {
List list = new ArrayList();
if (beginYear == endYear) {
for (int j = beginMonth; j <= endMonth; j++) {
list.add(getTimeList(beginYear, j, k));
}
} else {
{
for (int j = beginMonth; j < 12; j++) {
list.add(getTimeList(beginYear, j, k));
}
for (int i = beginYear + 1; i < endYear; i++) {
for (int j = 0; j < 12; j++) {
list.add(getTimeList(i, j, k));
}
}
for (int j = 0; j <= endMonth; j++) {
list.add(getTimeList(endYear, j, k));
}
}
}
return list;
}
//获取某年某月按天切片日期集合(某个月间隔多少天的日期集合)
@SuppressWarnings({ "unchecked", "rawtypes" })
public static List getTimeList(int beginYear, int beginMonth, int k) {
List list = new ArrayList();
Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1);
int max = begincal.getActualMaximum(Calendar.DATE);
for (int i = 1; i < max; i = i + k) {
list.add(begincal.getTime());
begincal.add(Calendar.DATE, k);
}
begincal = new GregorianCalendar(beginYear, beginMonth, max);
list.add(begincal.getTime());
return list;
}
//给当前时间加上min分钟
public static Date getNewDate(Date cur,Integer min ) {
Calendar c = Calendar.getInstance();
c.setTime(cur); //设置时间
c.add(Calendar.MINUTE, min); //日期分钟加30,Calendar.DATE(天),Calendar.HOUR(小时)
Date date = c.getTime(); //结果
return date;
}
/**
* 时间格式(yyyy-MM-dd)
*/
public final static String DATE_PATTERN = "yyyy-MM-dd";
/**
* 时间格式(yyyy-MM-dd HH:mm:ss)
*/
public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
/**
* 比较两个字符串时间大小
*/
public static int compareTwoTime(String time1, String time2) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
int flagValue = 0;
try {
Date date1, date2;
date1 = simpleDateFormat.parse(time1);
date2 = simpleDateFormat.parse(time2);
long millisecond = date1.getTime() - date2.getTime();
if (millisecond > 0) {
flagValue = 1;
} else if (millisecond < 0) {
flagValue = -1;
} else if (millisecond == 0) {
flagValue = 0;
}
} catch (ParseException e) {
e.printStackTrace();
}
return (flagValue);
}
/**
* 比较两个时间差
* time1>time2=1
* time1<time2=-1
* time1=time2=0
*
* @param time1
* @param time2
* @return
*/
public static int compareTwoTime(Date time1, Date time2) {
int flagValue = 0;
try {
long millisecond = time1.getTime() - time2.getTime();
if (millisecond > 0) {
flagValue = 1;
} else if (millisecond < 0) {
flagValue = -1;
} else if (millisecond == 0) {
flagValue = 0;
}
} catch (Exception e) {
e.printStackTrace();
}
return (flagValue);
}
/**
* 比较两个时间相差天数
*/
public static float calculateTimeGapDay(String time1, String time2) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
float day = 0;
Date date1 = null;
Date date2 = null;
try {
date1 = simpleDateFormat.parse(time1);
date2 = simpleDateFormat.parse(time2);
long millisecond = date2.getTime() - date1.getTime();
day = millisecond / (24 * 60 * 60 * 1000);
} catch (ParseException e) {
e.printStackTrace();
}
return (day);
}
/**
* 比较两个时间相差天数
*/
public static float calculateTimeGapDay(Date time1, Date time2) {
float day = 0;
try {
Date date1, date2;
date1 = time1;
date2 = time2;
long millisecond = date2.getTime() - date1.getTime();
day = millisecond / (24 * 60 * 60 * 1000);
} catch (Exception e) {
e.printStackTrace();
}
return (day);
}
/**
* 比较两个时间相差小时
*/
public static double calculatetimeGapHour(String time1, String time2) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
double hour = 0;
try {
Date date1, date2;
date1 = simpleDateFormat.parse(time1);
date2 = simpleDateFormat.parse(time2);
double millisecond = date2.getTime() - date1.getTime();
hour = millisecond / (60 * 60 * 1000);
} catch (ParseException e) {
e.printStackTrace();
}
return hour;
}
/**
* 比较两个时间相差小时
*/
public static double calculatetimeGapHour(Date date1, Date date2) {
double hour = 0;
double millisecond = date2.getTime() - date1.getTime();
hour = millisecond / (60 * 60 * 1000);
return hour;
}
/**
* 比较两个时间相差分钟
*/
public static double calculatetimeGapMinute(String time1, String time2) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
double minute = 0;
try {
Date date1, date2;
date1 = simpleDateFormat.parse(time1);
date2 = simpleDateFormat.parse(time2);
double millisecond = date2.getTime() - date1.getTime();
minute = millisecond / (60 * 1000);
} catch (ParseException e) {
e.printStackTrace();
}
return minute;
}
/**
* 比较两个时间相差分钟
*/
public static double calculatetimeGapMinute(Date date1, Date date2) {
double minute = 0;
double millisecond = date2.getTime() - date1.getTime();
minute = millisecond / (60 * 1000);
return minute;
}
/**
* 比较两个时间相差秒
*/
public static double calculatetimeGapSecond(String time1, String time2) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
double second = 0;
try {
Date date1, date2;
date1 = simpleDateFormat.parse(time1);
date2 = simpleDateFormat.parse(time2);
double millisecond = date2.getTime() - date1.getTime();
second = millisecond / (1000);
} catch (ParseException e) {
e.printStackTrace();
}
return second;
}
/**
* 比较两个时间相差秒
*/
public static double calculatetimeGapSecond(Date date1, Date date2) {
double second = 0;
double millisecond = date2.getTime() - date1.getTime();
second = millisecond / (1000);
return second;
}
/**
* 时间工具
*
* @param ctime 入参
* @return 返回
*/
public static String showTime(Date ctime) {
String r = "";
if (ctime == null) {
return r;
}
String format = "yyyy-MM-dd HH:mm:ss";
long nowtimelong = System.currentTimeMillis();
long ctimelong = ctime.getTime();
long result = Math.abs(nowtimelong - ctimelong);
if (result < 60000) {// 一分钟内
long seconds = result / 1000;
if (seconds == 0) {
r = "刚刚";
} else {
r = seconds + "秒前";
}
} else if (result >= 60000 && result < 3600000) {// 一小时内
long seconds = result / 60000;
r = seconds + "分钟前";
} else if (result >= 3600000 && result < 86400000) {// 一天内
long seconds = result / 3600000;
r = seconds + "小时前";
} else if (result >= 86400000 && result < 1702967296) {// 三十天内
long seconds = result / 86400000;
r = seconds + "天前";
} else {// 日期格式
SimpleDateFormat df = new SimpleDateFormat(format);
r = df.format(ctime);
}
return r;
}
/**
* 根据对应语言-显示对应格式
* languageCode:国际语言编码:en zh
* date:需处理的日期
*/
public static String formatDateByObject(String languageCode, Object date) {
if (StringUtils.isEmpty(languageCode) || date == null || StringUtils.isEmpty(date.toString())) {
return "";
} else {
return DateFormat.getDateInstance(DateFormat.MEDIUM, new Locale(languageCode)).format(date);
}
}
/**
* 根据对应语言-显示对应格式
* languageCode:国际语言编码:en zh
* date:需处理的日期
* 注:通过sql语句查询出来的date不能转换成Date类型来调用此方法 应直接调用用formatDateByObject方法
*/
public static String formatDateByDate(String languageCode, Date date) {
if (StringUtils.isEmpty(languageCode) || date == null || StringUtils.isEmpty(date.toString())) {
return "";
} else {
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, new Locale(languageCode));
return df.format(date);
}
}
/**
* 获取当前时间 默认返回 yyyy-MM-dd HH:mm:ss
*
* @return 当前时间字符串
*/
public static String nowTime(String pattern) {
//return new SimpleDateFormat(StringUtils.isEmpty(pattern) ? "yyyy-MM-dd HH:mm:ss" : pattern).format(new Date());
return localDateTimeToString(LocalDateTime.now(), StringUtils.isEmpty(pattern) ? "yyyy-MM-dd HH:mm:ss" : pattern);
}
/**
* 获取当前日期 默认返回 yyyy-MM-dd
*
* @return 当前日期字符串
*/
public String today(String pattern) {
//return new SimpleDateFormat(StringUtils.isEmpty(pattern) ? "yyyy-MM-dd" : pattern).format(new Date());
return localDateToString(LocalDate.now(), StringUtils.isEmpty(pattern) ? "yyyy-MM-dd" : pattern);
}
/**
* 日期格式化 Date 转 String
* 默认返回 yyyy-MM-dd HH:mm:ss
*
* @param date 日期 必填
* @param pattern 转换格式
* @return 返回格式后日期
*/
public static String dateToString(Date date, String pattern) {
if (date == null) {
return null;
} else {
return new SimpleDateFormat(StringUtils.isEmpty(pattern) ? "yyyy-MM-dd HH:mm:ss" : pattern).format(date);
}
}
/**
* String 转 LocalDate
*
* @param date 待转换的字符串 必填
* @param pattern 转换格式 默认为yyyy-MM-dd
* @return
*/
public static LocalDate stringToLocalDate(String date, String pattern) {
if (StringUtils.isEmpty(date)) {
return null;
} else {
return LocalDate.parse(date, java.time.format.DateTimeFormatter.ofPattern(StringUtils.isEmpty(pattern) ? "yyyy-MM-dd" : pattern));
}
}
/**
* String 转 LocalDateTime
*
* @param date 待转换的字符串 必填
* @param pattern 转换格式 默认为yyyy-MM-dd HH:mm:ss
* @return
*/
public static LocalDateTime stringToLocalDateTime(String date, String pattern) {
if (StringUtils.isEmpty(date)) {
return null;
} else {
return LocalDateTime.parse(date, java.time.format.DateTimeFormatter.ofPattern(StringUtils.isEmpty(pattern) ? "yyyy-MM-dd HH:mm:ss" : pattern));
}
}
/**
* String 转 LocalTime
*
* @param date 待转换的字符串 必填
* @param pattern 转换格式 默认为HH:mm:ss
* @return
*/
public static LocalTime stringToLocalTime(String date, String pattern) {
if (StringUtils.isEmpty(date)) {
return null;
} else {
return LocalTime.parse(date, java.time.format.DateTimeFormatter.ofPattern(StringUtils.isEmpty(pattern) ? "HH:mm:ss" : pattern));
}
}
/**
* LocalDate 转 String
*
* @param localDate
* @param pattern
* @return
*/
public static String localDateToString(LocalDate localDate, String pattern) {
return localDate.format(java.time.format.DateTimeFormatter.ofPattern(StringUtils.isEmpty(pattern) ? "yyyy-MM-dd" : pattern));
}
/**
* LocalDateTime 转 String
*
* @param localDateTime
* @param pattern
* @return
*/
public static String localDateTimeToString(LocalDateTime localDateTime, String pattern) {
return localDateTime.format(java.time.format.DateTimeFormatter.ofPattern(StringUtils.isEmpty(pattern) ? "yyyy-MM-dd HH:mm:ss" : pattern));
}
/**
* LocalTime 转 String
*
* @param localTime
* @param pattern
* @return
*/
public static String localTimeToString(LocalTime localTime, String pattern) {
return localTime.format(java.time.format.DateTimeFormatter.ofPattern(StringUtils.isEmpty(pattern) ? "HH:mm:ss" : pattern));
}
/**
* LocalDate 转 Date
*
* @param localDate
* @return
*/
public static Date localDateToDate(LocalDate localDate) {
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
return Date.from(zdt.toInstant());
}
/**
* LocalDateTime 转 Date
*
* @param localDateTime
* @return
*/
public static Date localDateTimeToDate(LocalDateTime localDateTime) {
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zdt = localDateTime.atZone(zoneId);
return Date.from(zdt.toInstant());
}
/**
* LocalDateTime 转 LocalDate
*
* @param localDateTime
* @return
*/
public static LocalDate localDateTimeToLocalDate(LocalDateTime localDateTime) {
return localDateTime.toLocalDate();
}
/**
* LocalDateTime 转 LocalTime
*
* @param localDateTime
* @return
*/
public static LocalTime localDateTimeToLocalTime(LocalDateTime localDateTime) {
return localDateTime.toLocalTime();
}
/**
* Date 转 LocalDate
* atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。
*
* @param date
* @return
*/
public static LocalDate dateToLocalDate(Date date) {
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
return instant.atZone(zoneId).toLocalDate();
}
/**
* 将 Date 转 LocalDateTime
* atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。
*
* @param date
* @return
*/
public static LocalDateTime dateToLocalDateTime(Date date) {
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
return instant.atZone(zoneId).toLocalDateTime();
}
/**
* 将 Date 转 LocalTime
* atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。
*
* @param date
* @return
*/
public static LocalTime dateToLocalTime(Date date) {
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
return instant.atZone(zoneId).toLocalTime();
}
/**
* 计算两个LocalDateTime yyyy-MM-dd HH:mm:ss 之间的 时间间隔
*
* @param localDateTime1
* @param localDateTime2
* @return Map<String, Object>
* millis:毫秒差
* seconds:秒差
* minutes:分钟差
* hours:小时差
* days:天数差
* weeks:周差
* months:月份差
* quarters:季度差
* years:年份差
*/
public static Map<String, Object> minusToLocalDateTime(LocalDateTime localDateTime1, LocalDateTime localDateTime2) {
Duration duration = Duration.between(localDateTime1, localDateTime2);
Map<String, Object> map = new HashMap<>(16);
//毫秒差
map.put("millis", duration.toMillis());
//秒差
map.put("seconds", duration.toMillis() / 1000);
//分钟差
map.put("minutes", duration.toMinutes());
//小时差
map.put("hours", duration.toHours());
//天数差
map.put("days", duration.toDays());
//周差
map.put("weeks", duration.toDays() / 7);
Map<String, Object> map1 = minusToLocalDate(localDateTime1.toLocalDate(), localDateTime2.toLocalDate());
//季度差
map.put("quarters", map1.get("quarters"));
//月份差
map.put("months", map1.get("months"));
//年份差
map.put("years", map1.get("years"));
return map;
}
/**
* 计算两个LocalTime HH:mm:ss 之间的 时间间隔
*
* @param localTime1
* @param localTime2
* @return Map<String, Object>
* millis:毫秒差
* seconds:秒差
* minutes:分钟差
* hours:小时差
*/
public static Map<String, Object> minusToLocalTime(LocalTime localTime1, LocalTime localTime2) {
Duration duration = Duration.between(localTime1, localTime2);
Map<String, Object> map = new HashMap<>(16);
//毫秒差
map.put("millis", duration.toMillis());
//秒差
map.put("seconds", duration.toMillis() / 1000);
//分钟差
map.put("minutes", duration.toMinutes());
//小时差
map.put("hours", duration.toHours());
return map;
}
/**
* 计算两个LocalDate yyyy-MM-dd 之间的 时间间隔
*
* @param localDate1
* @param localDate2
* @return Map<String, Object>
* days:天数差
* weeks:周差
* months:月份差
* quarters:季度差
* years:年份差
*/
public static Map<String, Object> minusToLocalDate(LocalDate localDate1, LocalDate localDate2) {
Period period = localDate1.until(localDate2);
Map<String, Object> map = new HashMap<>(16);
//天数差
map.put("days", period.getDays());
//周差
map.put("weeks", period.getDays() / 7);
//月份差
map.put("months", period.getMonths());
// 季度差
map.put("quarters", period.getMonths() / 3);
//年份差
map.put("years", period.getYears());
return map;
}
/**
* 根据时间 和时间格式 校验是否正确
* 示例:
* String date = "2020-01-25 12:36:45";
* 传入date的长度、data、"yyyy-MM-dd HH:mm:ss" = true 《或者》 传入date的长度、data、"yyyy-MM-dd" = false
* @param length 校验的长度
* @param sDate 校验的日期
* @param format 校验的格式
* @return
*/
public static boolean isLegalDate(int length, String sDate,String format) {
int legalLen = length;
if ((sDate == null) || (sDate.length() != legalLen)) {
return false;
}
DateFormat formatter = new SimpleDateFormat(format);
try {
Date date = formatter.parse(sDate);
return sDate.equals(formatter.format(date));
} catch (Exception e) {
return false;
}
}
}