wanghc
2023-07-04 a042e0e63f86a0880d20e57c14ec4a60b65801fb
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
package com.jttech.pfcs.util;
 
import org.springframework.util.StringUtils;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
 
public class DateUtil {
    public static final SimpleDateFormat         yyyyMMdd    = new SimpleDateFormat("yyyy-MM-dd");
 
    private static ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<SimpleDateFormat>() {
                                                                 @Override
                                                                 protected SimpleDateFormat initialValue() {
                                                                     return new SimpleDateFormat("yyyy-MM-dd");
                                                                 }
                                                             };
 
    public static String[]                       pattern     = new String[] { "yyyyMMdd", "yyyy-MM-dd", "yyyy.MM.dd", "yyyy/MM/dd",
            "yyyy-MM", "yyyyMM", "yyyy/MM", "yyyyMMddHHmmss", "yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss",
            "yyyyMMddHHmmssSSS", "HH:mm", "HHmmss", "MM-dd HH:mm", "YYYY-MM-dd HH:mm", "MM-dd HH:mm:ss" };
 
 
 
    public static Date previous(int days) {
        return new Date(System.currentTimeMillis() - days * 3600000L * 24L);
    }
 
 
 
    public static Date addDay(Date date, int days) {
        return new Date(date.getTime() + days * 3600000L * 24L);
    }
 
 
 
    public static Date addMin(Date date, int mins) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MINUTE, mins);
        // System.out.println(c.getTime());
        return c.getTime();
    }
 
 
 
    public static Date addHour(Date date, int hours) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.HOUR_OF_DAY, hours);
        // System.out.println(c.getTime());
        return c.getTime();
    }
 
 
 
    public static String formatDateTime(String format, long d) {
        return new SimpleDateFormat(format).format(d);
    }
 
 
 
    public static String formatDate(String format, Date d) {
 
        return StringUtils.isEmpty(format) || null == d ? "" : new SimpleDateFormat(format).format(d);
    }
 
 
 
    public static String formatDate(Date d) {
 
        return formatDate(pattern[7], d);
    }
 
 
 
    public static Date parseDate(String format, String d) {
        try {
            return new SimpleDateFormat(format).parse(d);
        } catch (Exception e) {
            throw new RuntimeException("需要时间格式为:" + format);
        }
    }
 
 
 
    public static Date getNextWeekDay(int weekDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
 
        int addDateNumber = 0;
        if (Calendar.SUNDAY == calendar.get(Calendar.DAY_OF_WEEK)) {
            if (Calendar.SUNDAY == weekDay) {
                addDateNumber = 7;
            }
        } else {
            addDateNumber = 7;
            if (Calendar.SUNDAY == weekDay) {
                addDateNumber = 14;
            }
        }
        calendar.add(Calendar.DATE, addDateNumber);
        calendar.set(Calendar.DAY_OF_WEEK, weekDay);
        return calendar.getTime();
    }
 
 
 
    public static Date getThisWeekMonday() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
 
        int min = calendar.getActualMinimum(Calendar.DAY_OF_WEEK); // 获取周开始基准
        int current = calendar.get(Calendar.DAY_OF_WEEK); // 获取当天周内天数
        calendar.add(Calendar.DAY_OF_WEEK, min - current + 1); // 当天-基准,获取周开始日期
        return calendar.getTime();
    }
 
 
 
    /**
     * 获取所在日期周的星期一(周一到周日为一周)
     *
     * @return 日期所在周的星期一
     */
    public static Date getWeekMonday(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);
 
        int min = calendar.getActualMinimum(Calendar.DAY_OF_WEEK); // 获取周开始基准
        int current = calendar.get(Calendar.DAY_OF_WEEK); // 获取当天周内天数
        calendar.add(Calendar.DAY_OF_WEEK, min - current + 1 + (current == Calendar.SUNDAY ? -7 : 0)); // 当天-基准,获取周开始日期
        return calendar.getTime();
    }
 
 
 
    public static Date parse(String str) {
        try {
            return threadLocal.get().parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
 
 
 
    /**
     * 获取字符串当月第一天,需要date(yyyy-MM-dd)
     *
     * @param date
     * @return
     */
    public static String getFirstDayOfMonth(String date) {
        return getFirstDayOfMonth(parse(date));
    }
 
 
 
    /**
     * 获取字符串当月最后一天
     *
     * @param date
     * @return
     */
    public static String getFirstDayOfMonth(String format, String date) {
        return getFirstDayOfMonth(parseDate(format, date));
    }
 
 
 
    /**
     * 获取字符串当月第一天,需要date(yyyy-MM-dd)
     *
     * @param date
     * @return
     */
    public static String getEndDayOfMonth(String date) {
        return getEndDayOfMonth(parse(date));
    }
 
 
 
    /**
     * 获取字符串当月最后一天
     *
     * @param date
     * @return
     */
    public static String getEndDayOfMonth(String format, String date) {
        return getEndDayOfMonth(parseDate(format, date));
    }
 
 
 
    /**
     * 获取字符串当月最后一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayDateOfMonth(String format, String date) {
        return getFirstDayDateOfMonth(parseDate(format, date));
    }
 
 
 
    /**
     * 获取字符串当月最后一天
     *
     * @param date
     * @return
     */
    public static Date getEndDayDateOfMonth(String format, String date) {
        return getEndDayDateOfMonth(parseDate(format, date));
    }
 
 
 
    public static String getFirstDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, 1);
        return threadLocal.get().format(cal.getTime());
    }
 
 
 
    public static String getEndDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return threadLocal.get().format(cal.getTime());
    }
 
 
 
    public static Date getFirstDayDateOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, 1);
        return cal.getTime();
    }
 
 
 
    public static Date getEndDayDateOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }
 
 
 
    public static Date addMonth(Date date, int count) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(2, count);
        return c.getTime();
    }
 
 
 
    public static Date addYear(Date date, int count) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.YEAR, count);
        return c.getTime();
    }
 
 
 
    public static String getCurrentDate() {
        return threadLocal.get().format(new Date());
    }
 
 
 
    public static Date getTodayDate() {
        try {
            return threadLocal.get().parse(getCurrentDate());
        } catch (ParseException e) {
            throw new RuntimeException();
        }
    }
 
 
 
    public static int getDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        return cal.get(7);
    }
 
 
 
    public static int getDayOfWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(7);
    }
 
 
 
    public static int getDayOfWeek(String date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(parse(date));
        return cal.get(7);
    }
 
 
 
    public static int getMaxDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.getActualMaximum(5);
    }
 
 
 
    public static String toString(Date date) {
        if (date == null)
            return "";
        else
            return threadLocal.get().format(date);
    }
 
 
 
    public static String toString(Date date, String format) {
        SimpleDateFormat t = new SimpleDateFormat(format);
        return t.format(date);
    }
 
 
 
    // 获取当前年
    public static String getYear() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
        return formatter.format(new Date());
    }
 
 
 
    // 获取年
    public static String getYear(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
        return formatter.format(date);
    }
 
 
 
    // 获取当年月
    public static String getMonth() {
        SimpleDateFormat formatter = new SimpleDateFormat("MM");
        return formatter.format(new Date());
    }
 
 
 
    // 获取当年月
    public static String getYearMonth() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM");
        return formatter.format(new Date());
    }
 
 
 
    // 获取月
    public static String getMonth(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("MM");
        return formatter.format(date);
    }
 
 
 
    // 获取当日
    public static String getDay() {
        SimpleDateFormat formatter = new SimpleDateFormat("dd");
        return formatter.format(new Date());
    }
 
 
 
    // 获取日
    public static String getDay(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("dd");
        return formatter.format(date);
    }
 
 
 
    /**
     * 月
     *
     * @return
     */
    public static List<String> getMonthList() {
        List<String> list = new ArrayList<String>();
        list.add("01");
        list.add("02");
        list.add("03");
        list.add("04");
        list.add("05");
        list.add("06");
        list.add("07");
        list.add("08");
        list.add("09");
        list.add("10");
        list.add("11");
        list.add("12");
        return list;
    }
 
 
 
    // 计算相差时间
    public static double dateDiff(String startTime, String endTime, String format) {
        SimpleDateFormat sd = new SimpleDateFormat(format);
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long diff;
        double hour = 0;
        try {
            // 获得两个时间的毫秒时间差异
            diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
            hour = (diff % nd) * 1.0f / nh;// 计算差多少小时
 
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // DecimalFormat df = new DecimalFormat("#.00");
        System.out.println(new Double(String.format("%.2f", hour)));
        return new Double(String.format("%.2f", hour));
    }
 
 
 
    // 计算相差天数时间
    public static int dateDayDiff(String startTime, String endTime, String format) {
        SimpleDateFormat sd = new SimpleDateFormat(format);
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long diff;
        int day = 0;
        try {
            // 获得两个时间的毫秒时间差异
            diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
            day = (int) (diff / nd);// 计算差多少天
 
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return day;
    }
 
 
 
    public static List<WeekDay> getDisplayWeekDays(Date startTime, Date endTime) {
        List<WeekDay> weekDays = new ArrayList<WeekDay>();
        Date date = startTime;
        long endTimeLong = endTime.getTime();
        while (date.getTime() <= endTimeLong) {
            weekDays.add(new WeekDay(date));
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE, 1);
            date = calendar.getTime();
        }
        return weekDays;
    }
 
    public static class WeekDay {
 
        private Date                  date;
 
        private static final String[] WEEK_DAY_NAME = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
 
 
 
        public WeekDay() {
        }
 
 
 
        public WeekDay(Date date) {
            this.setDate(date);
        }
 
 
 
        public Date getDate() {
            return date;
        }
 
 
 
        public void setDate(Date date) {
            this.date = date;
        }
 
 
 
        @SuppressWarnings("deprecation")
        @Override
        public String toString() {
            return threadLocal.get().format(date) + "(" + WEEK_DAY_NAME[date.getDay()] + ")";
        }
 
 
 
        @SuppressWarnings("deprecation")
        public String getDay() {
            return WEEK_DAY_NAME[date.getDay()];
        }
    }
 
 
 
    // 指定日期所在月的第一天日期
    public static Date getBeginDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, 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();
    }
 
 
 
    // 获取指定日期的下个月的日期
    public static Date getNextBeginDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.MONTH, +1);
        return calendar.getTime();
    }
 
 
 
    // 指定日期所在年的第一天日期
    public static Date getBeginDayOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DATE, 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();
    }
 
 
 
    public static Date getEndDayOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
        return calendar.getTime();
    }
 
 
 
    // 获取指定日期的下年的日期
    public static Date getNextBeginDayOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, 1);
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.YEAR, +1);
        return calendar.getTime();
    }
 
 
 
    /**
     * @param startDate
     * @param endDate
     * @return
     * @功能描述:
     *        <p>
     *        获取两个时间相差秒
     *        </p>
     * @创建作者: lance
     * @创建日期: 2016年12月12日 下午12:02:30
     */
    public static Integer getTimeSecond(Date startDate, Date endDate) {
        long start = startDate.getTime();
        long end = endDate.getTime();
        int second = (int) ((end - start) / 1000);
        return second;
 
    }
 
 
 
    /**
     * @param beginDate
     * @param endDate
     * @return
     * @功能描述:
     *        <p>
     *        获取两个日期之间的天数
     *        </p>
     * @创建作者: lance
     * @创建日期: 2017年1月6日 上午10:52:47
     */
    public static Integer getDays(String beginDate, String endDate) {
        SimpleDateFormat sd = new SimpleDateFormat(DateUtil.pattern[1]);
        try {
            return (int) ((sd.parse(endDate).getTime() - sd.parse(beginDate).getTime()) / 86400000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
 
 
 
    /**
     * @param beginDate
     * @param endDate
     * @return
     * @功能描述:
     *        <p>
     *        获取两个日期之间的天数
     *        </p>
     * @创建作者: lance
     * @创建日期: 2017年1月6日 上午10:52:47
     */
    public static Integer getDays(Date beginDate, Date endDate) {
        return (int) ((getDayBeginTime(endDate).getTime() - getDayBeginTime(beginDate).getTime()) / 86400000);
    }
 
 
 
    /**
     * @param date
     * @return
     * @功能描述:
     *        <p>
     *        获取两个日期之间的天数
     *        </p>
     * @创建作者: lance
     * @创建日期: 2017年1月6日 上午10:52:47
     */
    public static Date getDayBeginTime(Date date) {
        try {
            String dateStr = threadLocal.get().format(date);
            return threadLocal.get().parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
 
 
 
    public static Integer getDayOfHour(Date date) {
        String hour = new SimpleDateFormat("HH").format(date);
        return Integer.valueOf(hour);
    }
 
 
 
    public static Integer getDayOfMinute(Date date) {
        String hour = new SimpleDateFormat("mm").format(date);
        return Integer.valueOf(hour);
    }
 
 
 
    public static Date getDayEndTime(Date date) {
        try {
            return new Date(getDayBeginTime(date).getTime() + 0x5265BFFL);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
 
 
    /**
     * 获取当前时间距离当天结束的分钟数
     *
     * @param date
     * @return
     */
    public static Integer getDayEndMinute(Date date) {
        Date dayEndTime = getDayEndTime(date);
        Integer timeSecond = getTimeSecond(date, dayEndTime);
        return timeSecond / 60;
    }
 
 
 
    /**
     * 计算两点时间间隔年
     *
     * @param startTime
     * @param endTime
     * @param format
     * @return
     */
    public static int betweenTimesYear(String startTime, String endTime, String format) {
        SimpleDateFormat sd = new SimpleDateFormat(format);
 
        int year = 0;
        try {
            long end = sd.parse(endTime).getTime();
            long start = sd.parse(startTime).getTime();
            long oneYear = 1000l * 60 * 60 * 24 * 365;
            year = (int) ((end - start) / oneYear);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return year;
    }
 
 
 
    public static int getAgeByBirth(Date birthday) {
        int age = 0;
        try {
            Calendar now = Calendar.getInstance();
            now.setTime(new Date());// 当前时间
 
            Calendar birth = Calendar.getInstance();
            birth.setTime(birthday);
 
            if (birth.after(now)) {// 如果传入的时间,在当前时间的后面,返回0岁
                age = 0;
            } else {
                age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
                if (now.get(Calendar.DAY_OF_YEAR) < birth.get(Calendar.DAY_OF_YEAR)) {
                    age -= 1;
                }
            }
            return age;
        } catch (Exception e) {
            throw new RuntimeException("身份证号异常");
        }
    }
 
 
 
    /**
     * 获取精确到秒的时间戳
     *
     * @param pDate
     * @return
     */
    public static int getSecondTimestamp(Date pDate) {
        if (null == pDate) {
            return 0;
        }
        String timestamp = String.valueOf(pDate.getTime() / 1000);
        return Integer.valueOf(timestamp);
    }
 
 
 
    /**
     * 秒时间戳转成时间
     *
     * @param pSecondTimestamp
     * @return
     */
    public static Date parseSecondTimestamp(int pSecondTimestamp) {
        Date date = null;
        if (pSecondTimestamp != 0) {
            date = new Date(pSecondTimestamp * 1000L);
        }
        return date;
    }
 
 
 
    public static Date getLastQuarterStartTime() {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.set(Calendar.MONTH, (startCalendar.get(Calendar.MONTH) / 3 - 1) * 3);
        startCalendar.set(Calendar.DAY_OF_MONTH, 1);
        setMinTime(startCalendar);
        return startCalendar.getTime();
    }
 
 
 
    public static Date getLastQuarterEndTime() {
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.set(Calendar.MONTH, (endCalendar.get(Calendar.MONTH) / 3 - 1) * 3 + 2);
        endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        setMaxTime(endCalendar);
 
        return endCalendar.getTime();
    }
 
 
 
    public static Date getQuarterStartTime(Integer year, Integer quarter) {
        Calendar quarterCalendar = Calendar.getInstance();
        quarterCalendar.set(Calendar.YEAR, year);
        switch (quarter) {
        case 1:
            quarterCalendar.set(Calendar.MONTH, 0);
            break;
        case 2:
            quarterCalendar.set(Calendar.MONTH, 3);
            break;
        case 3:
            quarterCalendar.set(Calendar.MONTH, 6);
            break;
        case 4:
            quarterCalendar.set(Calendar.MONTH, 9);
            break;
        default:
        }
        quarterCalendar.set(Calendar.DAY_OF_MONTH, 1);
        setMinTime(quarterCalendar);
        return quarterCalendar.getTime();
    }
 
 
 
    public static Date getQuarterEndTime(Integer year, Integer quarter) {
        Calendar quarterCalendar = Calendar.getInstance();
        quarterCalendar.set(Calendar.YEAR, year);
        switch (quarter) {
        case 1:
            quarterCalendar.set(Calendar.MONTH, 2);
            break;
        case 2:
            quarterCalendar.set(Calendar.MONTH, 5);
            break;
        case 3:
            quarterCalendar.set(Calendar.MONTH, 8);
            break;
        case 4:
            quarterCalendar.set(Calendar.MONTH, 11);
            break;
        default:
        }
        quarterCalendar.set(Calendar.DAY_OF_MONTH, quarterCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        setMaxTime(quarterCalendar);
        return quarterCalendar.getTime();
    }
 
 
 
    public static Date getMonthStartTime(Integer year, Integer month) {
        Calendar monthCalendar = Calendar.getInstance();
        monthCalendar.set(Calendar.YEAR, year);
        monthCalendar.set(Calendar.MONTH, month - 1);
        monthCalendar.set(Calendar.DAY_OF_MONTH, 1);
        setMinTime(monthCalendar);
        return monthCalendar.getTime();
    }
 
 
 
    public static Date getMonthEndTime(Integer year, Integer month) {
        Calendar monthCalendar = Calendar.getInstance();
        monthCalendar.set(Calendar.YEAR, year);
        monthCalendar.set(Calendar.MONTH, month - 1);
        monthCalendar.set(Calendar.DAY_OF_MONTH, monthCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        setMaxTime(monthCalendar);
        return monthCalendar.getTime();
    }
 
 
 
    public static Date getYearStartTime(Integer year) {
        Calendar quarterCalendar = Calendar.getInstance();
        quarterCalendar.set(Calendar.YEAR, year);
        quarterCalendar.set(Calendar.MONTH, 0);
        quarterCalendar.set(Calendar.DAY_OF_MONTH, 1);
        setMinTime(quarterCalendar);
        return quarterCalendar.getTime();
    }
 
 
 
    public static Date getYearEndTime(Integer year) {
        Calendar quarterCalendar = Calendar.getInstance();
        quarterCalendar.set(Calendar.YEAR, year);
        quarterCalendar.set(Calendar.MONTH, 11);
        quarterCalendar.set(Calendar.DAY_OF_MONTH, quarterCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        setMaxTime(quarterCalendar);
        return quarterCalendar.getTime();
    }
 
 
 
    public static Date getDayEndTimeWithoutMillisecond(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, 0);
        return calendar.getTime();
    }
 
 
 
    private static void setMinTime(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }
 
 
 
    private static void setMaxTime(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
    }
 
}