Lius
2025-03-27 b0f56573ea27a8798764314c72cf39e9eb0651dc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
package org.jeecg.modules.mdc.util;
 
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
public class DateUtils {
 
    private final static long DAYTIMESUNSET = 86400;
 
    /**
     * @return 得到明天
     */
    public static Date getNextDay(Date d1) {
        long d2 = d1.getTime() + 86400 * 1000;
        return new Date(d2);
    }
 
    /**
     * @return 得到昨天
     */
    public static Date getPreviousDay(Date d1) {
        long d2 = d1.getTime() - 86400 * 1000;
        return new Date(d2);
    }
 
    /**
     * @return 返回时间差的语言描述  如1天2小时5分6秒
     */
    public static String different(Date d1, Date d2) {
        if (d1 == null || d2 == null) {
            return "";
        }
        StringBuilder strB = new StringBuilder();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            //毫秒ms
            long diff = d2.getTime() - d1.getTime();
            long diffSeconds = diff / 1000 % 60;
            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000) % 24;
            long diffDays = diff / (24 * 60 * 60 * 1000);
            if (diffDays > 0) {
                strB.append(diffDays + " 天 ");
                strB.append(diffHours + " 小时 ");
                strB.append(diffMinutes + " 分钟 ");
                strB.append(diffSeconds + " 秒 ");
            } else if (diffHours > 0) {
                strB.append(diffHours + " 小时 ");
                strB.append(diffMinutes + " 分钟 ");
                strB.append(diffSeconds + " 秒 ");
            } else if (diffMinutes > 0) {
                strB.append(diffMinutes + " 分钟 ");
                strB.append(diffSeconds + " 秒 ");
            } else {
                strB.append(diffSeconds + " 秒 ");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strB.toString();
    }
 
    /**
     * @return 获取两个date的时间差,结果为秒 除
     */
    public static long differentSecond(Date startDate, Date endDate) {
        return new BigDecimal(endDate.getTime() - startDate.getTime()).divide(new BigDecimal("1000"), 0, BigDecimal.ROUND_HALF_UP).longValue();
//        return (endDate.getTime() - startDate.getTime()) / 1000;
    }
 
    /**
     * @return 获取两个date的时间差,结果为分钟
     */
    public static Integer differentMinutes(Date startDate, Date endDate) {
        return new BigDecimal(endDate.getTime() - startDate.getTime()).divide(new BigDecimal("60000"), 0, RoundingMode.HALF_UP).intValue();
    }
 
    /**
     * @return 返回传入时间的0点0分0秒
     */
    public static Date getTodayZero(Date d) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }
 
    /**
     * @return 返回传入时间的0点0分0秒
     */
    public static Date getTomorrowZero(Date d) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.add(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }
 
    /**
     * @return 判断时间是否是当天
     */
    public static boolean isTaday(Date date) {
        Date now = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        String nowDay = sf.format(now);
        String day = sf.format(date);
        return day.equals(nowDay);
    }
 
    /**
     * @return 判断两个时间是否为同一天
     */
    public static boolean isOneDay(Date d1, Date d2) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        String d1Str = sf.format(d1);
        String d2Str = sf.format(d2);
        return d1Str.equals(d2Str);
    }
 
    /**
     * @return 判断两个时间是否为同一年
     */
    public static boolean isOneYear(Date d1, Date d2) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy");
        String d1Str = sf.format(d1);
        String d2Str = sf.format(d2);
        return d1Str.equals(d2Str);
    }
 
    public static String dateProportion(Date start, Date end) {
        float differentSecond = DateUtils.differentSecond(start, end);
        float f = differentSecond / DAYTIMESUNSET * 100;
        return String.format("%.2f", f) + "%";
    }
 
    public static Date strToDate(String dateStr, String format) {
        SimpleDateFormat sf = new SimpleDateFormat(format);
        Date result = null;
        try {
            result = sf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }
 
    public static final String STR_DATE = "yyyy-MM-dd";
    public static final String STRDATE = "yyyyMMdd";
    public static final String STR_YEAR_MONTH = "yyyy-MM";
    public static final String STRYEARMONTH = "yyyyMM";
    public static final String STR_DATE_TIME = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String STR_DATE_TIME_SMALL = "yyyy-MM-dd HH:mm:ss";
    public static final String STR_DATE_TIME_MIN = "yyyy-MM-dd HH:mm";
    public static final String STR_DATE_TIME_HOUR = "yyyy-MM-dd HH";
    public static final String STR_DATE_TIME_FULL = "yyyyMMddHHmmssSSS";
    public static final String STR_HHMMSS = "HH:mm:ss";
    public static final String STR_HHMM = "HH:mm";
    public static final String STR_MMDD = "MM-dd";
 
    /**
     * <p>
     * Description: 实际投标月份
     * </p>
     *
     * @param startDate
     * @param endDate
     * @return obj [0] 实际投标月份 例如实际投标月份(3.86)意义是3个月又26天 obj[1] 实际取整月份 (3)
     */
    public static Integer getRealMonth(Date startDate, Date endDate) {
        Integer month = 0;
        Calendar start = Calendar.getInstance();
        start.setTime(startDate);
 
        Calendar end = Calendar.getInstance();
        end.setTime(endDate);
        int year = start.get(Calendar.YEAR);
        int year2 = end.get(Calendar.YEAR);
        int month2 = start.get(Calendar.MONTH);
        int month3 = end.get(Calendar.MONTH);
        try {
            while (start.before(end)) {
                start.add(Calendar.MONTH, 1);
                month++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        int day2 = start.get(Calendar.DAY_OF_MONTH);
        int day3 = end.get(Calendar.DAY_OF_MONTH);
        int tmpYear = year2 - year;
        if (day2 == day3) {
            return (tmpYear * 12) + (month3 - month2);
        }
        return month == 0 ? month : (month - 1);
    }
 
    public static Date getNow() {
        return new Date(System.currentTimeMillis());
    }
 
    public static int getDayOfMonth() {
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        return day;
    }
 
    /**
     * <p>
     * Description: 修改时间为指定时间当天的23:59:59.000
     * </p>
     *
     * @param date 需要修改的时间
     * @return 修改后的时间
     */
    public static Date fillTime(Date date) {
        Date result = removeTime(date);
        result.setTime(result.getTime() + 24 * 60 * 60 * 1000 - 1000); // 加一天
        return result;
    }
 
    /**
     * @param date 当前时间
     * @param i    往前几天
     * @return
     */
    public static Date fillBeforeTime(Date date, Integer i) {
        Date result = removeTime(date);
        Calendar calendar = Calendar.getInstance(); // 得到日历
        calendar.setTime(result);// 把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, i); // 设置为前一天
        result.setTime(calendar.getTime().getTime() + 24 * 60 * 60 * 1000
                - 1000); // 加一天
        return result;
    }
 
    /**
     * <p>
     * Description: 修改时间为指定时间前一天的00:00:00
     * </p>
     *
     * @param date 需要修改的时间
     * @return 修改后的时间
     */
    public static Date plusTime(Date date, Integer i) {
        Date result = removeTime(date);
        Calendar calendar = Calendar.getInstance(); // 得到日历
        calendar.setTime(result);// 把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, i); // 设置为前一天
        return calendar.getTime();
    }
 
    /**
     * <p>
     * Description: 去掉日期时间中的时间部分
     * </p>
     * 如: 2013-11-11 18:56:33 ---> 2013-11-11 00:00:00
     *
     * @param date 需要修改的时间
     * @return 修改后的时间
     */
    public static Date removeTime(Date date) {
        Date result = null;
        try {
            SimpleDateFormat df = new SimpleDateFormat(STR_DATE);
            String dateStr = df.format(date);
            result = df.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }
 
    /**
     * <p>
     * Description: 按默认格式(yyyy-MM-dd HH:mm:ss.SSS)获取时间字符串
     * </p>
     *
     * @param date 要转换的日期
     * @return 转换后的时间字符串
     */
    public static String format(Date date) {
        SimpleDateFormat df = new SimpleDateFormat(STR_DATE_TIME);
        return df.format(date);
    }
 
    public static Date parseDate(Date date, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        try {
            date = df.parse(df.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
 
    public static Date getDelayedYear() {
        Calendar curr = Calendar.getInstance();
        curr.set(Calendar.YEAR, curr.get(Calendar.YEAR) + 1);
        Date date = curr.getTime();
        return date;
    }
 
    /**
     * <p>
     * Description: 按指定格式获取时间字符串
     * </p>
     *
     * @param date   要转换的日期
     * @param format 格式,例如:yyyy-MM-dd HH:mm:ss.SSS
     * @return 转换后的时间字符串
     */
    public static String format(Date date, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(date);
    }
 
    /**
     * <p>
     * Description: 加月函数
     * </p>
     *
     * @param month 月份数
     * @return
     */
    public static Date addMonth(Integer month, Date time) {
        Calendar c = Calendar.getInstance();
        c.setTime(time);
        c.add(Calendar.MONTH, month);
        return c.getTime();
    }
 
    public static Boolean greater(Date startTime, Date endTime) {
        return startTime.getTime() > endTime.getTime();
    }
 
    public static Boolean less(Date startTime, Date endTime) {
        return startTime.getTime() < endTime.getTime();
    }
 
    public static Boolean equals(Date startTime, Date endTime) {
        return startTime.getTime() == endTime.getTime();
    }
 
    public static Integer getDiffMonth(Calendar c, Calendar c1) {
        return (c.get(Calendar.YEAR) - c1.get(Calendar.YEAR)) * 12
                + (c.get(Calendar.MONTH) - c1.get(Calendar.MONTH));
    }
 
    /**
     * 是否为同一天
     */
    public static boolean equalsDay(Date a, Date b) {
        return removeTime(a).getTime() == removeTime(b).getTime();
    }
 
    public static Integer getDays(Date startTime, Date endTime) {
        Calendar c = Calendar.getInstance();
        c.setTime(startTime);
        Calendar c1 = Calendar.getInstance();
        c1.setTime(endTime);
        long t1 = c.getTimeInMillis();
        long t2 = c1.getTimeInMillis();
        // 计算天数
        Long days = (t2 - t1) / (24 * 60 * 60 * 1000);
        return days.intValue();
    }
 
    public static Integer getSeconds(Date startTime, Date endTime) {
        try {
            Calendar c = Calendar.getInstance();
            c.setTime(startTime);
            Calendar c1 = Calendar.getInstance();
            c1.setTime(endTime);
            long t1 = c.getTimeInMillis();
            long t2 = c1.getTimeInMillis();
            // 计算秒数
            Long days = (t2 - t1) / (1000);
            return days.intValue();
        } catch (Exception e) {
            return 0;
        }
 
    }
 
    //获取小时差
    public static double subHours(Date startTime, Date endTime) {
        Calendar c = Calendar.getInstance();
        c.setTime(startTime);
        Calendar c1 = Calendar.getInstance();
        c1.setTime(endTime);
        double t1 = c.getTimeInMillis();
        double t2 = c1.getTimeInMillis();
        // 计算天数
        double hours = (t2 - t1) / (1000 * 60 * 60);
        return Double.valueOf(new DecimalFormat("#.00").format(hours));
    }
 
    //根据当前时间和出生日期获取年龄
    public static int getAge(Date birthDay) {
        Calendar cal = Calendar.getInstance();
        // 取出系统当前时间的年、月、日部分
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);
        // 取出出生日期的年、月、日部分
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
        // 当前年份与出生年份相减,初步计算年龄
        int age = yearNow - yearBirth;
        // 当前月份与出生日期的月份相比,如果月份小于出生月份,则年龄上减1,表示不满多少周岁
        if (monthNow <= monthBirth) {
            // 如果月份相等,在比较日期,如果当前日,小于出生日,也减1,表示不满多少周岁
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) {
                    age--;
                }
            } else {
                age--;
            }
 
        }
        return age;
    }
 
    public static Date subDays(Date startTime, Integer day) {
        Calendar c = Calendar.getInstance();
        c.setTime(startTime);
        c.add(Calendar.DATE, day * -1);
        return c.getTime();
    }
 
    public static Date addDays(Date startTime, Integer day) {
        Calendar c = Calendar.getInstance();
        c.setTime(startTime);
        c.add(Calendar.DATE, day);
        return c.getTime();
    }
    /*public static Date addHours(Date startTime, Integer hours) {
        Calendar c = Calendar.getInstance();
        c.setTime(startTime);
        c.add(Calendar.HOUR, hours );
        return c.getTime();
    }*/
 
    public static Date toDate(String date, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        try {
            return df.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public static Date toDateFull(String date) {
        SimpleDateFormat df = new SimpleDateFormat(STR_DATE_TIME);
        try {
            return df.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public static Date toDateMedium(String date) {
        SimpleDateFormat df = new SimpleDateFormat(STR_DATE_TIME_SMALL);
        try {
            Date dd = df.parse(date);
            return dd;
        } catch (ParseException e) {
            return null;
        }
    }
 
    public static Integer getDate(Date date) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int day = calendar.get(Calendar.DATE);
            return day;
        }
        return null;
    }
 
    public static Integer getYear() {
        return getYear(new Date());
    }
 
    public static Integer getYear(Date date) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int year = calendar.get(Calendar.YEAR);
            return year;
        }
        return null;
    }
 
    public static Integer getMonth() {
        return getMonth(new Date());
    }
 
    public static Integer getMonth(Date date) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int month = calendar.get(Calendar.MONTH);
            return month + 1;
        }
        return null;
    }
 
    public static Integer getDay() {
        return getDay(new Date());
    }
 
    public static Integer getDay(Date date) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            return day;
        }
        return null;
    }
 
 
    public static Date[] getMonthStartTimeAndEndTime(Integer month) {
        Calendar calendar = Calendar.getInstance();
        Date[] dates = new Date[2];
        Date startTime = null;
        Date endsTime = null;
        if (month != null) {
            calendar.set(Calendar.MONTH, month);
            //获得到本月的第一天
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            startTime = calendar.getTime();
            startTime = DateUtils.removeTime(startTime);
            //获得到本月的最后一天
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            endsTime = calendar.getTime();
            endsTime = DateUtils.fillTime(endsTime);
        }
        dates[0] = startTime;
        dates[1] = endsTime;
        return dates;
    }
 
    public static Date[] getMonthStartTimeAndEndTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        Integer month = calendar.get(Calendar.MONTH);
        Date[] dates = new Date[2];
        Date startTime = null;
        Date endsTime = null;
        if (month != null) {
            calendar.set(Calendar.MONTH, month);
            //获得到本月的第一天
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            startTime = calendar.getTime();
            startTime = DateUtils.removeTime(startTime);
            //获得到本月的最后一天
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            endsTime = calendar.getTime();
            endsTime = DateUtils.fillTime(endsTime);
        }
        dates[0] = startTime;
        dates[1] = endsTime;
        return dates;
    }
 
    /**
     * 获取days天的时间区间,endTime 为当天的后一天0点,
     * startTime 为endTime前days天
     *
     * @param days
     * @return
     */
    public static Date[] getDaysStartTimeAndEndTime(Integer days) {
        Date[] dates = new Date[2];
        Date endDate = plusTime(removeTime(getNow()), 1);
        Date startDate = subDays(endDate, days);
        dates[0] = startDate;
        dates[1] = endDate;
        return dates;
    }
 
    /**
     * 根据某一年获取最后一天的时间
     * 2013 ---> 2013-12-31 23:59:59.000
     *
     * @param year
     * @return
     */
    public static Date getYearEndDay(Integer year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date yearLast = calendar.getTime();
        Date lastYear = DateUtils.fillTime(yearLast);
        return lastYear;
    }
 
    /**
     * 获取start/end的所有日期字符串 格式yyyy-MM-dd
     *
     * @param start
     * @param end
     * @return
     */
    public static List<String> getDatesStringList(Date start, Date end, String strDate) {
        List<String> list = new ArrayList<>();
        int i = getDays(start, end);
        for (int j = 0; j <= i; j++) {
            if (j == 0) {
                list.add(format(start, strDate));
            } else {
                list.add(format(plusTime(start, j), strDate));
            }
        }
        return list;
    }
 
    /**
     * 获取start/end的所有日期字符串 格式yyyy-MM-dd
     *
     * @param start
     * @param end
     * @return
     */
    public static List<String> getDatesStringList(Date start, Date end) {
        List<String> list = new ArrayList<>();
        int i = getDays(start, end);
        for (int j = 0; j <= i; j++) {
            if (j == 0) {
                list.add(format(start, STR_DATE));
            } else {
                list.add(format(plusTime(start, j), STR_DATE));
            }
        }
        return list;
    }
 
    /**
     * 获取start/end的所有日期字符串 格式yyyyMMdd
     *
     * @param start
     * @param end
     * @return
     */
    public static List<String> getDatesStringList2(Date start, Date end) {
        List<String> list = new ArrayList<>();
        int i = getDays(start, end);
        for (int j = 0; j <= i; j++) {
            if (j == 0) {
                list.add(format(start, STRDATE));
            } else {
                list.add(format(plusTime(start, j), STRDATE));
            }
        }
        return list;
    }
 
    /**
     * 获取start/end的所有日期字符串 格式MM-dd
     *
     * @param start
     * @param end
     * @return
     */
    public static List<String> getDatesStringLists(Date start, Date end) {
        List<String> list = new ArrayList<>();
        int i = getDays(start, end);
        for (int j = 0; j <= i; j++) {
            if (j == 0) {
                list.add(format(start, STR_MMDD));
            } else {
                list.add(format(plusTime(start, j), STR_MMDD));
            }
        }
        return list;
    }
 
    public static List<String> getMonthBetween(Date start, Date end) {
        List<String> list = new ArrayList<>();
        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();
        min.setTime(start);
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
        max.setTime(end);
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
        Calendar curr = min;
        while (curr.before(max)) {
            list.add(format(curr.getTime(), STR_YEAR_MONTH));
            curr.add(Calendar.MONTH, 1);
        }
        return list;
    }
 
    /**
     * 获取dateStr的日期格式yyyy-MM-dd
     *
     * @param dateStr
     * @return
     */
    public static Date getShortDate(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat(STR_DATE);
        Date startTime = null;
        try {
            startTime = sdf.parse(dateStr);
        } catch (ParseException e) {
        }
        return startTime == null ? removeTime(new Date()) : startTime;
    }
 
    /**
     * 获取dateStr的日期格式yyyyMMdd
     *
     * @param dateStr
     * @return
     */
    public static Date getShortDate2(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat(STRDATE);
        Date startTime = null;
        try {
            startTime = sdf.parse(dateStr);
        } catch (ParseException e) {
        }
        return startTime == null ? removeTime(new Date()) : startTime;
    }
 
    public static Date getFormatDate(String dateStr, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date startTime = null;
        try {
            startTime = sdf.parse(dateStr);
        } catch (ParseException e) {
        }
        return startTime == null ? removeTime(new Date()) : startTime;
    }
 
    /**
     * @param time
     * @param n
     * @return 在一个时间上加秒
     */
    public static Date addSecond(Date time, Integer n) {
        Calendar c = Calendar.getInstance();
        c.setTime(time);
        c.add(Calendar.SECOND, n);
        return c.getTime();
    }
 
    /**
     * @param time
     * @param n
     * @return 在一个时间上加分钟
     */
    public static Date addMinute(Date time, Integer n) {
        Calendar c = Calendar.getInstance();
        c.setTime(time);
        c.add(Calendar.MINUTE, n);
        return c.getTime();
    }
 
    /**
     * @param time
     * @param n
     * @return 在一个时间上加小时
     */
    public static Date addHour(Date time, Integer n) {
        Calendar c = Calendar.getInstance();
        c.setTime(time);
        c.add(Calendar.HOUR_OF_DAY, n);
        return c.getTime();
    }
 
    /**
     * 获取start/end的所有日期字符串 格式yyyy-MM-dd
     *
     * @param start
     * @param end
     * @return
     */
    public static List<String> getDateMonth(String start, String end) {
        List<String> list = new ArrayList<>();
        SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyy-MM");
        Date startyear = null;
        try {
            startyear = yyyyMM.parse(start);
            Date endyear = yyyyMM.parse(end);
            Calendar dd = Calendar.getInstance();//定义日期实例
            dd.setTime(startyear);//设置日期起始时间
            while (dd.getTime().before(endyear)) {//判断是否到结束日期
                String str = yyyyMM.format(dd.getTime());
                dd.add(Calendar.MONTH, 1);//进行当前日期月份加1
                list.add(str);
            }
            list.add(yyyyMM.format(endyear));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return list;
    }
 
    /**
     * 获取start/end的所有日期字符串 格式yyyy
     *
     * @param start
     * @param end
     * @return
     */
    public static List<String> getDateYear(String start, String end) {
        List<String> list = new ArrayList<>();
        SimpleDateFormat yyyy = new SimpleDateFormat("yyyy");
        Date startyear = null;
        try {
            startyear = yyyy.parse(start);
            Date endyear = yyyy.parse(end);
            Calendar dd = Calendar.getInstance();//定义日期实例
            dd.setTime(startyear);//设置日期起始时间
            while (dd.getTime().before(endyear)) {//判断是否到结束日期
                String str = yyyy.format(dd.getTime());
                dd.add(Calendar.YEAR, 1);//进行当前日期月份加1
                list.add(str);
            }
            list.add(yyyy.format(endyear));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return list;
    }
 
    /**
     * 获取当前时间 定位到小时
     * 格式为 yyyy-MM-dd hh:mm
     * 例:2018-02-27 11:00
     *
     * @return
     */
    public static Date getNowHourDate() {
        Date now = getNow();
        Date result;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        String dateStr = format(now, STR_DATE);
        dateStr = dateStr + " " + (hour < 10 ? "0" + hour : hour) + ":00";
        result = toDate(dateStr, STR_DATE_TIME_MIN);
        return result;
    }
 
    /**
     * 获取每日8:11或11:11的时间点
     *
     * @return
     */
    public static Date getNowHourDateTo() {
        Date now = getNow();
        Date result;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        if (hour != 8 && hour != 11) {
            return null;
        }
        int minute = calendar.get(Calendar.MINUTE);
        if (minute < 11) {
            return null;
        }
        String dateStr = format(now, STR_DATE);
        dateStr = dateStr + " " + (hour < 10 ? "0" + hour : hour) + ":11";
        result = toDate(dateStr, STR_DATE_TIME_MIN);
        return result;
    }
 
    /**
     * 获取每日8:00、13:00、20:00的时间点
     *
     * @return
     */
    public static List<Date> getHourDateList(Date time) {
        List<Date> result = new ArrayList<>();
        result.add(addHour(time, 8));
        result.add(addHour(time, 13));
        result.add(addHour(time, 20));
        return result;
    }
 
    /**
     * 获取上月的最后一天
     *
     * @return
     */
    public static Date getPreviousMonthLastDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        return DateUtils.removeTime(calendar.getTime());
    }
 
    /**
     * 获取end时间月的第一天
     *
     * @param end 月份最后一天
     * @return
     */
    public static Date getTheMonthFirstDay(Date end) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(end);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }
 
    public static String secToTimeHour(int time) {
        String timeStr = null;
        int hour = 0;
        if (time <= 0) {
            return "0小时";
        } else {
            BigDecimal bigDecimal = new BigDecimal(time);
            BigDecimal bigDecimal1 = new BigDecimal(3600);
            BigDecimal hourBigDecimal = new BigDecimal(0);
            hourBigDecimal = new BigDecimal(String.valueOf(bigDecimal)).
                    divide(new BigDecimal(String.valueOf(bigDecimal1)), 2, BigDecimal.ROUND_HALF_UP);
            timeStr = hourBigDecimal + "小时";
        }
        return timeStr;
    }
 
    public static String secToTime(int time) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (time <= 0)
            return " ";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = unitFormat(minute) + "分" + unitFormat(second) + " 秒";
            } else {
                hour = minute / 60;
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + "小时" + unitFormat(minute) + "分" + unitFormat(second) + "秒";
            }
        }
        return timeStr;
    }
 
    public static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }
 
    /**
     * 验证时间
     *
     * @param time 时间字符串 HH:mm
     * @return 验证成功返回true,验证失败返回false
     */
    public static boolean checkTime(String time) {
        String regex = "([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
        return Pattern.matches(regex, time);
    }
 
    /**
     * 获取每日07:30:00的时间点
     * 格式为 yyyy-MM-dd HH:mm:ss
     * 例:2018-05-28 07:30:00
     *
     * @return
     */
    public static Date getHourDateTo(Date now, String time) {
        String dateStr = format(now, STR_DATE);
        dateStr = dateStr + " " + time;
        Date result = toDate(dateStr, STR_DATE_TIME_SMALL);
        return result;
    }
 
    public static Date setTimeForDay(Date theDate, String planTime) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(theDate);
        String[] times = planTime.split(":");
        cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(times[0]));
        cal.set(Calendar.MINUTE, Integer.parseInt(times[1]));
        cal.set(Calendar.SECOND, Integer.parseInt(times[2]));
        return cal.getTime();
    }
 
    public static long getTimeWithOutDay(Date d) {
        return d.getTime() / 1000 % 86400;
    }
 
    /**
     * @param lo 毫秒数
     * @return String yyyy-MM-dd HH:mm:ss
     * @Description: long类型转换成日期
     */
    public static String longToDate(long lo) {
        Date date = new Date(lo);
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sd.format(date);
    }
 
    /**
     * @param startDate
     * @param endDate
     * @return
     */
    public static List<Date> getWeekDays(LocalDate startDate, LocalDate endDate) {
        List<Date> result = new ArrayList<>();
        List<LocalDate> dateList = Stream.iterate(startDate, localDate -> localDate.plusDays(1))
                .limit(ChronoUnit.DAYS.between(startDate, endDate) + 1)
                .filter(localDate -> DayOfWeek.SATURDAY.equals(DayOfWeek.of(localDate.get(ChronoField.DAY_OF_WEEK))) || DayOfWeek.SUNDAY.equals(DayOfWeek.of(localDate.get(ChronoField.DAY_OF_WEEK))))
                .collect(Collectors.toList());
        dateList.forEach(localDate -> result.add(Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant())));
        return result;
    }
 
    /**
     * @param smallDate
     * @param bigDate
     * @desc 获取两个日期之间的天数
     */
    public static Integer getDaysBetween(String smallDate, String bigDate) throws ParseException {
        // 日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        // 获取两个日期的时间戳
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smallDate));
        long smallTime = cal.getTimeInMillis();
        cal.setTime(sdf.parse(bigDate));
        long bigTime = cal.getTimeInMillis();
        // 相差的日期
        long days = (bigTime - smallTime) / (1000 * 3600 * 24);
        // long转int 存在溢出情况  根据业务情况编辑catch中返回值
        try {
            return Integer.parseInt(String.valueOf(days));
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return 0;
        }
    }
 
    /**
     * 日期转换,将接口返回的20180524转为2018-05-24
     *
     * @param str
     * @return
     */
    public static String dateConvertion(String str) {
        Date parse = null;
        String dateString = "";
        try {
            parse = new SimpleDateFormat("yyyyMMdd").parse(str);
            dateString = new SimpleDateFormat("yyyy-MM-dd").format(parse);
        } catch (ParseException e) {
            dateString = null;
        }
 
        return dateString;
    }
}