chenjiahe
2024-04-15 c2bb89da722d5b5db5f7151546369eb84b775a05
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
package com.hx.util;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
 
public class DateUtil {
 
    private static SimpleDateFormat Format_1 = new SimpleDateFormat("yyyyMMdd");
    private static SimpleDateFormat Format_2 = new SimpleDateFormat("yyyyMMddHHmmss");
    private static SimpleDateFormat Format_3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat Format_4 = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat Format_5 = new SimpleDateFormat("HH:mm:ss");
    private static SimpleDateFormat Format_6 = new SimpleDateFormat("yyyyMM");
    private static SimpleDateFormat Format_7 = new SimpleDateFormat("MM月dd日 HH:mm");
    private static SimpleDateFormat Format_8 = new SimpleDateFormat("yyyy年MM月");
    private static SimpleDateFormat Format_9 = new SimpleDateFormat("MM-dd");
    private static SimpleDateFormat Format_10 = new SimpleDateFormat("yyyy年MM月dd日");
    private static SimpleDateFormat Format_11 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    private static SimpleDateFormat Format_12 = new SimpleDateFormat("yyyyMMddHHmm");
    private static SimpleDateFormat Format_13 = new SimpleDateFormat("yyyy/MM/dd");
    private static SimpleDateFormat Format_14 = new SimpleDateFormat("yyyy-MM");
    private static SimpleDateFormat Format_15 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    private static SimpleDateFormat Format_16 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
    private static SimpleDateFormat Format_17 = new SimpleDateFormat("HH:mm");
 
    /**时间格式转化iso8601
     * @param date 时间
     * @return 返回的时间格式字符串
     */
    public static String dateFormatISO8601(Date date) {
        if(!SimpleTool.checkNotNull(date)){
            return "";
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");//设置日期格式
        return df.format(date);
    }
 
    /**时间格式转化
     * @param date 时间
     * @param format 时间格式
     * @return 返回的时间格式字符串
     */
    public static String dateFormat(Date date, String format) {
        if(!SimpleTool.checkNotNull(date)){
            return "";
        }
        SimpleDateFormat df = new SimpleDateFormat(format);//设置日期格式
        return df.format(date);
    }
 
    /**时间戳转时间
     * @param timestamp 时间戳
     * @param format 时间格式
     * @return 返回的时间格式字符串
     */
    public static Date timestampToDate(long timestamp, String format) {
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sd = sdf.format(new Date(timestamp));      // 时间戳转换成时间
        return DateUtil.parseString(sd,"yyyy-MM-dd HH:mm:ss");
    }
 
    /**
     * 转换成yyyyMMdd格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        return Format_1.format(date);
    }
 
    /**
     * 转换成yyyyMMddHHmmss格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String formatDate_1(Date date) {
        return Format_2.format(date);
    }
 
    /**
     * 转换成yyyy-MM-dd HH:mm:ss格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String formatDate_2(Date date) {
        return date == null ? null : Format_3.format(date);
    }
 
    /**
     * 转换成yyyy-MM-dd格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String formatDate_3(Date date) {
        return date != null ? Format_4.format(date) : null;
    }
 
    /**
     * 转换成HH:mm:ss格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String formatDate_4(Date date) {
        return Format_5.format(date);
    }
 
    /**
     * 转换成yyyyMM格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String formatDate_5(Date date) {
        return Format_6.format(date);
    }
 
    /**
     * 转换成mm月dd日 HH:mm格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String formatDate_6(Date date) {
        return Format_7.format(date);
    }
 
    /**
     * 转换成yyyy年mm月格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String formatDate_7(Date date) {
        return Format_8.format(date);
    }
 
    /**
     * 转换成mm-dd格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String formatDate_8(Date date) {
        return Format_9.format(date);
    }
 
    /**
     * 转换成yyyy年MM月dd日格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String formatDate_9(Date date) {
        return Format_10.format(date);
    }
 
    /**
     * 转换成yyyyMMddHHmm格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String formatDate_11(Date date) {
        return Format_12.format(date);
    }
 
    /**
     * 转换成yyyy年MM月dd日 HH:mm:ss格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String formatDate_10(Date date) {
        return Format_11.format(date);
    }
 
    /**
     * 转换成yyyy/MM/dd 格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String formatDate_13(Date date) {
        return Format_13.format(date);
    }
 
    /**
     * 转换成yyyy-MM 格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String formatDate_14(Date date) {
        return Format_14.format(date);
    }
 
 
    /**
     * 当前时间之前的时间与当前时间相差多少秒
     * @param startDate 当前时间之前的时间
     * @return
     */
    public static int calLastedTime(Date startDate) {
        long nowDate = new Date().getTime();
        long startDateTime = startDate.getTime();
        int diffSeconds = (int) ((nowDate - startDateTime) / 1000);
        return diffSeconds;
    }
 
 
    /**
     * 转换成yyyyMMddHHmmssSSS格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String formatDate_15(Date date) {
        return Format_15.format(date);
    }
 
    /**
     * 转换成yyyy/MM/dd HH:mm格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String formatDate_16(Date date) {
        return Format_16.format(date);
    }
 
    /**
     * 转换成HH:mm格式的日期字符串
     *
     * @param date
     * @return
     */
    public static String formatDate_17(Date date) {
        return Format_17.format(date);
    }
 
    /**
     * 转换成字符串yyyyMMddHHmmss成日�?
     *
     * @param str
     * @return
     * @throws Exception
     */
    public static Date parseString(String str) {
        try {
            return Format_2.parse(str);
        }catch (Exception e)
        {
            return null;
        }
    }
 
    /**
     * 转换成字符串到指定的日期
     *
     * @param str
     * @param format
     * @return
     * @throws Exception
     */
    public static Date parseString(String str, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try{
            return sdf.parse(str);
        }catch (Exception e)
        {
            return null;
        }
    }
 
    /**字符串转成时间yyyy-MM-dd HH:mm:ss*/
    public static Date parseString_1(String str)
    {
        try{
            return  Format_3.parse(str);
        }catch (Exception e)
        {
            return null;
        }
    }
 
    /**字符串转成时间yyyy-MM-dd*/
    public static Date parseString_2(String str)
    {
        try{
            return Format_4.parse(str);
        }catch (Exception e)
        {
            return null;
        }
    }
 
    /**时间上秒叠加
     * @Author: ChenJiaHe
     * @param dateTime 时间
     * @param second 秒
     * @return
     */
    public static Date addSecond(Date dateTime,int second){
        Calendar c = Calendar.getInstance();
        c.setTime(dateTime);
        c.add(Calendar.SECOND, second);
        return c.getTime();
    }
 
    /**时间上分钟叠加
     * @Author: ChenJiaHe
     * @param dateTime 时间
     * @param min 分钟
     * @return
     */
    public static Date addMin(Date dateTime,int min){
        Calendar c = Calendar.getInstance();
        c.setTime(dateTime);
        c.add(Calendar.MINUTE, min);
        return c.getTime();
    }
 
    /**时间上小时叠加
     * @Author: ChenJiaHe
     * @param dateTime 时间
     * @param hour 小时
     * @return
     */
    public static Date addhour(Date dateTime,int hour){
        Calendar c = Calendar.getInstance();
        c.setTime(dateTime);
        c.add(Calendar.HOUR, hour);
        return c.getTime();
    }
 
    /**时间上天数叠加
     * @Author: ChenJiaHe
     * @param dateTime 时间
     * @param dayNum 天数
     * @return
     */
    public static Date addDay(Date dateTime,int dayNum){
        Calendar c = Calendar.getInstance();
        c.setTime(dateTime);
        c.add(Calendar.DATE, dayNum);
        return c.getTime();
    }
 
    /**时间上月数叠加
     * @Author: ChenJiaHe
     * @param dateTime 时间
     * @param dayNum 天数
     * @return
     */
    public static Date addMonth(Date dateTime,int dayNum){
        Calendar c = Calendar.getInstance();
        c.setTime(dateTime);
        c.add(Calendar.MONTH, dayNum);
        return c.getTime();
    }
 
    /**时间上年数叠加
     * @Author: ChenJiaHe
     * @param dateTime 时间
     * @param dayNum 天数
     * @return
     */
    public static Date addYear(Date dateTime,int dayNum){
        Calendar c = Calendar.getInstance();
        c.setTime(dateTime);
        c.add(Calendar.YEAR, dayNum);
        return c.getTime();
    }
 
    /**
     * 转换成字符串到指定的日期
     *
     * @param date
     * @param format
     * @return
     * @throws Exception
     */
    public static String formatDate(Date date, String format) {
        if (date == null) {
            return null;
        }
 
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
        return sdf.format(date);
    }
 
    /**日期转星期
     * @param dateTime 字符串时间
     * @param format 字符串时间格式
     * @param returnType 返回类型 0星期几1周几
     * @return 数据
     */
    public static String dateToWeek(String dateTime,String format,Integer returnType) throws ParseException {
        SimpleDateFormat f = new SimpleDateFormat(format);
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        if(SimpleTool.checkNotNull(returnType)&&returnType==1){
            weekDays  = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        }
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        Date datet = null;
        datet = f.parse(dateTime);
        cal.setTime(datet);
 
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if (w < 0)
            w = 0;
        return weekDays[w];
    }
 
    /**日期转星期
     * @param dateTime 时间
     * @param returnType 返回类型 0星期几1周几
     * @return 数据
     */
    public static String dateToWeek(Date dateTime,Integer returnType) {
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        if(SimpleTool.checkNotNull(returnType)&&returnType==1){
            weekDays  = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        }
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        cal.setTime(dateTime);
 
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if (w < 0)
            w = 0;
        return weekDays[w];
    }
 
    /**
     * 获取两个时间相差分钟数
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 分钟
     */
    public static long differMinute(Date startTime, Date endTime) {
        long NTime = startTime.getTime();
        long OTime = endTime.getTime();
        return (NTime - OTime) / 1000 / 60;
    }
 
    /**
     * endTime比startTime多的天数
     * @param startTime 最小时间
     * @param endTime 最大时间
     * @return 返回
     */
    public static Integer differDay(Date startTime,Date endTime){
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(startTime);
 
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(endTime);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);
 
        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2) { //同一年
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++) {
                if(i%4==0 && i%100!=0 || i%400==0) {//闰年
                    timeDistance += 366;
                }
                else {//不是闰年
                    timeDistance += 365;
                }
            }
 
            return timeDistance + (day2-day1) ;
        }
        else { //不同年
            System.out.println("判断day2 - day1 : " + (day2-day1));
            return day2-day1;
        }
    }
 
    /**判断两个时间是不是同一天*/
    public static boolean timeEqual(Date startTime,Date endTime){
       if(startTime == null || endTime==null){
           return false;
       }
       boolean status = false;
       if(formatDate(startTime,"yyyyMMdd").equals(formatDate(endTime,"yyyyMMdd"))){
           status = true;
       }
       return status;
    }
 
    /**把秒转换成X天X时X分X秒*/
    public static String getChineseStr(Integer second) {
        int day = 24 * 60 * 60;
        int hour = 60 * 60;
        int min = 60;
 
        int dayNum = second / day;
        int hourNum = second % day / hour;
        int minNum = second % day % hour / min;
        second = second % day % hour % min;
 
        String str = dayNum > 0 ? dayNum + "天" : "";
        str += hourNum > 0 ? hourNum + "时" : "";
        str += minNum > 0 ? minNum + "分" : "";
        str += second + "秒";
 
        return str;
    }
 
    /**
     * 针对str格式的时间做转换 格式为"xx:xx"
     * @param time  传入的时间
     * @return  返回分钟如果10:25,则返回625
     */
    public static int getMinuteNum(String time){
 
        if(!StringUtils.isEmpty(time))
        {
            String[] arr = time.split(":");
            if(arr != null && arr.length == 2)
            {
                return Integer.parseInt(arr[0]) * 60 + Integer.parseInt(arr[1]);
            }
        }
 
        return 0;
    }
 
    /**
     * 获取当前月的开始时间
     * @param time 时间
     * @return 返回时间 格式yyyy-MM-dd 00:00:00
     */
    public static Date getMonthStart(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        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();
    }
 
    /**
     * 获取当前月的开始时间
     *
     * @param num 0拿取当月,正代表后,负代表前,值为几个(月)
     * @return 返回时间 格式yyyy-MM-dd 00:00:00
     */
    public static String getMonthStart(Integer num) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, num);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        return sdf.format(calendar.getTime());
    }
 
    /**
     * 获取当前月的结束时间
     * @param date 点前时间
     * @return 返回时间 格式yyyy-MM-dd 23:59:59999
     */
    public static Date getMonthEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        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();
    }
 
    /**
     * 获取当前月的结束时间,没有毫秒
     * @param date 点前时间
     * @return 返回时间 格式yyyy-MM-dd 23:59:59
     */
    public static Date getMonthEndNoMillisecond(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }
 
    /**
     * 获取当前月的结束时间
     * @param num 0拿取当月,正代表后,负代表前,值为几个(月)
     * @return 返回时间 格式yyyy-MM-dd 23:59:59
     */
    public static String getMonthEnd(Integer num) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, num);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        return sdf.format(calendar.getTime());
    }
 
    /**
     * 获取当前年的开始时间
     * @param time 时间
     * @return 返回时间 格式yyyy-MM-dd 00:00:00
     */
    public static Date getYearStart(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(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();
    }
 
    /**
     * 获取当前年的结束时间
     * @param time 时间
     * @return 返回时间 格式yyyy-MM-dd 23:59:59999
     */
    public static Date getYearEnd(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.MONTH, 11);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.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();
    }
 
    /**
     * 获取当前年的结束时间,没有毫秒
     * @param time 时间
     * @return 返回时间 格式yyyy-MM-dd 23:59:59
     */
    public static Date getYearEndNoMillisecond(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.MONTH, 11);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DATE));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }
 
 
    /**这天的开始时间
     * 日期2000-01-01变2000-01-01 00:00:00
     */
    public static String dayToStart(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);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        return sdf.format(calendar.getTime());
    }
 
    /**这天的最后时间
     * 日期2000-01-01变2000-01-01 23:59:59
     */
    public static String dayToEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        return sdf.format(calendar.getTime());
    }
 
    /**这天的开始时间
     * 日期2000-01-01变2000-01-01 00:00:00
     */
    public static Date dayToStartDate(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);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }
 
    /**这天的最后时间
     * 日期2000-01-01变2000-01-01 23:59:59999
     */
    public static Date dayToEndDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(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();
    }
 
    /**这天的最后时间,没有毫秒
     * 日期2000-01-01变2000-01-01 23:59:59
     */
    public static Date dayToEndDateNoMillisecond(Date date) {
        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 date 时间
     * @return 月份的天数
     */
    public static int getMonthDays(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, 1);
        cal.roll(Calendar.DATE, -1);
        return cal.getActualMaximum(Calendar.DATE);
    }
    
    /**获取月份的天数
     * @param year 年份
     * @param month 月份
     * @return 月份的天数
     */
    public static int getMonthDays(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, (month - 1));
        cal.set(Calendar.DATE, 1);
        cal.roll(Calendar.DATE, -1);
        return cal.getActualMaximum(Calendar.DATE);
    }
 
    /**获取月份的天数
     * @param yearMonth 年月
     * @param format 时间格式
     * @return 返回
     */
    public static int getMonthDays(String yearMonth,String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(sdf.parse(yearMonth));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
 
    /**当天多少点前
     * @param date 时间
     * @param num 多少点前,不含当点前,24小时制
     * @return 月份的天数
     */
    public static boolean getFrontMinute(Date date,int num) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, num-1);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
 
        return date.compareTo(cal.getTime()) < 1;
    }
 
    /**
     * 获得某个日期的当天某点
     * 例如:2022-12-26 11:20:00 -> 2022-12-26 13:00:00
     * @param num 24小时
     */
    public static String getHourDayTime(Date date,int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, num);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        return sdf.format(calendar.getTime());
    }
 
    /**
     * 获取时间当月剩余天数
     * */
    public static Integer getMonthSurplus(Date date) {
        Calendar month = Calendar.getInstance();
        month.setTime(new Date());
        month.set(Calendar.DATE, 1);
        month.roll(Calendar.DATE, -1);
        return month.getActualMaximum(Calendar.DATE) - Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    }
 
    /**获取当前时间所在周的周一00:00:00*/
    public static Date getMonday(Date date) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTime(date);
        //以周一为首日
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        //周一
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return calendar.getTime();
    }
 
    /**
     * 根据出生年月日计算年龄
     * @param birth
     * @return
     */
    public static int getAge(Date birth) {
        Calendar cal = Calendar.getInstance();
        int thisYear = cal.get(Calendar.YEAR);
        int thisMonth = cal.get(Calendar.MONTH);
        int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
 
        cal.setTime(birth);
        int birthYear = cal.get(Calendar.YEAR);
        int birthMonth = cal.get(Calendar.MONTH);
        int birthdayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
 
        int age = thisYear - birthYear;
 
        // 未足月
        if (thisMonth <= birthMonth) {
            // 当月
            if (thisMonth == birthMonth) {
                // 未足日
                if (dayOfMonth < birthdayOfMonth) {
                    age--;
                }
            } else {
                age--;
            }
        }
        return age;
    }
 
    /**
     * 获取某天结束秒数
     * @param dateTime      日期
     * @param lateSecond    延迟秒数
     * @return
     */
    public static long todayEndSecond(Date dateTime, Long lateSecond) {
        if(dateTime == null){
            dateTime = new Date();
        }
        if(lateSecond == null){
            lateSecond = 0L;
        }
        Date endTime = DateUtil.dayToEndDate(dateTime);
        return differSecond(dateTime, endTime) + lateSecond;
    }
 
    /**
     * 计算2个实际相差秒数
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    public static long differSecond(Date startTime, Date endTime) {
        if(startTime == null || endTime == null){
            return 0L;
        }
        long sTime = startTime.getTime();
        long eTime = endTime.getTime();
        return (eTime - sTime) / 1000L;
    }
}