DateUtils工具类

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

import lombok.extern.slf4j.Slf4j;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

@Slf4j
public class DateUtils {

//年月日格式
public static final String DATE_FORMAT = "yyyy-MM-dd";
public static final String DATE_FORMAT_CH = "yyyy年MM月dd日";
public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String DATETIME_FORMAT2 = "yyyyMMddHHmmss";
public static final String DATETIME_FORMAT3 = "HH小时mm分钟ss秒";
public static final String DATETIME_FORMAT_LINE = "yyyy-MM-dd HH";
public static final String DEFAULT_TIMEZONE = "GMT+0:00";
public static final String DATETIME_FORMAT4 = "yyyy-MM-dd'T'HH:mm:ss";
public static final String EXCEL_DATETIME = "yyyyMMddHHmmss";
public static final String DATETIME1_FORMAT = "yyyy-MM-dd HH:mm";


/**
* 一天的毫秒数
*/
public static final long MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;

private static final SimpleDateFormat sdf_date_format = new SimpleDateFormat(DATE_FORMAT);

private DateUtils() {
}

/**
* @param date 日期
* @param pattern 日期格式
* @DESC 格式化日期
* @author QYK
* @time 2016-08-02
*/
public static String formatDate(Date date, String pattern) {
if (date == null) throw new IllegalArgumentException("date is null");
if (pattern == null) throw new IllegalArgumentException("pattern is null");
SimpleDateFormat formatter = new SimpleDateFormat(pattern, Locale.CHINESE);
return formatter.format(date);
}

/**
* 将时间转换为 {@value DATETIME_FORMAT} 格式的字符串,使用默认的区域信息。
*
* @param date 要格式化的日期,不可为 null
* @throws IllegalArgumentException 参数为null时抛出此异常
* @author zhouzhsh
* @date 2020/10/22
*/
public static String formatDatetime(Date date) throws IllegalArgumentException {
if (date == null) throw new IllegalArgumentException("date should not be null");
SimpleDateFormat formatter = new SimpleDateFormat(DATETIME_FORMAT);
return formatter.format(date);
}

/**
* 将时间转换为 {@value DATETIME_FORMAT} 格式的字符串,使用默认的区域信息。
*
* @param date 要格式化的日期
* @return 当 date 参数为 null 时返回空串
* @author zhouzhsh
* @date 2020/10/22
*/
public static String formatDatetimeOrEmpty(Date date) {
if (date == null) return "";
SimpleDateFormat formatter = new SimpleDateFormat(DATETIME_FORMAT);
return formatter.format(date);
}

/**
* 将时间转换为 {@value DATETIME_FORMAT} 格式的字符串,使用默认的区域信息。
*
* @param date 要格式化的日期
* @return 当 date 参数为 null 时返回 null
* @author zhouzhsh
* @date 2020/10/22
*/
public static String formatDatetimeOrNull(Date date) {
if (date == null) return null;
SimpleDateFormat formatter = new SimpleDateFormat(DATETIME_FORMAT);
return formatter.format(date);
}

/**
* 将 {@value DATETIME_FORMAT} 格式的字符串解析为 Date 对象,使用默认的区域信息。
*
* @param date 要解析的日期
* @throws IllegalArgumentException 参数无法解析
* @author zhouzhsh
* @date 2020/12/07
* @see Date
*/
public static Date parseDatetime(String date) throws IllegalArgumentException {
if (date == null) {
throw new IllegalArgumentException("参数不可为 null");
}
SimpleDateFormat formatter = new SimpleDateFormat(DATETIME_FORMAT);
try {
return formatter.parse(date);
} catch (ParseException e) {
//throw new IllegalArgumentException("参数不符合格式:" + DATETIME_FORMAT);
return parseSdfDate(date);
}
}

/**
* 将 {@value DATETIME_FORMAT} 格式的字符串解析为 Date 对象,使用默认的区域信息。
*
* @param dateStr 要解析的日期
* @return 当参数无法解析,返回 null
* @author zhouzhsh
* @date 2020/12/07
* @see Date
*/
public static Date parseDatetimeOrNull(String dateStr) {
if (StringUtils.isEmpty(dateStr)) {
return null;
}
SimpleDateFormat formatter = new SimpleDateFormat(DATETIME_FORMAT);
try {
return formatter.parse(dateStr);
} catch (Exception e) {
//return null;
return parseSdfDate(dateStr);
}
}

public static LocalDateTime parseLocalDatetime(String dateStr) {
if (StringUtils.isEmpty(dateStr)) {
return null;
}
Date date = parseDatetime(dateStr);
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
}

/**
* @param date
* @return
* @throws IllegalArgumentException
*/
public static Date parseSdfDate(String date) throws IllegalArgumentException {
if (date == null) {
throw new IllegalArgumentException("参数不可为 null");
}
try {
return sdf_date_format.parse(date);
} catch (ParseException e) {
throw new IllegalArgumentException("参数不符合格式:" + DATETIME_FORMAT);
}
}

/**
* 将 {@value DATETIME_FORMAT} 格式的字符串解析为 Date 对象,使用默认的区域信息。
*
* @param date 要解析的日期
* @date 2022/09/02
* @see Date
*/
public static Date parseDatetime2(String date) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(DATETIME_FORMAT);
return formatter.parse(date);
}


public static String formatDate(Date date) {
return formatDate(date, DATE_FORMAT);
}

/**
* @param date 日期
* @param offset 偏移量(offset= -1 代表上一个月,+1代表下一个月)
* @DESC 获取根据当前月份的偏移月份
* @author QYK
* @time 2016-08-02
*/
public static Date getMonthOffset(Date date, Integer offset) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, offset);
return cal.getTime();
}

/**
* 将日期字符串按指定格式转换成日期类型
*
* @param aMask 指定的日期格式,如:yyyy-MM-dd
* @param strDate 待转换的日期字符串
* @return
* @throws ParseException
* @author zhengxiaojun
* @date 2018-11-22
*/
public static final Date convertStringToDate(String aMask, String strDate)
throws ParseException {
if (strDate == null || "".equals(strDate)) {
return null;
}
SimpleDateFormat df = null;
Date date = null;
df = new SimpleDateFormat(aMask);

if (log.isDebugEnabled()) {
log.debug("converting '" + strDate + "' to date with mask '" + aMask + "'");
}
try {
date = df.parse(strDate);
} catch (ParseException pe) {
log.error("ParseException: " + pe);
throw pe;
}
return (date);
}

public static final Date dateTime(final String date, final String format) {
try {
return new SimpleDateFormat(format).parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}

public static final Date convertStringToDate(String strDate) throws ParseException {
if (StringUtils.isEmpty(strDate)) {
return null;
}
if (strDate.length() == 10) {
return convertStringToDate(DATE_FORMAT, strDate);
} else if (strDate.length() == 13) {
return convertStringToDate("yyyy-MM-dd HH", strDate);
} else if (strDate.length() == 14) {
return convertStringToDate(DATETIME_FORMAT2, strDate);
} else {
return convertStringToDate(DATETIME_FORMAT, strDate);
}
}

public static final String convertFormatString(String strDate) throws ParseException {
if (StringUtils.isEmpty(strDate)) {
return null;
}
if (strDate.length() == 14) {
return formatDatetime(convertStringToDate(DATETIME_FORMAT2, strDate));
} else {
return null;
}
}

public static String getDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(new Date());
}


public static boolean checkDateStr(String dateStr, String pattern) {
try {
if (dateStr == null || dateStr.equals("")) {
return false;
}
convertStringToDate(pattern, dateStr);
} catch (Exception e) {
return false;
}
return true;
}

/**
* 返回日期加X天后的日期
*
* @param date
* @param i
* @return
* @author Li Haolai
* @date 2017-06-08
*/
public static String addDay(String date, int i) {
try {
GregorianCalendar gCal = new GregorianCalendar(
Integer.parseInt(date.substring(0, 4)),
Integer.parseInt(date.substring(5, 7)) - 1,
Integer.parseInt(date.substring(8, 10)));
gCal.add(GregorianCalendar.DATE, i);
return sdf_date_format.format(gCal.getTime());
} catch (Exception e) {
log.debug("DateUtils.addDay():" + e.toString());
return getDate();
}
}

/**
* 返回日期加X天后的日期
*
* @param date
* @param i
* @return
*/
public static Date addDay(Date date, int i) {
if (date == null) {
return null;
}
return org.apache.commons.lang3.time.DateUtils.addDays(date, i);
}

public static Date addHour(Date date, int i) {
if (date == null) {
return null;
}
return org.apache.commons.lang3.time.DateUtils.addHours(date, i);
}

public static Date addMinute(Date date, int i) {
if (date == null) {
return null;
}
return org.apache.commons.lang3.time.DateUtils.addMinutes(date, i);
}

/**
* 获取当前时间到几个月之后的日期
*
* @return
*/
public static Date getNextMonth(Date createTime, int month, int day) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(createTime);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.add(Calendar.MONTH, month);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}

/**
* 获取今天0点0分0秒的日期对象
*
* @author zxh
*/
public static Date getTodayZero() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date zero = calendar.getTime();
return zero;
}

/**
* 获取前后 n 天的0点0分0秒的日期对象
*
* @return
*/
public static Date getNextIntDayZero(int n) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.add(Calendar.DATE, n);
Date zero = calendar.getTime();
return zero;
}


/**
* 校验时期是否在当前时间前 offset 天内
*
* @param sDate
* @param offset
* @return
*/
public static Boolean thisDateAfterToday(String sDate, int offset) {
Date date = DateUtils.parseDatetimeOrNull(sDate);
if (date == null) {
return false;
}
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DAY_OF_MONTH, offset);

Date formatDate = cal.getTime();
return date.after(formatDate);
}

/**
* 根据时间 和时间格式 校验是否正确
*
* @param sDate 校验的日期
* @param format 校验的格式
* @return
*/
public static boolean isLegalDate(String sDate, String format) {
if ((sDate == null) || (sDate.length() != format.length())) {
return false;
}
DateFormat formatter = new SimpleDateFormat(format);
try {
Date date = formatter.parse(sDate);
return sDate.equals(formatter.format(date));
} catch (Exception e) {
return false;
}
}

/**
* 获取月份的第一天
*/
public static String getMonthFirstDay(String month) {
try {
Calendar calendar = Calendar.getInstance();
calendar.set(Integer.parseInt(month.substring(0, 4)), Integer.parseInt(month.substring(5, 7)) - 1, 1);
String firstDayOfMonth = new SimpleDateFormat("yyyy-MM-dd ").format(calendar.getTime());
return firstDayOfMonth;
} catch (Exception e) {
return "";
}
}

/**
* 获取月份的最后一天
*/
public static String getMonthEndDay(String month) {
try {
Calendar calendar = Calendar.getInstance();
calendar.set(Integer.parseInt(month.substring(0, 4)), Integer.parseInt(month.substring(5, 7)), 1);//这里先设置要获取月份的下月的第一天
calendar.add(Calendar.DATE, -1);//这里将日期值减去一天,从而获取到要求的月份最后一天
String lastDayOfMonth = new SimpleDateFormat("yyyy-MM-dd ").format(calendar.getTime());
return lastDayOfMonth;
} catch (Exception e) {
return "";
}
}

/**
* 获取当年的第一天
*
* @return
*/
public static Date getCurrYearFirst() {
Calendar currCal = Calendar.getInstance();
int currentYear = currCal.get(Calendar.YEAR);
return getYearFirst(currentYear);
}

/**
* 获取当年的最后一天
*
* @return
*/
public static Date getCurrYearLast() {
Calendar currCal = Calendar.getInstance();
int currentYear = currCal.get(Calendar.YEAR);
return getYearLast(currentYear);
}


/**
* 获取某年第一天日期
*
* @param year 年份
* @return Date
*/
public static Date getYearFirst(int year) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, year);
Date currYearFirst = calendar.getTime();
return currYearFirst;
}

/**
* 获取某年最后一天日期
*
* @param year 年份
* @return Date
*/
public static Date getYearLast(int year) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, year);
calendar.roll(Calendar.DAY_OF_YEAR, -1);
Date currYearLast = calendar.getTime();

return currYearLast;
}

/**
* 获得当天开始时刻
*
* @return
*/
public static Date initDateByDay() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}

/**
* 获得20天前开始时刻
*
* @return
*/
public static Date initDateByDayDeThree() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY, -72);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}

/**
* 获得当天结束时刻
*
* @return
*/
public static Date endDateByDay() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
return calendar.getTime();
}

/**
* 获得当月开始时刻
*
* @return
*/
public static Date initDateByMonth() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}

/**
* 获得上个月开始时刻
*
* @return
*/
public static Date getLastMonthInit() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.MONTH, -1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}

/**
* 获得当月最后时刻
*
* @return
*/
public static Date endDateByMonth() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, 0);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
return calendar.getTime();
}

/**
* 判断当前时间是否在[startTime, endTime]区间,注意时间格式要一致
*
* @param nowTime 当前时间
* @param startTime 开始时间
* @param endTime 结束时间
* @return
*/
public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
if (startTime == null && endTime == null) {
return true;
}
if (nowTime == null) {
return false;
}

Calendar date = Calendar.getInstance();
date.setTime(nowTime);
Calendar begin = null;
if (startTime != null) {
begin = Calendar.getInstance();
begin.setTime(startTime);
}
Calendar end = null;
if (endTime != null) {
end = Calendar.getInstance();
end.setTime(endTime);
}

if (begin == null) {
return date.before(end) || nowTime.getTime() == endTime.getTime();
}
if (end == null) {
return date.after(begin) || nowTime.getTime() == startTime.getTime();
}
if (nowTime.getTime() == startTime.getTime()
|| nowTime.getTime() == endTime.getTime()) {
return true;
}
return date.after(begin) && date.before(end);
}

/**
* 获取当前日期
*
* @return
*/
public static String getNowDate2Str() {
return getNowDate2Str(DateUtils.DATETIME_FORMAT);
}

public static String getNowDate2Str(String dateFormat) {
return DateUtils.formatDate(new Date(), dateFormat);
}

/**
* 秒转化为小时-分钟-秒
*
* @param seconds
* @return
*/
public static String convertSeconds2Date(long seconds) {
String result = null;
try {
Date date = new Date(seconds * 1000);

SimpleDateFormat df = new SimpleDateFormat(DATETIME_FORMAT3);
df.setTimeZone(TimeZone.getTimeZone(DEFAULT_TIMEZONE));
result = df.format(date);
} catch (Exception e) {
log.error("时间转换异常:", e);
}
return result;
}


/**
* yyyy-MM-dd'T'HH:mm:ss.SSS 转换为时间
*
* @param dateStr
* @return
* @throws ParseException
*/
public static Date parseDatetimeOrNull4(String dateStr) {
try {
if (dateStr == null) return null;
SimpleDateFormat formatter = new SimpleDateFormat(DATETIME_FORMAT4);
return formatter.parse(dateStr);
} catch (Exception e) {
log.error("时间转换异常:", e);
}
return null;
}

/**
* 计算两个日期相差的天数。
* <ul>
* <li>以毫秒进行计算,每{@value MILLISECONDS_PER_DAY}毫秒为1天;</li>
* <li>向下取整,不足{@value MILLISECONDS_PER_DAY}毫秒视为0天;</li>
* <li>计算结果为绝对值,与参数的顺序无关。</li>
* </ul>
*
* @author zhouzhsh 2022/7/20
*/
public static int dateDiffInDays(Date date1, Date date2) {
if (date1 == null || date2 == null) {
throw new IllegalArgumentException("日期参数为空");
}
return (int) (Math.abs(date1.getTime() - date2.getTime()) / MILLISECONDS_PER_DAY);
}

/**
* 计算两个日期相差秒
*
* @param date1
* @param date2
* @return
*/
public static long dateDiffInMilliseconds(Date date1, Date date2) {
if (date1 == null || date2 == null) {
throw new IllegalArgumentException("日期参数为空");
}
return (long) (Math.abs(date1.getTime() - date2.getTime()) / 1000);
}

/**
* 获取当天开始时间
*
* @param date
* @return
*/
public static Date getDayStartTime(Date date) {
if (date == null) {
return null;
}
return DateUtils.parseDatetimeOrNull(DateUtils.formatDate(date, DateUtils.DATE_FORMAT));
}

/**
* 获取当天结束时间
*
* @param date
* @return
*/
public static Date getDayEndTime(Date date) {
if (date == null) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
return calendar.getTime();
}

/**
* 获取一周中的某一天
*
* @param whichDay
* @return
*/
public static Date getDayOfWeek(int whichDay) {
Calendar cld = Calendar.getInstance(Locale.CHINA);
cld.setFirstDayOfWeek(Calendar.MONDAY);//以周一为首日
cld.setTimeInMillis(System.currentTimeMillis());//当前时间

cld.set(Calendar.DAY_OF_WEEK, whichDay);
return cld.getTime();
}

public static Date fromLocalDate(LocalDateTime localDateTime) {
if (localDateTime == null) {
return null;
}
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}

public static String formatDateTime(LocalDateTime localDateTime) {
if (localDateTime == null) {
return null;
}
Date date = fromLocalDate(localDateTime);
return formatDatetimeOrNull(date);
}

}

参考:聊聊 SpringBoot 中的两种占位符:@@ 和 ${}



DateUtils工具类
https://github.com/i-xiaoxin/2023/04/03/DateUtils工具类/
作者
xiaoxinlore
发布于
2023年4月3日
许可协议