派生自 projectDept/qhighschool

yn147
2023-11-23 bccada7cbf7eea3c37c0243d95426d1a29d9121f
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
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
package com.qxueyou.scc.base.util;
 
import java.math.BigDecimal;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
 
import org.apache.commons.lang3.StringUtils;
 
/**
 * 扩展有关日期工具类的方法。
 *
 * @author 邓志永
 * @since JDK1.6
 * @history 2014-11-28 邓志永 新建
 */
public class DateTimeUtils {
 
    /**
     * 设置 UTC time zone (often referred to as GMT)区域.
     */
    public static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone("GMT");
 
    /**
     * YEAR_RANGE_SCOPE
     */
    public final static int YEAR_RANGE_SCOPE = 4;
 
    /**
     * 设置毫秒和秒之间的对应关系
     */
    public static final long MILLIS_PER_SECOND = 1000;
 
    /**
     * 设置毫秒和分之间的关系.
     */
    public static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND;
 
    /**
     * 设置毫秒和分之间的约束关系。
     */
    public static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE;
 
    /**
     * 设置一天为多少小时.
     */
    public static final long MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR;
 
    /**
     * SEMI_MONTH
     */
    public final static int SEMI_MONTH = 1001;
 
    /**
     * fields
     */
    private static final int[][] FIELDS = { { Calendar.MILLISECOND }, { Calendar.SECOND }, { Calendar.MINUTE }, { Calendar.HOUR_OF_DAY, Calendar.HOUR }, { Calendar.DATE, Calendar.DAY_OF_MONTH, Calendar.AM_PM }, { Calendar.MONTH, DateTimeUtils.SEMI_MONTH }, { Calendar.YEAR }, { Calendar.ERA } };
 
    /**
     * RANGE_WEEK_SUNDAY
     */
    public final static int RANGE_WEEK_SUNDAY = 1;
 
    /**
     * RANGE_WEEK_MONDAY
     */
    public final static int RANGE_WEEK_MONDAY = 2;
 
    /**
     * RANGE_WEEK_RELATIVE
     */
    public final static int RANGE_WEEK_RELATIVE = 3;
 
    /**
     * RANGE_WEEK_CENTER
     */
    public final static int RANGE_WEEK_CENTER = 4;
 
    /**
     * RANGE_MONTH_SUNDAY
     */
    public final static int RANGE_MONTH_SUNDAY = 5;
 
    /**
     * RANGE_MONTH_MONDAY
     */
    public final static int RANGE_MONTH_MONDAY = 6;
 
    /** ISO_DATETIME_FORMAT */
    public final static String ISO_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
 
    /** ISO_DATE_FORMAT */
    public final static String ISO_DATE_FORMAT = "yyyy-MM-dd";
 
    /** ISO_SHORT_DATE_FORMAT */
    public final static String ISO_SHORT_DATE_FORMAT = "yyyy-MM";
 
    /** ISO_DATE_FORMAT */
    public final static String ISO_YEAR_FORMAT = "yyyy";
 
    /** ISO_TIME_FORMAT */
    public final static String ISO_TIME_FORMAT = "HH:mm:ss";
    
    /** ISO_TIME_FORMAT */
    public final static String DATE_IS_NULL = "The date must not be null";
 
    
    /**
     * 根据配置的默认日期时间格式来获取指定的对象的字符串信息。
     *
     * @param value
     *            需要被转换的日期时间对象引用。
     * @return 返回的是格式化后的字符串。
     */
    public static String formatDateTime(Timestamp value) {
        return formatDateTime(value, ISO_DATETIME_FORMAT);
    }
 
    /**
     * 根据配置的默认日期时间格式来获取指定的对象的字符串信息。
     *
     * @param value
     *            需要被转换的日期时间对象引用。
     * @param defaultFormat
     *            按照指定的格式来处理当前的日期时间对象。
     * @return 返回的是格式化后的字符串。
     */
    public static String formatDateTime(Timestamp value, String defaultFormat) {
        if (value == null) {
            return "";
        }
        String strFormatStyle = StringUtils.isEmpty(defaultFormat) ? ISO_DATETIME_FORMAT : defaultFormat;
        SimpleDateFormat objSimpleDateFormat = new SimpleDateFormat(strFormatStyle, Locale.CHINA);
        return objSimpleDateFormat.format(value);
    }
 
    public static String formatDate(Timestamp value) {
        return formatDateTime(value, ISO_DATE_FORMAT);
    }
 
    public static String formatYear(Timestamp value) {
        return formatDateTime(value, ISO_YEAR_FORMAT);
    }
 
    public static String formatDate(Timestamp value, String defaultFormat) {
        if (value == null) {
            return "";
        }
        String strFormatStyle = StringUtils.isEmpty(defaultFormat) ? ISO_DATE_FORMAT : defaultFormat;
        SimpleDateFormat objSimpleDateFormat = new SimpleDateFormat(strFormatStyle, Locale.CHINA);
        return objSimpleDateFormat.format(value);
    }
 
    public static String formatDate(java.sql.Date value) {
        return formatDate(value, ISO_DATE_FORMAT);
    }
 
    public static String formatDate(java.util.Date value) {
        return formatDate(value, ISO_DATE_FORMAT);
    }
 
    public static String formatDate(java.util.Date value, String defaultFormat) {
        if (value == null) {
            return "";
        }
        String strFormatStyle = StringUtils.isEmpty(defaultFormat) ? ISO_DATE_FORMAT : defaultFormat;
        SimpleDateFormat objSimpleDateFormat = new SimpleDateFormat(strFormatStyle, Locale.CHINA);
        return objSimpleDateFormat.format(value);
    }
 
    public static String formatDate(java.sql.Date value, String defaultFormat) {
        if (value == null) {
            return "";
        }
        String strFormatStyle = StringUtils.isEmpty(defaultFormat) ? ISO_DATE_FORMAT : defaultFormat;
        SimpleDateFormat objSimpleDateFormat = new SimpleDateFormat(strFormatStyle, Locale.CHINA);
        return objSimpleDateFormat.format(value);
    }
 
    public static String formatTime(Time value, String defaultFormat) {
        if (value == null) {
            return "";
        }
        String strFormatStyle = StringUtils.isEmpty(defaultFormat) ? ISO_TIME_FORMAT : defaultFormat;
        SimpleDateFormat objSimpleDateFormat = new SimpleDateFormat(strFormatStyle, Locale.CHINA);
        return objSimpleDateFormat.format(value);
    }
 
    public static String formatTime(Time value) {
        return formatTime(value, ISO_TIME_FORMAT);
    }
 
    public static String formatShortDate(Timestamp value) {
        return formatDateTime(value, ISO_SHORT_DATE_FORMAT);
    }
 
    public static String formatShortDate(Timestamp value, String defaultFormat) {
        if (value == null) {
            return "";
        }
        String strFormatStyle = StringUtils.isEmpty(defaultFormat) ? ISO_SHORT_DATE_FORMAT : defaultFormat;
        SimpleDateFormat objSimpleDateFormat = new SimpleDateFormat(strFormatStyle, Locale.CHINA);
        return objSimpleDateFormat.format(value);
    }
 
    /**
     * 确定两个日期是否是同一天
     *
     * @param dateSource
     *            需要执行判断的第一个日期时间
     * @param dateDesti
     *            需要执行的第二个日期时间。
     * @return true 如果两个日期都代表了同一天,那么将会返回true。
     */
    public static boolean isSameDay(Date dateSource, Date dateDesti) {
        if (dateSource == null || dateDesti == null) {
            throw new IllegalArgumentException(DATE_IS_NULL);
        }
        Calendar objCalendarSource = Calendar.getInstance();
        objCalendarSource.setTime(dateSource);
        Calendar objCalendarDesti = Calendar.getInstance();
        objCalendarDesti.setTime(dateDesti);
        return isSameDay(objCalendarSource, objCalendarDesti);
    }
 
    public static boolean isSameDay(Calendar calSource, Calendar calDesti) {
        if (calSource == null || calDesti == null) {
            throw new IllegalArgumentException(DATE_IS_NULL);
        }
        return calSource.get(Calendar.ERA) == calDesti.get(Calendar.ERA) && calSource.get(Calendar.YEAR) == calDesti.get(Calendar.YEAR) && calSource.get(Calendar.DAY_OF_YEAR) == calDesti.get(Calendar.DAY_OF_YEAR);
    }
 
    public static boolean isSameInstant(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            throw new IllegalArgumentException(DATE_IS_NULL);
        }
        return date1.getTime() == date2.getTime();
    }
 
    public static boolean isSameInstant(Calendar cal1, Calendar cal2) {
        if (cal1 == null || cal2 == null) {
            throw new IllegalArgumentException(DATE_IS_NULL);
        }
        return cal1.getTime().getTime() == cal2.getTime().getTime();
    }
 
    public static boolean isSameLocalTime(Calendar calSource, Calendar calDesti) {
        return calSource.equals(calDesti);
    }
 
    public static Date parseDate(String value, String[] parsePatterns) throws ParseException {
        if (value == null || parsePatterns == null) {
            throw new IllegalArgumentException("Date and Patterns must not be null");
        }
 
        SimpleDateFormat parser = null;
        ParsePosition pos = new ParsePosition(0);
        for (int i = 0; i < parsePatterns.length; i++) {
            if (i == 0) {
                parser = new SimpleDateFormat(parsePatterns[0],Locale.CHINA);
            } else {
                parser.applyPattern(parsePatterns[i]);
            }
            pos.setIndex(0);
            Date date = parser.parse(value, pos);
            if (date != null && pos.getIndex() == value.length()) {
                return date;
            }
        }
        throw new ParseException("Unable to parse the date: " + value, -1);
    }
 
    public static java.sql.Date nowAsDate() {
        Calendar objCurrentCalendar = Calendar.getInstance();
        StringBuffer sbDateString = new StringBuffer();
        int iYear = objCurrentCalendar.get(Calendar.YEAR);
        int iMonth = objCurrentCalendar.get(Calendar.MONTH) + 1;
        int iDate = objCurrentCalendar.get(Calendar.DATE);
        sbDateString.append(iYear);
        sbDateString.append('-');
        sbDateString.append(iMonth);
        sbDateString.append('-');
        sbDateString.append(iDate);
        try {
            return java.sql.Date.valueOf(sbDateString.toString());
        } catch (Exception e) {
            return new java.sql.Date(System.currentTimeMillis());
        }
    }
 
    public static Time nowAsTime() {
        Calendar objCurrentCalendar = Calendar.getInstance();
        StringBuffer sbTimeString = new StringBuffer();
        int iHourOfDay = objCurrentCalendar.get(Calendar.HOUR_OF_DAY);
        int iMinute = objCurrentCalendar.get(Calendar.MINUTE);
        int iSecond = objCurrentCalendar.get(Calendar.SECOND);
        sbTimeString.append(iHourOfDay);
        sbTimeString.append(':');
        sbTimeString.append(iMinute);
        sbTimeString.append(':');
        sbTimeString.append(iSecond);
        return java.sql.Time.valueOf(sbTimeString.toString());
    }
 
    public static Timestamp nowAsTimestamp() {
        Calendar objCurrentCalendar = Calendar.getInstance();
        return new Timestamp(objCurrentCalendar.getTimeInMillis());
    }
 
    public static Timestamp addYears(Date date, int amount) {
        return add(date, Calendar.YEAR, amount);
    }
 
    public static Timestamp addMonths(Date date, int amount) {
        return add(date, Calendar.MONTH, amount);
    }
 
    public static Timestamp addWeeks(Date date, int amount) {
        return add(date, Calendar.WEEK_OF_YEAR, amount);
    }
 
    public static Timestamp addDays(Date date, int amount) {
        return add(date, Calendar.DAY_OF_MONTH, amount);
    }
 
    public static Timestamp addHours(Date date, int amount) {
        return add(date, Calendar.HOUR_OF_DAY, amount);
    }
 
    public static Timestamp addMinutes(Date date, int amount) {
        return add(date, Calendar.MINUTE, amount);
    }
 
    public static Timestamp addSeconds(Date date, int amount) {
        return add(date, Calendar.SECOND, amount);
    }
 
    public static Timestamp addMilliseconds(Date date, int amount) {
        return add(date, Calendar.MILLISECOND, amount);
    }
 
    public static Timestamp add(Date date, int calendarField, int amount) {
        if (date == null) {
            throw new IllegalArgumentException(DATE_IS_NULL);
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(calendarField, amount);
        return new Timestamp(c.getTimeInMillis());
    }
 
    public static Date round(Date date, int field) {
        if (date == null) {
            throw new IllegalArgumentException(DATE_IS_NULL);
        }
        Calendar gval = Calendar.getInstance();
        gval.setTime(date);
        modify(gval, field, true);
        return gval.getTime();
    }
 
    public static Calendar round(Calendar date, int field) {
        if (date == null) {
            throw new IllegalArgumentException(DATE_IS_NULL);
        }
        Calendar rounded = (Calendar) date.clone();
        modify(rounded, field, true);
        return rounded;
    }
 
    public static Date round(Object date, int field) {
        if (date == null) {
            throw new IllegalArgumentException(DATE_IS_NULL);
        }
        if (date instanceof Date) {
            return round((Date) date, field);
        } else if (date instanceof Calendar) {
            return round((Calendar) date, field).getTime();
        } else {
            throw new ClassCastException("Could not round " + date);
        }
    }
 
    public static Date truncate(Date date, int field) {
        if (date == null) {
            throw new IllegalArgumentException(DATE_IS_NULL);
        }
        Calendar gval = Calendar.getInstance();
        gval.setTime(date);
        modify(gval, field, false);
        return gval.getTime();
    }
 
    public static Calendar truncate(Calendar date, int field) {
        if (date == null) {
            throw new IllegalArgumentException(DATE_IS_NULL);
        }
        Calendar truncated = (Calendar) date.clone();
        modify(truncated, field, false);
        return truncated;
    }
 
    public static Date truncate(Object date, int field) {
        if (date == null) {
            throw new IllegalArgumentException(DATE_IS_NULL);
        }
        if (date instanceof Date) {
            return truncate((Date) date, field);
        } else if (date instanceof Calendar) {
            return truncate((Calendar) date, field).getTime();
        } else {
            throw new ClassCastException("Could not truncate " + date);
        }
    }
 
    private static void modify(Calendar val, int field, boolean round) {
        if (val.get(Calendar.YEAR) > 280000000) {
            throw new ArithmeticException("Calendar value too large for accurate calculations");
        }
 
        if (field == Calendar.MILLISECOND) {
            return;
        }
        Date date = val.getTime();
        long time = date.getTime();
        boolean done = false;
 
        int millisecs = val.get(Calendar.MILLISECOND);
        if (!round || millisecs < 500) {
            time = time - millisecs;
            if (field == Calendar.SECOND) {
                done = true;
            }
        }
 
        int seconds = val.get(Calendar.SECOND);
        if (!done && (!round || seconds < 30)) {
            time = time - (seconds * 1000L);
            if (field == Calendar.MINUTE) {
                done = true;
            }
        }
        int minutes = val.get(Calendar.MINUTE);
        if (!done && (!round || minutes < 30)) {
            time = time - (minutes * 60000L);
        }
 
        if (date.getTime() != time) {
            date.setTime(time);
            val.setTime(date);
        }
        //子方法
        modifyChild(val, field, round);
        
        throw new IllegalArgumentException("The field " + field + " is not supported");
 
    }
 
    private static void modifyChild(Calendar val, int field, boolean round) {
        boolean roundUp = false;
        for (int i = 0; i < FIELDS.length; i++) {
            for (int j = 0; j < FIELDS[i].length; j++) {
                if (FIELDS[i][j] == field) {
                    if (round && roundUp) {
                        if (field == DateTimeUtils.SEMI_MONTH) {
                            if (val.get(Calendar.DATE) == 1) {
                                val.add(Calendar.DATE, 15);
                            } else {
                                val.add(Calendar.DATE, -15);
                                val.add(Calendar.MONTH, 1);
                            }
                        } else {
                            val.add(FIELDS[i][0], 1);
                        }
                    }
                    return;
                }
            }
            int offset = 0;
            boolean offsetSet = false;
 
            if (field == DateTimeUtils.SEMI_MONTH && FIELDS[i][0] == Calendar.DATE) {
                offset = val.get(Calendar.DATE) - 1;
                if (offset >= 15) {
                    offset -= 15;
                }
                roundUp = offset > 7;
                offsetSet = true;
            } else if (field == Calendar.AM_PM && FIELDS[i][0] == Calendar.HOUR_OF_DAY) {
                offset = val.get(Calendar.HOUR_OF_DAY);
                if (offset >= 12) {
                    offset -= 12;
                }
                roundUp = offset > 6;
                offsetSet = true;
            }
 
            if (!offsetSet) {
                int min = val.getActualMinimum(FIELDS[i][0]);
                int max = val.getActualMaximum(FIELDS[i][0]);
                offset = val.get(FIELDS[i][0]) - min;
                roundUp = offset > ((max - min) / 2);
            }
            if (offset != 0) {
                val.set(FIELDS[i][0], val.get(FIELDS[i][0]) - offset);
            }
        }
    }
 
    /**
     * 得到指定周的第一天(周日)00:00:00秒,返回对应的Timestamp,可以用作时间区间的计算
     *
     * @param cal
     *            Calendar对象,如果为null,则使用本地JVM中的Calendar对象
     * @param week
     * @return
     */
    public static Timestamp getFirstOfWeek(Calendar calen, int week) {
        Calendar cal = calen;
        if (cal == null) {
            cal = Calendar.getInstance();
        }
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return new Timestamp(cal.getTimeInMillis());
    }
 
    /**
     * 得到指定周的最后一天(周日)23:23:59秒,返回对应的Timestamp,可以用作时间区间的计算
     *
     * @param cal
     *            Calendar对象,如果为null,则使用本地JVM中的Calendar对象
     * @param week
     * @return
     */
    public static Timestamp getLastOfWeek(Calendar calen, int week) {
        Calendar cal = calen;
        if (cal == null) {
            cal = Calendar.getInstance();
        }
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return new Timestamp(cal.getTimeInMillis());
    }
 
    /**
     * 得到当前周在一年中的周数
     *
     * @param cal
     *            Calendar对象,如果为null,则使用本地JVM中的Calendar对象
     * @return
     */
    public static int getCurrentWeek() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.WEEK_OF_YEAR);
    }
 
    /**
     * 得到当前周在一年中的周数
     *
     * @param cal
     *            Calendar对象,如果为null,则使用本地JVM中的Calendar对象
     * @return
     */
    public static int getCurrentWeek(Calendar calen) {
        Calendar cal = calen;
        if (cal == null) {
            cal = Calendar.getInstance();
        }
        return cal.get(Calendar.WEEK_OF_YEAR);
    }
 
    public static int getCurrentWeek(Timestamp t1) {
        Calendar cal;
        if (t1 == null) {
            cal = Calendar.getInstance();
        } else {
            cal = new GregorianCalendar();
            cal.setTime(t1);
        }
        return cal.get(Calendar.WEEK_OF_YEAR);
    }
 
    /**
     * 得到指定Timestamp代表的日期第一年中第几周
     *
     * @param cal
     *            Calendar对象,如果为null,则使用本地JVM中的Calendar对象
     * @param ts
     *            指定Timestamp
     * @return 周数
     */
    public static int getWeekOfYear(Calendar cale, Timestamp ts) {
        Calendar cal =cale;
        if (cal == null) {
            cal = Calendar.getInstance();
        }
        cal.setTimeInMillis(ts.getTime());
        return cal.get(Calendar.WEEK_OF_YEAR);
    }
 
    /**
     * 得到当前年份
     *
     * @return 年
     */
    public static int getCurrentYear() {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        return calendar.get(Calendar.YEAR);
    }
 
    public static int getYear(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }
 
    /**
     * 得到当前月份
     *
     * @return 月份
     */
    public static int getCurrentMonth() {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        return calendar.get(Calendar.MONTH) + 1;
    }
 
    public static int getCurrentMonth(Timestamp t1) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(t1);
        return calendar.get(Calendar.MONTH) + 1;
    }
 
    public static int getCurrentMonth(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }
 
    /**
     * 获取指定时间的前后多少天的日期以分秒
     */
    public static Timestamp getTimestamp(Timestamp timestamp, int day, int hour, int minute) {
        Calendar calendar = new GregorianCalendar();
        if (timestamp != null) {
            calendar.setTimeInMillis(timestamp.getTime());
        } else {
            calendar = Calendar.getInstance();
        }
        calendar.add(Calendar.DATE, day);
        calendar.set(Calendar.HOUR, hour);
        calendar.set(Calendar.MINUTE, minute);
        return new Timestamp(calendar.getTimeInMillis());
    }
 
    /**
     * 给出两个日期,计算他们之间相差的年数|月数|天数
     *
     * @param c1
     *            日期1
     * @param c2
     *            日期2
     * @param what
     *            比较模式,如果是Calendar.YEAR则在年份上比较; 如果是Calendar.MONTH则在月数上比较; 如果是Calendar.DATE则在天数上比较(默认情形)
     * @return 相差的年数或月数或天数
     */
    public static int compare(Calendar c1, Calendar c2, int what) {
        int number = 0;
        switch (what) {
        case Calendar.YEAR:
            number = c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
            break;
        case Calendar.MONTH:
            int years = compare(c1, c2, Calendar.YEAR);
            number = 12 * years + (c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH));
            break;
        case Calendar.DATE:
            number = (int) ((c1.getTimeInMillis() - c2.getTimeInMillis()) / (1000 * 60 * 60 * 24));
            break;
        default:
            number = (int) ((c1.getTimeInMillis() - c2.getTimeInMillis()) / (1000 * 60 * 60 * 24));
            break;
        }
        return number;
    }
 
    /*
     * 得到指定日期的所在年份的最后一天
     */
    public static Timestamp getLastOfYear(Timestamp t1) {
        Calendar a = new GregorianCalendar();
        a.setTime(t1);
        a.set(Calendar.MONTH, 11);
        a.set(Calendar.DATE, 1);// 把日期设置为12月第一天
        a.roll(Calendar.DATE, -1);// 日期回滚一天,也就是当年最后一天
        return new Timestamp(a.getTimeInMillis());
    }
 
    /*
     * 得到指定日期的所在年份的第一天
     */
    public static Timestamp getFirstOfYear(Timestamp t1) {
        Calendar a = new GregorianCalendar();
        a.setTime(t1);
        a.set(Calendar.MONTH, 0);
        a.set(Calendar.DATE, 1);// 把日期设置为12月第一天
        return new Timestamp(a.getTimeInMillis());
    }
 
    /*
     * 得到指定日期的所在月份的最后一天
     */
    public static Timestamp getLastOfMonth(Timestamp t1) {
        Calendar a = new GregorianCalendar();
        a.setTime(t1);
        a.set(Calendar.DATE, 1);// 把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);// 日期回滚一天,也就是最后一天
        return new Timestamp(a.getTimeInMillis());
    }
 
    /**
     * 得到指定日期的所在月份的第一天
     *
     * @param t1
     * @return
     */
    public static Timestamp getFirstOfMonth(Timestamp t1) {
        Calendar a = new GregorianCalendar();
        a.setTime(t1);
        a.set(Calendar.DATE, 1);// 把日期设置为当月第一天
        return new Timestamp(a.getTimeInMillis());
    }
 
    /**
     * 给出两个日期,计算他们之间相差的年数|月数|天数
     *
     * @param c1
     *            日期1
     * @param c2
     *            日期2
     * @param what
     *            比较模式,如果是Calendar.YEAR则在年份上比较; 如果是Calendar.MONTH则在月数上比较; 如果是Calendar.DATE则在天数上比较(默认情形)
     * @return 相差的年数或月数或天数
     */
    public static int compare(Timestamp t1, Timestamp t2, int what) {
 
        Calendar c1 = Calendar.getInstance();
        c1.setTime(t1);
 
        Calendar c2 = Calendar.getInstance();
        c2.setTime(t2);
 
        return compare(c1, c2, what);
    }
 
    public static boolean after(Timestamp t1, Timestamp t2) {
        if (t1 == null || t2 == null) {
            return false;
        }
 
        return t1.after(t2);
    }
 
    public static boolean equals(Timestamp t1, Timestamp t2) {
        if (t1 == null && t2 == null) {
            return true;
        } else {
            if (t1 == null) {
                return t2.equals(t1);
            } else {
                return t1.equals(t2);
            }
        }
    }
 
    public static boolean before(Timestamp t1, Timestamp t2) {
        if (t1 == null || t2 == null) {
            return false;
        }
 
        return t1.before(t2);
    }
 
    /**
     * 把字符串的日期转成Timestamp
     *
     * @param time
     *            字符串的日期
     * @return int Timestamp型日期
     */
    public static Timestamp stringConvertTimestamp(String stime) {
        String time =stime;
        if (null == time || "".equals(time)) {
            return null;
        }
        if (time.length() == 10) {// yyyy-MM-dd
            time = time.concat(" 00:00:00.000000000");
        } else if (time.length() == 16) {// yyyy-MM-dd hh:mm
            time = time.concat(":00.000000000");
        } else if (time.length() == 19) {// yyyy-MM-dd hh:mm:dd
            time =time.concat(".000000000");
        }
        return Timestamp.valueOf(time);
    }
 
    /**
     * 把字符串的日期转成Timestamp
     *
     * @param date
     *            字符串的日期
     * @return int Timestamp型日期
     */
    public static Date stringConvertDate(String sdate) {
        String date =sdate;
        if (null == date || "".equals(date)) {
            return null;
        }
        if (date.length() == 10) {// yyyy-MM-dd
            date = date.concat(" 00:00:00");
        } 
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);
        Date objDate;
        try {
            objDate = formatter.parse(date);
        } catch (ParseException e) {
            objDate = null;
        }
        return objDate;
    }
 
    /**
     * 把字符串的日期转成Timestamp
     *
     * @param date
     *            字符串的日期
     * @return int Timestamp型日期
     */
    public static Timestamp stringConvertTS(String sdate) {
        String date =sdate;
        if (null == date || "".equals(date)) {
            return null;
        }
        if (date.length() == 10) {// yyyy-MM-dd
            date = date.concat(" 00:00:00");
        } 
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);
        Timestamp ts;
        try {
            Date objDate = formatter.parse(date);
 
            ts = new Timestamp(objDate.getTime());
        } catch (ParseException e) {
            ts = null;
        }
        return ts;
    }
 
 
    /**
     * 去除date的毫秒数
     *
     * @param date
     *            字符串的日期
     * @return int Timestamp型日期
     */
    public static Date getCurrDateTime(Date date) {
        return stringConvertTS(formatDate(date, ISO_DATETIME_FORMAT));
    }
    
    /**
     * 比较两日期大小
     *
     * @param DATE1
     * @param DATE2
     * @param what
     *            比较模式,如果是Calendar.YEAR则在年份上比较; 如果是Calendar.MONTH则在月数上比较; 如果是Calendar.DATE则在天数上比较(默认情形)
     * @return date1 在date2前=1;date1在date2后=-1;否则=0;
     * @throws ParseException
     */
    public static int compareDate(String date1, String date2, int what) throws ParseException {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA);
        Date objDate1 = df.parse(date1);
        Date objDate2 = df.parse(date2);
        return compareDate(objDate1, objDate2, what);
    }
 
    /**
     * 比较两日期大小
     *
     * @param DATE1
     * @param DATE2
     * @param what
     *            比较模式,如果是Calendar.YEAR则在年份上比较; 如果是Calendar.MONTH则在月数上比较; 如果是Calendar.DATE则在天数上比较(默认情形)
     * @return date1 在date2前=1;date1在date2后=-1;否则=0;
     */
    public static int compareDate(Date date1, Date date2, int what) {
        Calendar objCalendar1 = Calendar.getInstance();
        objCalendar1.setTime(date1);
        Calendar objCalendar2 = Calendar.getInstance();
        objCalendar2.setTime(date2);
        int iResult = compare(objCalendar1, objCalendar2, what);
        if (iResult > 0) {
            return 1;
        } else if (iResult < 0) {
            return -1;
        } else {
            return 0;
        }
    }
 
    /**
     * 和当前日期比较大小
     *
     * @param currentDate
     * @return currentDate 在当前日期前=1;date1在当前日期后=-1;否则=0;
     */
    public static int compareCurrentDate(Date currentDate) {
        return compareDate(currentDate, nowAsDate(), Calendar.DATE);
    }
 
    /**
     * 和当前日期比较大小
     *
     * @param currentDate
     * @return currentDate 在当前日期前=1;date1在当前日期后=-1;否则=0;
     * @throws ParseException
     */
    public static int compareCurrentDate(String currentDate) throws ParseException {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA);
        Date objDate = df.parse(currentDate);
        return compareDate(objDate, nowAsDate(), Calendar.DATE);
 
    }
 
    /**
     * 根据日期,输出日期对应的月份
     * 
     * @param date
     * @return month
     */
    public static int getMonth(Date date) {
        if (null == date) {
            throw new IllegalArgumentException("The date is null");
        }
 
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
 
        return calendar.get(Calendar.MONTH);
    }
 
    /**
     * 封装每月的最后一天
     * 
     * @param date
     * @author 伍升存
     * @history 2012-03-13 伍升存
     * @return
     */
    public static Date lastDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.roll(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }
 
    /**
     * 当前时间的前几个小时
     * 
     * @return
     */
    public static Date beforeHourToNowDate(int number) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - number);
        return calendar.getTime();
    }
 
    /**
     * 当前时间的后几个小时
     * 
     * @return
     */
    public static Date afterHourToNowDate(int number) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + number);
        return calendar.getTime();
    }
 
    // 计算时间
    public static String showTime(Date ctime, String sformat) {
        String format = sformat;
        String r = "";
        if (ctime == null) {
            return r;
        }
        if (format == null) {
            format = "yyyy-MM-dd HH:mm";
        }
        long nowtimelong = System.currentTimeMillis();
        long nowtime = nowtimelong;
        long ctimelong = ctime.getTime();
        long result = nowtime - ctimelong;
        if (result - (long) 60 * 1000 < (long) 0)// 一分钟内
        {
            BigDecimal seconds = cal(new BigDecimal(result), (long) 1000, 0);
            r = seconds + "秒钟前";
        } else if (result - (long) 60 * 1000 >= (long) 0 && result - (long) 60 * 60 * 1000 < (long) 0)// 一小时内
        {
            BigDecimal seconds = cal(new BigDecimal(result), (long) (60 * 1000), 0);
            r = seconds + "分钟前";
        } else if (result - (long) 60 * 60 * 1000 >= (long) 0 && result - (long) 24 * 60 * 60 * 1000 < (long) 0)// 一天内
        {
            BigDecimal seconds = cal(new BigDecimal(result), (long) (60 * 60 * 1000), 0);
            r = seconds + "小时前";
        } else if (result - (long) 24 * 60 * 60 * 1000 >= (long) 0 && result - (long) 30 * 24 * 60 * 60 * 1000 < (long) 0) {// 一个月内
            BigDecimal seconds = cal(new BigDecimal(result), (long) (24 * 60 * 60 * 1000), 0);
            r = seconds + "天前";
        } else if (result - (long) 30 * 24 * 60 * 60 * 1000 >= (long) 0 && result - (long) 12 * 30 * 24 * 60 * 60 * 1000 < (long) 0) {// 一年内
            BigDecimal seconds = cal(new BigDecimal(result), new BigDecimal(30).multiply(new BigDecimal(24)).multiply(new BigDecimal(60)).multiply(new BigDecimal(60)).multiply(new BigDecimal(1000)).longValue(), 0);
            r = seconds + "个月前";
        } else{// 日期格式
            SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.CHINA);
            r = sdf.format(ctime);
        }
        return r;
    }
 
    public static BigDecimal cal(BigDecimal result, long a, int digit) {
        return result.divide(new BigDecimal(a), digit, BigDecimal.ROUND_HALF_UP);
    }
 
    public static String transForm(Date beginTime, Date endTime) {
        Calendar cal = Calendar.getInstance();
        String trans = "";
        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日-HH:mm",Locale.CHINA);
        String str_begin = sdf.format(beginTime);
        String str_end = sdf.format(endTime);
        cal.setTime(beginTime);
        int begin_week = cal.get(Calendar.DAY_OF_WEEK);
        String str_begin_week = getWeek(begin_week);
 
        String str_begin_month = str_begin.substring(0, str_begin.indexOf('-'));
        String str_begin_hour = str_begin.substring(str_begin.indexOf('-') + 1);
 
        String str_end_month = str_end.substring(0, str_begin.indexOf('-'));
        String str_end_hour = str_end.substring(str_end.indexOf('-') + 1);
 
        if (str_begin_month.equals(str_end_month)) {
            trans = str_begin_month + " (" + str_begin_week + ") " + str_begin_hour + " 至  " + str_end_hour;
        } else {
            cal.setTime(endTime);
            int end_week = cal.get(Calendar.DAY_OF_WEEK);
            String str_end_week = getWeek(end_week);
            trans = str_begin_month + " (" + str_begin_week + ") " + str_begin_hour + " 至  " + str_end_month + " (" + str_end_week + ") " + str_end_hour;
        }
        return trans;
    }
 
    public static String getWeek(int week) {
        String str_week = "";
        if (week == 2) {
            str_week = "周一";
        }
        if (week == 3) {
            str_week = "周二";
        }
        if (week == 4) {
            str_week = "周三";
        }
        if (week == 5) {
            str_week = "周四";
        }
        if (week == 6) {
            str_week = "周五";
        }
        if (week == 7) {
            str_week = "周六";
        }
        if (week == 1) {
            str_week = "周日";
        }
        return str_week;
 
    }
}