skyouc
2025-03-05 bc76a082becae4e5d344ae1b193582be38761d2d
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
package com.vincent.rsf.server.common.utils;
 
import org.apache.tika.utils.StringUtils;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
 
/**
 * 日期时间工具类
 *
 * @author Ryan
 * @createTime 2018-07-19 10:42:00
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
 
    public final static String YYYYMMDDHHMMSS_PATTER = "yyyyMMddHHmmss";
 
    public final static String YYYYMMDDHHMMSS_PATTER_HB = "YYYY-MM-dd HH:mm:ss.SSS";
    public final static String YYYYMMDDHHMMSS_PATTER_HB1 = "YYYY-MM-dd HH:mm:ss.000";
 
    public static String datetimeFormat = "yyyy-MM-dd HH:mm:ss";
 
    public final static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM/dd", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM.dd", "yyyy.MM", "YYYY-MM-dd HH:mm:ss.SSS"};
 
    public static Date now() {
        return Calendar.getInstance().getTime();
    }
 
    /**
     * 仅显示年月日,例如 2021-08-11.
     */
    public static final String DATE_PATTERN = "yyyy-MM-dd";
 
 
    /**
     * 一天的结束时间,仅显示时分秒
     */
    private static final String END_TIME = "23:59:59";
 
    /**
     * @Description: 返回 yyyy-MM-dd HH:mm:ss
     */
    public static String nowDate() {
        return getDate(new Date());
    }
 
    /**
     * 将日期字符串转换为日期类型
     *
     * @param pattern 日期字符串格式 不传则为 yyyy-MM-dd
     * @return
     * @throws ParseException
     */
    public static Date nowDate(String pattern) throws ParseException {
        if (StringUtils.isBlank(pattern)) {
            pattern = "yyyy-MM-dd";
        }
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.parse(df.format(new Date()));
    }
 
    /**
     * 获取指定日期当周星期一的时间
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //获取本周第一天
        calendar.set(Calendar.WEEK_OF_YEAR, calendar.get(Calendar.WEEK_OF_YEAR));
        int dayOfWeek = 0;
        if (calendar.get(Calendar.DAY_OF_WEEK) == 1) {
            dayOfWeek = -6;
        } else {
            dayOfWeek = 2 - calendar.get(Calendar.DAY_OF_WEEK);
        }
        calendar.add(Calendar.DAY_OF_WEEK, dayOfWeek);
 
        return calendar.getTime();
    }
 
    public static Date getFirstDayOfWeekT(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //获取本周第一天
        calendar.set(Calendar.WEEK_OF_YEAR, calendar.get(Calendar.WEEK_OF_YEAR));
        int dayOfWeek = 0;
        if (calendar.get(Calendar.DAY_OF_WEEK) == 1) {
            dayOfWeek = -6;
        } else {
            dayOfWeek = 2 - calendar.get(Calendar.DAY_OF_WEEK);
        }
        calendar.add(Calendar.DAY_OF_WEEK, dayOfWeek);
 
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat(DATE_PATTERN);
 
        try {
            return df.parse(df.format(calendar.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 获取指定日期当周星期日的时间
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(date);
        // Sunday
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() + 6);
 
        return calendar.getTime();
    }
 
    /**
     * 获取指定日期当月第一天的时间
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }
 
    /**
     * 获取指定日期当月最后一天的时间
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }
 
    /**
     * 获取指定日期当年第一天的时间
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, date.getYear() + 1900);
        return calendar.getTime();
    }
 
    /**
     * 获取指定日期当年最后一天的时间
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, date.getYear() + 1900);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        return calendar.getTime();
    }
 
    /**
     * 获取两个日期相隔的天数
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static float getDateDiffOfDays(Date startDate, Date endDate) {
        long diff = endDate.getTime() - startDate.getTime();
 
        return diff / (24 * 60 * 60 * 1000);
    }
 
    /**
     * 获取每天工作小时数。例如:早上9点上班,下午6点下班,休息一个小时,工作时间为8
     *
     * @param startTime 开始时间,格式:HHmm。例如早上7点半,使用0730表示
     * @param endTime   结束时间,格式:HHmm。例如下午6点半,使用1830表示
     * @param restHours 休息时间
     * @return
     */
    public static float getWorkHours(String startTime, String endTime, float restHours) {
        Date m = new Date();
        m.setHours(Integer.parseInt(startTime.substring(0, 2)) - 1);
        m.setMinutes(Integer.parseInt(startTime.substring(2)));
        Date a = new Date();
        a.setHours(Integer.parseInt(endTime.substring(0, 2)) - 1);
        a.setMinutes(Integer.parseInt(endTime.substring(2)));
 
        return (a.getTime() - m.getTime()) / (1000 * 60 * 60 * 1.0f) - restHours;
    }
 
    /**
     * 将日期字符串转换为日期类型
     *
     * @param dateStr 日期字符串
     * @param pattern 日期字符串格式
     * @return
     * @throws ParseException
     */
    public static Date parse(String dateStr, String pattern) throws ParseException {
        if (StringUtils.isBlank(dateStr)) {
            return null;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.parse(dateStr);
    }
 
    /**
     * 日期型字符串转化为日期 格式
     * { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
     * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm",
     * "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
     */
    public static Date parse(String str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str, parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }
 
    public static String formatToAnotherPattern(String dateStr, String pattern, String anotherPatter) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
 
        Date date = dateFormat.parse(dateStr);
        dateFormat = new SimpleDateFormat(anotherPatter);
        return dateFormat.format(date);
    }
 
    /**
     * 获取日期时间字符串
     *
     * @param date 需要转化的日期时间
     * @return String 日期时间字符串,例如 2015-08-11
     */
    public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }
 
    public static String format(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }
 
    public static Date addHours(Date date, int amount) {
        return add(date, Calendar.HOUR_OF_DAY, amount);
    }
 
    public static Date addDays(Date date, int amount) {
        return add(date, Calendar.DAY_OF_MONTH, amount);
    }
 
    public static Date addYear(Date date, int amount) {
        return add(date, Calendar.YEAR, amount);
    }
 
    public static Date addMonth(Date date, int amount) {
        return add(date, Calendar.MONTH, amount);
    }
 
 
    private static Date add(Date date, int calendarField, int amount) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(calendarField, amount);
        return c.getTime();
    }
 
    /**
     * 将毫秒格式化,如果大于60秒,返回 1分xx秒,大于60分钟,返回 1小时xx分xx秒
     *
     * @param ms 毫秒
     * @return
     */
    public static String formatTime(long ms) {
        if (ms < 1000) { //毫秒
            return ms + "毫秒";
        } else if (ms / 1000 < 60) {
            // 小于60秒,以秒显示
            return ms / 1000 + "秒";
        } else if (ms / 1000 / 60 < 60) {
            // 小于60分,以分显示
            return ms / 1000 / 60 + "分" + (ms / 1000 % 60) + "秒";
        } else if (ms / 1000 / 60 / 60 < 24) {
            // 小于24小时
            return (ms / 1000 / 60 / 60) + "时" + (ms / 1000 % 60) + "分" + (ms / 1000 / 60 % 60) + "秒";
        } else {
            return (ms / 1000 / 60 / 60 / 24) + "天" + (ms / 1000 / 60 / 60) + "时" + (ms / 1000 % 60) + "分" + (ms / 1000 / 60 % 60) + "秒";
        }
    }
 
    public static Date dateToISODate(Date date) {
        //T代表后面跟着时间,Z代表UTC统一时间
        SimpleDateFormat format =
                new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
        String isoDate = format.format(date);
        try {
            return format.parse(isoDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
 
 
    public static Date localDateTime2Date(LocalDateTime localDateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        //Combines this date-time with a time-zone to create a  ZonedDateTime.
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        //Tue Mar 27 14:17:17 CST 2018
        return Date.from(zdt.toInstant());
    }
 
    public static String localDateTimeToString(LocalDateTime localDateTime) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(datetimeFormat);
        return fmt.format(localDateTime);
    }
 
    public static LocalDateTime stringToLocalDateTime(String str) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(datetimeFormat);
        return LocalDateTime.parse(str, fmt);
    }
 
    public static LocalDateTime stringToLocalDateTime(String str, String fmtStr) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(fmtStr);
        return LocalDateTime.parse(str, fmt);
    }
 
    /**
     * Date转LocalDateTime
     *
     * @param date Date
     * @return LocalDateTime
     */
    public static LocalDateTime dateToLocalDateTime(Date date) {
        try {
            Instant instant = date.toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            return instant.atZone(zoneId).toLocalDateTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * @Description:获取当前日期 yyyy-MM-dd HH:mm:ss
     * @Author wyt
     * @Date 2020/8/21
     */
    public static String getDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(datetimeFormat);
        return sdf.format(date);
    }
 
    public static String getDate(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }
 
    /**
     * @Description: 判断是否是合法日期格式
     * @Author wyt
     * @Date 2021/2/24
     */
    public static boolean isValidDate(String str, String fmt) {
        if (StringUtils.isBlank(str)) {
            return false;
        }
        boolean convertSuccess = true;
        // 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
        SimpleDateFormat format = new SimpleDateFormat(fmt);
        try {
            // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
            format.setLenient(false);
            Date date = format.parse(str);
            long time = date.getTime();
            if (time < 14400) {
                convertSuccess = false;
            }
        } catch (ParseException e) {
            // e.printStackTrace();
            // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
            convertSuccess = false;
        }
        return convertSuccess;
    }
 
 
    /**
     * 计算两个日期相差的秒数
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static int calLastedTime(Date startDate, Date endDate) {
        long a = endDate.getTime();
        long b = startDate.getTime();
        int c = (int) ((a - b) / 1000);
        return c;
    }
 
    /**
     * 获取本月的第一天
     *
     * @return Calendar 日历
     */
    public static Calendar getStartDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar;
    }
 
    /**
     * 根据日历返回日期时间字符串
     *
     * @param calendar 日历
     * @return String 日期时间字符串
     */
    public static String getDateTimeStr(Calendar calendar) {
        StringBuffer buf = new StringBuffer("");
 
        buf.append(calendar.get(Calendar.YEAR));
        buf.append("-");
        buf.append(calendar.get(Calendar.MONTH) + 1 > 9 ? calendar.get(Calendar.MONTH) + 1 + ""
                : "0" + (calendar.get(Calendar.MONTH) + 1));
        buf.append("-");
        buf.append(calendar.get(Calendar.DAY_OF_MONTH) > 9 ? calendar.get(Calendar.DAY_OF_MONTH) + ""
                : "0" + calendar.get(Calendar.DAY_OF_MONTH));
        buf.append(" ");
        buf.append(calendar.get(Calendar.HOUR_OF_DAY) > 9 ? calendar.get(Calendar.HOUR_OF_DAY) + ""
                : "0" + calendar.get(Calendar.HOUR_OF_DAY));
        buf.append(":");
        buf.append(calendar.get(Calendar.MINUTE) > 9 ? calendar.get(Calendar.MINUTE) + ""
                : "0" + calendar.get(Calendar.MINUTE));
        buf.append(":");
        buf.append(calendar.get(Calendar.SECOND) > 9 ? calendar.get(Calendar.SECOND) + ""
                : "0" + calendar.get(Calendar.SECOND));
        return buf.toString();
    }
 
 
    /**
     * 获取指定日期当月第一天的日期字符串
     *
     * @param date 指定日期
     * @return String 格式:yyyy-MM-dd HH:mm:ss
     */
    public static String getMonthStartTimeStr(Date date) {
        return getDateTimeStr(getStartDayOfMonth(date));
    }
 
 
    /**
     * 获得指定日期所在日的结束时间字符串
     *
     * @param date 指定日期
     * @return String 例如:2021-08-11 23:59:59
     */
    public static String getDateEndTimeStr(Date date) {
        String result = format(date, DATE_PATTERN);
        return result.concat(" ").concat(END_TIME);
    }
 
 
    /**
     * 时间戳转日期
     *
     * @param msStr 时间戳
     * @return 不为空:yyyy-MM-dd HH:mm:ss格式日期,为空:返回null
     */
    public static String transForDate(String msStr) {
        if (StringUtils.isBlank(msStr)) {
            return null;
        }
 
        long msl = Long.parseLong(msStr);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(msl);
    }
 
    /**
     * 判断是否是时间戳
     *
     * @param msStr 时间戳
     * @return 不为空,并且是时间戳格式,返回true
     */
    public static boolean isTimeStamp(String msStr) {
        if (StringUtils.isBlank(msStr)) {
            return false;
        }
        boolean is = false;
        try {
            if (transForDate(msStr) != null) {
                is = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return is;
    }
 
    /**
     * 如果endDate>startDate,返回true,否则返回false
     *
     * @param startDate 开始日期字符串
     * @param endDate   结束日期字符串
     * @return boolean
     */
    public static boolean compareDate(Date startDate, Date endDate) {
        String startDateStr = format(startDate, DATE_PATTERN);
        String endDateStr = format(endDate, DATE_PATTERN);
        return compareDate(startDateStr, endDateStr);
    }
 
    /**
     * @param thisDate  –  当前日期
     * @param otherDate – 比较日期
     * @Description:把时间格式化到秒级继续比较 yyyy-MM-dd HH:mm:ss
     * 如果thisDate>otherDate,返回 1,
     * thisDate=otherDate,   返回 0,
     * thisDate<otherDate,   返回 -1,
     * @Returns int
     **/
    public static int compareDateBySecond(Date thisDate, Date otherDate) {
        String thisDateStr = format(thisDate, datetimeFormat);
        String otherDateStr = format(otherDate, datetimeFormat);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datetimeFormat);
        try {
            Date tDate = simpleDateFormat.parse(thisDateStr);
            Date oDate = simpleDateFormat.parse(otherDateStr);
            return tDate.compareTo(oDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return thisDate.compareTo(otherDate);
    }
 
    public static boolean compareDate(String startDateStr, String endDateStr) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_PATTERN);
        try {
            Date startDate = simpleDateFormat.parse(startDateStr);
            Date endDate = simpleDateFormat.parse(endDateStr);
            return endDate.after(startDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
 
    /**
     * 指定时间往前或往后几天
     *
     * @param day 天数
     * @return
     */
    public static String getTimeBeforeDay(int day, String fmt) {
        if (StringUtils.isBlank(fmt)) {
            fmt = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date nowDate = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);
        calendar.add(Calendar.DATE, day);
        Date updateDate = calendar.getTime();
        return sdf.format(updateDate);
    }
 
    /**
     * 获取日期所在月第一天零点
     *
     * @return
     */
    public static Date getFirstDayZeroOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(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);
        return calendar.getTime();
    }
 
    /**
     * 获取日期零点
     *
     * @return
     */
    public static Date getDayZero(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }
 
    /**
     * 获取指定日期当周星期一的时间
     *
     * @param date
     * @return
     */
    public static Date getFirstDayZeroOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //获取本周第一天
        calendar.set(Calendar.WEEK_OF_YEAR, calendar.get(Calendar.WEEK_OF_YEAR));
        int dayOfWeek = 0;
        if (calendar.get(Calendar.DAY_OF_WEEK) == 1) {
            dayOfWeek = -6;
        } else {
            dayOfWeek = 2 - calendar.get(Calendar.DAY_OF_WEEK);
        }
        calendar.add(Calendar.DAY_OF_WEEK, dayOfWeek);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }
 
    /**
     * @author Ryan
     * @param: [date]
     * @return: java.time.LocalDate
     * @date: 2023/7/28
     * @description: 获取当前时间的前一年
     */
    public static Date getCurrDateOfLastYear(Date date, String perciod, boolean isMinus, Integer num) {
 
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime of = LocalDateTime.ofInstant(instant, zoneId);
        LocalDate localDate = of.toLocalDate();
        ZonedDateTime zonedDateTime = null;
        if (perciod.equals("year")) {
            if (isMinus) {
                zonedDateTime = localDate.minusYears(num).atStartOfDay(zoneId);
            } else {
                zonedDateTime = localDate.plusYears(num).atStartOfDay(zoneId);
            }
        } else if (perciod.equals("months")) {
            if (isMinus) {
                zonedDateTime = localDate.minusMonths(num).atStartOfDay(zoneId);
            } else {
                zonedDateTime = localDate.plusMonths(num).atStartOfDay(zoneId);
            }
        } else if (perciod.equals("week")) {
            if (isMinus) {
                zonedDateTime = localDate.minusWeeks(num).atStartOfDay(zoneId);
            } else {
                zonedDateTime = localDate.plusWeeks(num).atStartOfDay(zoneId);
            }
        } else {
            if (isMinus) {
                zonedDateTime = localDate.minusDays(num).atStartOfDay(zoneId);
            } else {
                zonedDateTime = localDate.plusDays(num).atStartOfDay(zoneId);
            }
        }
        return Date.from(zonedDateTime.toInstant());
    }
 
 
    public static Date getCurrDateOfLastYear(Date date, Integer num) {
        return getCurrDateOfLastYear(date, "year", true, num);
    }
 
    //给日期增加指定年
    public static Date addYears(Date date, Integer num) {
        // 创建Calendar对象
        Calendar calendar = Calendar.getInstance();
 
        // 设置待操作的日期
        calendar.setTime(date);
 
        // 增加三年
        calendar.add(Calendar.YEAR, num);
 
        // 获取增加后的日期
        Date newDate = calendar.getTime();
 
        return newDate;
    }
}