测试用户
2023-04-13 43393f2bb11cbf9e6af40077bbc5284660e8a754
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
package com.common.core.utils;
 
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
 
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class DateUtils {
    /**
     * 年-月-日 时:分:秒 时间格式
     */
    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    /**
     * 年-月-日 时:分 时间格式
     */
    public static final String DATE_TIME_MINUTE_FORMAT = "yyyy-MM-dd HH:mm";
    /**
     * 年/月/日 时:分 时间格式
     */
    public static final String DATE_TIME_MINUTE_FORMAT1 = "yyyy/MM/dd HH:mm";
    /**
     * 日/月/年 时:分 时间格式
     */
    public static final String DATE_TIME_MINUTE_FORMAT2 = "dd/MM/yyyy HH:mm";
    /**
     * 2020-08-09T02:35:20.000+0000 格林威治时间
     */
    public static final String DATE_TIME_GL_ST_FORMAT = "yyyy-MM-dd'T'hh:mm:ss.SSSZ";
 
    public static final String DATE_TIME_GL_ST_FORMAT_ZT = "yyyy-MM-dd'T'HH:mm:ssXXX";
    public static final String DATE_TIME_GL_ST_FORMAT_ZT_1 = "yyyy-MM-dd'T'HH:mm:ss'Z'";
    /**
     * 年-月-日 时间格式
     */
    public static final String DATE_FORMAT = "yyyy-MM-dd";
 
    /**
     * 年-月-日 时间格式
     */
    public static final String YEAR_MONTH_FORMAT = "yyyy-MM-";
 
    /**
     * 小时格式
     */
    public static final String HOUR_FORMAT = "HH";
    /**
     * 年月日 时间格式
     */
    public static final String DATE_FORMAT_NO_SPACER = "yyyyMMdd";
    /**
     * 月日 时间格式
     */
    public static final String MONTH_DAY_FORMAT_NO_SPACER = "MMdd";
    /**
     * 年月 时间格式
     */
    public static final String YEAR_MONTH_FORMAT_NO_SPACER = "yyyyMM";
 
    public static final String YEAR_FORMAT = "yyyy";
 
    public static final String MONTH_FORMAT = "MM";
 
    public static final String DAY_OF_MONTH_FORMAT = "dd";
 
    public static final String BJ_TIME_ZONE = "+8";
 
    /**
     * 默认格式化类型集合
     */
    public static final Map<Pattern, String> SUPPORT_FORMAT_MAP;
    /**
     * 年-月-日T时:分:秒+时:分 时间格式正则匹配表达式
     */
    private static final Pattern DATE_TIME_PATTERN_ZT = Pattern.compile("^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\\b\\+[0-9]{2}:[0-9]{2}$");
    /**
     * 年-月-日 时:分:秒 时间格式正则匹配表达式
     */
    private static final Pattern DATE_TIME_PATTERN = Pattern.compile("^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$");
    /**
     * 年-月-日 时:分 时间格式正则匹配表达式
     */
    private static final Pattern DATE_TIME_MINUTE_PATTERN = Pattern.compile("^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$");
    /**
     * 年/月/日 时:分 时间格式正则匹配表达式
     */
    private static final Pattern DATE_TIME_MINUTE_PATTERN1 = Pattern.compile("^[0-9]{4}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2}$");
    /**
     * 日/月年 时:分 时间格式正则匹配表达式
     */
    private static final Pattern DATE_TIME_MINUTE_PATTERN2 = Pattern.compile("[0-9]{2}/[0-9]{2}/^[0-9]{4} [0-9]{2}:[0-9]{2}$");
 
    /**
     * 年-月-日 时间格式正则匹配表达式
     */
    private static final Pattern DATE_PATTERN = Pattern.compile("^[0-9]{4}-[0-9]{2}-[0-9]{2}$");
    /**
     * 年月日 时间格式正则匹配表达式
     */
    private static final Pattern DATE_PATTERN_NO_SPACER = Pattern.compile("^[0-9]{4}[0-9]{2}[0-9]{2}$");
    /**
     * 月日 时间格式正则匹配表达式
     */
    private static final Pattern MONTH_DAY_PATTERN_NO_SPACER = Pattern.compile("^[0-9]{2}[0-9]{2}$");
    /**
     * 年日 时间格式正则匹配表达式
     */
    private static final Pattern YEAR_MONTH_PATTERN_NO_SPACER = Pattern.compile("^[0-9]{4}[0-9]{2}$");
 
    /**
     * 3
     */
    private static final Integer NUM_3 = 3;
 
    /**
     * 5
     */
    private static final Integer NUM_5 = 5;
 
    static {
        SUPPORT_FORMAT_MAP = new ConcurrentHashMap<Pattern, String>();
        SUPPORT_FORMAT_MAP.put(DATE_TIME_PATTERN, DATE_TIME_FORMAT);
        SUPPORT_FORMAT_MAP.put(DATE_TIME_MINUTE_PATTERN, DATE_TIME_MINUTE_FORMAT);
        SUPPORT_FORMAT_MAP.put(DATE_TIME_MINUTE_PATTERN1, DATE_TIME_MINUTE_FORMAT1);
        SUPPORT_FORMAT_MAP.put(DATE_TIME_MINUTE_PATTERN2, DATE_TIME_MINUTE_FORMAT2);
        SUPPORT_FORMAT_MAP.put(DATE_PATTERN, DATE_FORMAT);
        SUPPORT_FORMAT_MAP.put(DATE_PATTERN_NO_SPACER, DATE_FORMAT_NO_SPACER);
        SUPPORT_FORMAT_MAP.put(YEAR_MONTH_PATTERN_NO_SPACER, YEAR_MONTH_FORMAT_NO_SPACER);
        SUPPORT_FORMAT_MAP.put(MONTH_DAY_PATTERN_NO_SPACER, MONTH_DAY_FORMAT_NO_SPACER);
        SUPPORT_FORMAT_MAP.put(DATE_TIME_PATTERN_ZT, DATE_TIME_GL_ST_FORMAT_ZT);
    }
 
    /**
     * 字符串转日期类型
     *
     * @param dateString
     * @return
     */
    public static Date convert(String dateString) {
        Date date = null;
        if (StringUtils.isEmpty(dateString)) {
            return null;
        }
        for (Pattern pattern : SUPPORT_FORMAT_MAP.keySet()) {
            Matcher matcher = pattern.matcher(dateString);
            if (matcher.matches()) {
                try {
                    String format = SUPPORT_FORMAT_MAP.get(pattern);
                    SimpleDateFormat sdf = new SimpleDateFormat(format);
                    sdf.setLenient(false);
                    date = sdf.parse(dateString);
                } catch (ParseException ignored) {
 
                }
                break;
            }
        }
        return date;
    }
 
    /**
     * 带时区字符串转日期类型
     *
     * @param dateString
     * @return
     */
    public static Date timeZoneConvert(String dateString) {
        Date date = null;
        if (StringUtils.isNotBlank(dateString)) {
            Matcher matcher = DATE_TIME_PATTERN_ZT.matcher(dateString);
            if (matcher.matches()) {
                try {
                    String format = SUPPORT_FORMAT_MAP.get(DATE_TIME_PATTERN_ZT);
                    SimpleDateFormat sdf = new SimpleDateFormat(format);
                    sdf.setLenient(false);
                    date = sdf.parse(dateString);
                } catch (ParseException ignored) {
                }
            }
        }
        return date;
    }
 
    /**
     * 根据格式,转化字符串为日期
     *
     * @param dateString
     * @param format
     * @return
     */
    public static Date convert(String dateString, String format) {
        if (StringUtils.isBlank(dateString)) {
            return null;
        }
        boolean matches = false;
        for (String fmt : SUPPORT_FORMAT_MAP.values()) {
            if (fmt.equals(format)) {
                matches = true;
                break;
            }
        }
        if (!matches) {
            throw new IllegalArgumentException(format + " is not a valid date format");
        }
        Date date = null;
        try {
            date = new SimpleDateFormat(format).parse(dateString);
        } catch (ParseException ignored) {
 
        }
        return date;
    }
 
    /**
     * 根据格式,转化字符串为日期
     *
     * @param dateString
     * @param format
     * @return
     */
    public static Date convertSimple(String dateString, String format) {
        if (StringUtils.isBlank(dateString)) {
            return null;
        }
        Date date = null;
        try {
            date = new SimpleDateFormat(format).parse(dateString);
        } catch (ParseException ignored) {
 
        }
        return date;
    }
 
    /**
     * 根据时间字符串解析成Date对象,该方法会自适应时间字符串格式
     *
     * @param dateText 时间字符串
     */
    public static Date parse(String dateText) {
        return DateUtils.convert(dateText);
    }
 
    /**
     * 根据时间字符串,解析成指定格式的的Date对象
     *
     * @param dateText 时间字符串
     * @param format   指定解析格式
     */
    public static Date parse(String dateText, String format) {
        return DateUtils.convert(dateText, format);
    }
 
    /**
     * 格式化日期为yyyy-MM-dd HH:mm:ss格式字符串
     *
     * @param date 日期对象
     */
    public static String format(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        simpleDateFormat.applyPattern(DATE_TIME_FORMAT);
        return simpleDateFormat.format(date);
    }
 
    /**
     * 格式化日期为指定格式字符串
     *
     * @param date 日期对象
     */
    public static String format(Date date, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        simpleDateFormat.applyPattern(pattern);
        return simpleDateFormat.format(date);
    }
 
    /**
     * 格式化日期为指定格式字符串
     *
     * @param date 日期对象
     */
    public static String formatToUtc(Date date, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        df.setTimeZone(new SimpleTimeZone(0, "UTC"));
        return df.format(date);
    }
 
    /**
     * 格式化日期为yyyy-MM-dd HH:mm:ss格式字符串
     *
     * @param timestamp 时间戳
     */
    public static String format(long timestamp) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        simpleDateFormat.applyPattern(DATE_TIME_FORMAT);
        return simpleDateFormat.format(DateUtils.newDate(timestamp));
    }
 
    /**
     * 格式化日期为指定格式字符串
     *
     * @param timestamp 时间戳
     */
    public static String format(long timestamp, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        simpleDateFormat.applyPattern(pattern);
        return simpleDateFormat.format(DateUtils.newDate(timestamp));
    }
 
    /**
     * 格式化日期为时间戳
     */
    public static long format(String dateText) {
        if (StringUtils.isEmpty(dateText)) {
            return 0L;
        }
        Date date=DateUtils.convert(dateText);
        return date==null ? 0L : date.getTime();
    }
 
    /**
     * 获取当前时间的日期对象
     */
    public static Date now() {
        return DateUtils.newDate(DateUtils.nowTimestamp());
    }
 
    /**
     * 获取当前时间时间戳
     */
    public static long nowTimestamp() {
        return System.currentTimeMillis();
    }
 
    /**
     * 根据时间戳创建日期对象
     *
     * @param timestamp 时间戳
     */
    public static Date newDate(long timestamp) {
        return new Date(timestamp);
    }
 
    /**
     * 根据指定时间创建日期对象
     *
     * @param year        年
     * @param month       月
     * @param day         日
     * @param hour        时
     * @param minute      分
     * @param second      秒
     * @param millisecond 毫秒
     */
    public static Date newDate(int year, int month, int day, int hour, int minute, int second, int millisecond) {
        DateTime dateTime = new DateTime(year, month, day, hour, minute, second, millisecond);
        return dateTime.toDate();
    }
 
    /**
     * 根据指定时间创建日期对象
     *
     * @param year   年
     * @param month  月
     * @param day    日
     * @param hour   时
     * @param minute 分
     * @param second 秒
     */
    public static Date newDate(int year, int month, int day, int hour, int minute, int second) {
        return DateUtils.newDate(year, month, day, hour, minute, second, 0);
    }
 
    /**
     * 根据指定时间创建日期对象
     *
     * @param year   年
     * @param month  月
     * @param day    日
     * @param hour   时
     * @param minute 分
     */
    public static Date newDate(int year, int month, int day, int hour, int minute) {
        return DateUtils.newDate(year, month, day, hour, minute, 0, 0);
    }
 
    /**
     * 根据指定时间创建日期对象
     *
     * @param year  年
     * @param month 月
     * @param day   日
     * @param hour  时
     */
    public static Date newDate(int year, int month, int day, int hour) {
        return DateUtils.newDate(year, month, day, hour, 0, 0, 0);
    }
 
    /**
     * 根据指定时间创建日期对象
     *
     * @param year  年
     * @param month 月
     * @param day   日
     */
    public static Date newDate(int year, int month, int day) {
        return DateUtils.newDate(year, month, day, 0, 0, 0, 0);
    }
 
    /**
     * 日期加减 年月日时分秒毫秒数 正数加负数减
     *
     * @param date         日期
     * @param years        加减年数
     * @param months       加减月数
     * @param days         加减天数
     * @param hours        加减小时数
     * @param minutes      加减分钟数
     * @param seconds      加减秒数
     * @param milliseconds 加减毫秒数
     */
    public static Date add(Date date, int years, int months, int days, int hours, int minutes, int seconds, int milliseconds) {
        return DateUtils.add(date.getTime(), years, months, days, hours, minutes, seconds, milliseconds);
    }
 
    /**
     * 日期加减 年月日时分秒毫秒数 正数加负数减
     *
     * @param timestamp    时间戳
     * @param years        加减年数,如:1、-1
     * @param months       加减月数,如:1、-1
     * @param days         加减天数,如:1、-1
     * @param hours        加减小时数,如:1、-1
     * @param minutes      加减分钟数,如:1、-1
     * @param seconds      加减秒数,如:1、-1
     * @param milliseconds 加减毫秒数,如:1、-1
     */
    public static Date add(long timestamp, int years, int months, int days, int hours, int minutes, int seconds, int milliseconds) {
        int zero = 0;
        DateTime dateTime = new DateTime(timestamp);
        if (zero != years) {
            dateTime = dateTime.plusYears(years);
        }
        if (zero != months) {
            dateTime = dateTime.plusMonths(months);
        }
        if (zero != days) {
            dateTime = dateTime.plusDays(days);
        }
        if (zero != hours) {
            dateTime = dateTime.plusHours(hours);
        }
        if (zero != minutes) {
            dateTime = dateTime.plusMinutes(minutes);
        }
        if (zero != seconds) {
            dateTime = dateTime.plusSeconds(seconds);
        }
        if (zero != milliseconds) {
            dateTime = dateTime.plusMillis(milliseconds);
        }
        return dateTime.toDate();
    }
 
    /**
     * 获取本周的第一天
     *
     * @return Long
     **/
    public static Long getWeekStart(long timestamp) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(timestamp);
        Integer date = cal.get(Calendar.DATE);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        if (cal.get(Calendar.DATE) > date) {
            cal.add(Calendar.DATE, -7);
        }
        Date time = cal.getTime();
        return format(new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00");
    }
 
    /**
     * 获取本周的最后一天
     *
     * @return Long
     **/
    public static Long getWeekEnd(long timestamp) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(timestamp);
        Integer date = cal.get(Calendar.DATE);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        if (cal.get(Calendar.DATE) < date) {
            cal.add(Calendar.DATE, 7);
        }
        Date time = cal.getTime();
        return format(new SimpleDateFormat("yyyy-MM-dd").format(time) + " 23:59:59");
    }
 
    /**
     * 获取本天的最后一秒
     *
     * @return Long
     **/
    public static Date getDayEnd(Date date) {
        if (date == null) {
            return null;
        }
        return parse(new SimpleDateFormat("yyyy-MM-dd").format(date.getTime()) + " 23:59:59");
    }
 
    /**
     * 获取本天的第一秒
     *
     * @return Long
     **/
    public static Date getDayStart(Date date) {
        if (date == null) {
            return null;
        }
        return parse(new SimpleDateFormat("yyyy-MM-dd").format(date.getTime()) + " 00:00:00");
    }
 
    /**
     * 根据时间戳获取版本号,Calendar取周是从周日开始计算,需要进行修改为以周一为准
     *
     * @param timestamp 时间戳
     * @return 版本号
     */
    public static Integer getDateVersion(long timestamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestamp);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setMinimalDaysInFirstWeek(4);
        return calendar.get(Calendar.YEAR) * 1000 + calendar.get(Calendar.WEEK_OF_YEAR);
    }
 
    /**
     * 获取当前时间的版本号
     *
     * @return 版本号
     */
    public static Integer getNowVersion() {
        return getDateVersion(nowTimestamp());
    }
 
 
    /**
     * 获取年月周
     *
     * @return String
     **/
    public static Map<String, String> getYearMonthWeekStr(long timestamp) {
        // 当月第一天
        Calendar firstDay = Calendar.getInstance();
        firstDay.setTimeInMillis(timestamp);
        firstDay.set(Calendar.DAY_OF_MONTH, 1);
 
        // 当月最后一天
        Calendar lastDay = Calendar.getInstance();
        lastDay.setTimeInMillis(timestamp);
        lastDay.add(Calendar.MONTH, 1);
        lastDay.set(Calendar.DAY_OF_MONTH, 0);
 
        // 第一个周日
        Calendar firsSunday = Calendar.getInstance();
        firsSunday.setTimeInMillis(firstDay.getTimeInMillis());
        firsSunday.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        if (firsSunday.getTimeInMillis() != firstDay.getTimeInMillis()) {
            firsSunday.add(Calendar.DATE, 7);
        }
 
 
        // 最后一个周一
        Calendar lastMonday = Calendar.getInstance();
        lastMonday.setTimeInMillis(lastDay.getTimeInMillis());
        lastMonday.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        if (lastMonday.getTimeInMillis() > lastDay.getTimeInMillis()) {
            lastMonday.add(Calendar.DATE, -7);
        }
 
        String year = "";
        String month = "";
        String week = "";
        DateFormat df = new SimpleDateFormat("MMM", Locale.ENGLISH);
        if (firsSunday.getTimeInMillis() >= timestamp) {
            // 三天以上算本月
            if (firsSunday.get(Calendar.DATE) > NUM_3) {
                Calendar cal = Calendar.getInstance();
                cal.setTimeInMillis(timestamp);
                year = cal.get(Calendar.YEAR) + "";
                month = df.format(cal.getTime());
 
                week = cal.get(Calendar.WEEK_OF_MONTH) + "th Week";
            } else {
                // 三天以内算上月
                // 上月第一天
                Calendar preFirstDay = Calendar.getInstance();
                preFirstDay.setTimeInMillis(timestamp);
                preFirstDay.set(Calendar.DAY_OF_MONTH, 1);
                preFirstDay.add(Calendar.MONTH, -1);
 
                // 上月第一个周日
                Calendar preFirsSunday = Calendar.getInstance();
                preFirsSunday.setTimeInMillis(preFirstDay.getTimeInMillis());
                preFirsSunday.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
                if (preFirsSunday.getTimeInMillis() != preFirstDay.getTimeInMillis()) {
                    preFirsSunday.add(Calendar.DATE, 7);
                }
 
                // 上月最后一天
                Calendar preLastDay = Calendar.getInstance();
                preLastDay.setTimeInMillis(firstDay.getTimeInMillis());
                preLastDay.add(Calendar.DATE, -1);
 
                year = preLastDay.get(Calendar.YEAR) + "";
                month = df.format(preLastDay.getTime());
                if (preFirsSunday.get(Calendar.DATE) > NUM_3) {
                    week = preLastDay.get(Calendar.WEEK_OF_MONTH) + "th Week";
                } else {
                    week = (preLastDay.get(Calendar.WEEK_OF_MONTH) - 1) + "th Week";
                }
            }
        } else if (lastMonday.getTimeInMillis() <= timestamp) {
            // 本月在最后一周占4天及以上
            if (lastDay.get(Calendar.DAY_OF_WEEK) >= NUM_5 || lastDay.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                Calendar cal = Calendar.getInstance();
                cal.setTimeInMillis(timestamp);
                year = cal.get(Calendar.YEAR) + "";
                month = df.format(cal.getTime());
                if (firsSunday.get(Calendar.DATE) > NUM_3) {
                    week = cal.get(Calendar.WEEK_OF_MONTH) + "th Week";
                } else {
                    week = (cal.get(Calendar.WEEK_OF_MONTH) - 1) + "th Week";
                }
            } else {
                // 下月第一天
                Calendar nextFirstDay = Calendar.getInstance();
                nextFirstDay.setTimeInMillis(timestamp);
                nextFirstDay.add(Calendar.MONTH, 1);
                nextFirstDay.set(Calendar.DAY_OF_MONTH, 1);
 
                year = nextFirstDay.get(Calendar.YEAR) + "";
                month = df.format(nextFirstDay.getTime());
                week = nextFirstDay.get(Calendar.WEEK_OF_MONTH) + "th Week";
            }
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(timestamp);
 
            year = cal.get(Calendar.YEAR) + "";
            month = df.format(cal.getTime());
            if (firsSunday.get(Calendar.DATE) > NUM_3) {
                week = cal.get(Calendar.WEEK_OF_MONTH) + "th Week";
            } else {
                week = (cal.get(Calendar.WEEK_OF_MONTH) - 1) + "th Week";
            }
        }
 
        Map<String, String> result = new HashMap<String, String>();
        result.put("year", year);
        result.put("month", month);
        result.put("week", week);
        return result;
    }
 
    /**
     * 添加年份
     *
     * @param date
     * @param years
     * @return
     */
    public static Date addYears(Date date, int years) {
        return DateUtils.addYears(date.getTime(), years);
    }
 
    /**
     * 添加年份
     *
     * @param timestamp
     * @param years
     * @return
     */
    public static Date addYears(long timestamp, int years) {
        return DateUtils.add(timestamp, years, 0, 0, 0, 0, 0, 0);
    }
 
    /**
     * 添加月份
     *
     * @param date
     * @param months
     * @return
     */
    public static Date addMonths(Date date, int months) {
        return DateUtils.addMonths(date.getTime(), months);
    }
 
    /**
     * 添加月份
     *
     * @param timestamp
     * @param months
     * @return
     */
    public static Date addMonths(long timestamp, int months) {
        return DateUtils.add(timestamp, 0, months, 0, 0, 0, 0, 0);
    }
 
    /**
     * 添加日期
     *
     * @param date
     * @param days
     * @return
     */
    public static Date addDays(Date date, int days) {
        return DateUtils.addDays(date.getTime(), days);
    }
 
    /**
     * 添加日期
     *
     * @param timestamp
     * @param days
     * @return
     */
    public static Date addDays(long timestamp, int days) {
        return DateUtils.add(timestamp, 0, 0, days, 0, 0, 0, 0);
    }
 
    /**
     * 添加小时
     *
     * @param date
     * @param hours
     * @return
     */
    public static Date addHours(Date date, int hours) {
        return DateUtils.addHours(date.getTime(), hours);
    }
 
    /**
     * 添加小时
     *
     * @param timestamp
     * @param hours
     * @return
     */
    public static Date addHours(long timestamp, int hours) {
        return DateUtils.add(timestamp, 0, 0, 0, hours, 0, 0, 0);
    }
 
    /**
     * 添加分钟
     *
     * @param date
     * @param minutes
     * @return
     */
    public static Date addMinutes(Date date, int minutes) {
        return DateUtils.addMinutes(date.getTime(), minutes);
    }
 
    /**
     * 添加分钟
     *
     * @param timestamp
     * @param minutes
     * @return
     */
    public static Date addMinutes(long timestamp, int minutes) {
        return DateUtils.add(timestamp, 0, 0, 0, 0, minutes, 0, 0);
    }
 
    /**
     * 获取当前月份
     *
     * @return
     */
    public static int getCurrentMonth() {
        DateTime dateTime = DateTime.now();
        return dateTime.getMonthOfYear();
    }
 
    /**
     * 通过时间秒毫秒数判断两个时间的间隔
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDaysByMillisecond(Date date1, Date date2) {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
        return days;
    }
 
    /**
     * 取两个日期的中间日期,默认取大
     *
     * @param start
     * @param end
     * @return
     */
    public static Date getMeddleDate(Date start, Date end) {
        int days = (int) ((end.getTime() - start.getTime()) / (1000 * 3600 * 24));
        return DateUtils.addDays(start, (int) Math.floor(Double.valueOf(days) / 2));
    }
 
    /**
     * 通过时间秒毫秒数判断两个时间的间隔
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentSeconds(Date date1, Date date2) {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000));
        return days;
    }
 
    /**
     * 按日期递增规则生成分区列表
     *
     * @param start
     * @param end
     * @return
     */
    public static List<String> generateDayList(String start, String end, String pattern) {
        List<String> partitionList = new ArrayList<String>();
        partitionList.add(start);
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date startDate;
        Date endDate;
        try {
            startDate = sdf.parse(start);
            endDate = sdf.parse(end);
        } catch (Exception e) {
 
            return null;
        }
        Calendar startCal = Calendar.getInstance();
        Calendar endCal = Calendar.getInstance();
        startCal.setTime(startDate);
        endCal.setTime(endDate);
        while (startCal.before(endCal)) {
            startCal.add(Calendar.DAY_OF_MONTH, 1);
            partitionList.add(sdf.format(startCal.getTime()));
        }
        return partitionList;
    }
 
    /**
     * 格式化utc时间
     *
     * @param utcDate
     * @return
     */
    public static String formatUtcDate(String utcDate) {
        DateFormat df = new SimpleDateFormat(DateUtils.DATE_TIME_GL_ST_FORMAT);
        DateFormat df2 = new SimpleDateFormat(DateUtils.DATE_TIME_FORMAT);
 
        Date date = null;
        try {
            date = df.parse(utcDate);
        } catch (ParseException e) {
            return utcDate;
        }
        return df2.format(date);
    }
 
    /**
     * 变更日期字符串格式
     *
     * @return
     */
    public static String convertDateStrPattern(String dateStr, String sourcePattern, String targetPattern) {
        DateFormat df = new SimpleDateFormat(sourcePattern);
        DateFormat df2 = new SimpleDateFormat(targetPattern);
 
        Date date = null;
        try {
            date = df.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
        return df2.format(date);
    }
 
    /**
     * 格式化utc时间
     *
     * @param localDate
     * @return
     */
    public static Date localToUTC(Date localDate) {
        long localTimeInMillis = localDate.getTime();
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(localTimeInMillis);
        int zoneOffset = calendar.get(Calendar.ZONE_OFFSET);
        int dstOffset = calendar.get(Calendar.DST_OFFSET);
        calendar.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));
        Date utcDate = new Date(calendar.getTimeInMillis());
        return utcDate;
    }
 
    /**
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
 
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        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 Long timestamp2dateLong(Long timestamp) {
        return DateUtils.format(DateUtils.format(timestamp).substring(0, 10));
    }
 
    public static boolean isValidDate(String str, String pattern) {
        boolean convertSuccess = true;
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            convertSuccess = false;
        }
        return convertSuccess;
    }
}