xiaoyong931011
2022-02-25 89b7fb1d316cfce7eb98a27c8d668da493933f7f
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
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
package com.xcong.excoin.modules.blackchain.service;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.DecimalFormat;
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.TimeZone;
import java.util.concurrent.TimeUnit;
 
import org.apache.commons.lang.StringUtils;
public class DateUtil {
 
    public DateUtil() {
 
    }
    /**
     * 测试用
     * @param args
     * @throws ParseException 
     */
    public static void main(String[] args) throws ParseException {
//        long time = System.currentTimeMillis()+7200000;
//        Date a = new Date(time);
//        long space = dateSpaceHours(new Date(), a);
//        System.out.println(space);
//        System.out.println(DateUtil.dateToString(DateUtil.getStringToDate(DateUtil.getSQLDateYMD())));
        
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        Date date= DateUtil.addDays(new Date(), -1);
//        System.out.println(date);
//        System.out.println(DateUtil.dateToString(date));
//        System.out.println(simpleDateFormat.format(date));
//        System.out.println(DateUtil.getStringToDate(dateToStringYMD(date),"yyyy-MM-dd"));
//        System.out.println(DateUtil.getStringToDate(null));
        //for(int i=0;i<10;i++){
        //System.out.println(getSQLDateYMD());
        //}
        //System.out.println(dateDiff("2013-06-08 12:25:23","2013-07-08 12:25:23"));
        //        System.out.println(currentTimeDiff2("2013-12-08"));
        //        System.out.println(compare_date("2013-07-08 12:25:23", "2013-07-08 12:23:23"));
        //System.out.println(fomatDate("2013-10-16 9:27:34"));
        //System.out.println(System.currentTimeMillis());
        //System.out.println(getTenTimestamp());
        //System.out.println(getSixRandom());
        //        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前时间
        //        Date time = Calendar.getInstance().getTime();
 
        //        System.out.println(simpleDateFormat.format(datePlusOrMinus("2013-10-16 9:27:34",2)));
 
        //        System.out.println(dateToString(datePlusOrMinus(getSQLDate(), 2)));
 
        //        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //        String strDate1 = "2012-3-15";
        //        String strDate2 = "2013-5-13";
        //        Date date1=sdf.parse(strDate1);
        //        Date date2=sdf.parse(strDate2);
        //        //        
        //        //        System.out.println(getMonths(date2,date1));
        //        System.out.println(daysBetween(date2, date1));
        //        System.out.println(fomatDate("2014-04"));
        //        System.out.println(fomatDate("2014-05"));
        //        System.out.println(Double.parseDouble(fomatDate("2014-05")) > Double.parseDouble(fomatDate("2014-04")));
        //        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        //        String date1 = "2014-04";
        //        String date2 = "2015-08";
        //        Date start = sdf.parse(date1);
        //        Date end = sdf.parse(date2);
        //        
        //        
        //        System.out.println("###start:==" + start);
        //        System.out.println("###end:==" + end);
 
        //        System.out.println("月份="+myTimeStr(9615000));
        //        System.out.println(fomatDateToNumber("2014-07-28 14:12:14"));
        //System.out.println(compareDate("2014-08-13","2014-08-12"));
        //        System.out.println(getNineRandom());
        //        System.out.println("222="+dateDiffStr("2014-06-11 00:00:00","2014-06-25 23:59:59"));
//        System.out.println("getUTCTimeStr()=="+DateUtil.currentTimeDiffToHour("2015-01-22 18:12:59"));
        //System.out.println(DateUtil.dateDiff("2015-09-02 09:55:14",DateUtil.dateToString(new Date())));
        System.out.println(DateUtil.currentTimeDiff("2018-07-11 18:00:00"));
    }
    
    /**
     * 计算今天的剩余秒数
     * @return
     */
    public static int getTodaySeconds(){
        Calendar cal = Calendar.getInstance();
        long start = TimeUnit.MILLISECONDS.toSeconds(cal.getTimeInMillis());
        cal.add(Calendar.DAY_OF_YEAR, 1);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        long end = TimeUnit.MILLISECONDS.toSeconds(cal.getTimeInMillis());
        return (int) (end - start);
    }
    
    /**
     * 获取当前分钟
     * @return
     */
    public static Integer getNowDateMin(){
        Calendar c1 = Calendar.getInstance();
        c1.setTime(new Date());
        return c1.get(Calendar.MINUTE);
    }
    /**
     * 获取当前day
     * @return
     */
    public static Integer getNowDateDay(){
        Calendar c1 = Calendar.getInstance();
        c1.setTime(new Date());
        return c1.get(Calendar.DATE);
    }
 
    /**
     * 获取当前秒
     * @return
     */
    public static Integer getNowDateSecond(){
        Calendar c1 = Calendar.getInstance();
        c1.setTime(new Date());
        return c1.get(Calendar.SECOND);
    }
    /**
     * 返回两个日期之间相差天数---只比较天数
     * @param fDate
     * @param oDate
     * @return
     */
    public static int daysOfTwo(Date fDate, Date oDate) {
 
           Calendar aCalendar = Calendar.getInstance();
 
           aCalendar.setTime(fDate);
 
           int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
 
           aCalendar.setTime(oDate);
 
           int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
 
           return Math.abs(day2 - day1);
 
    }
    
    /**
     * 把日期格式转换为字符串格式HH:mm:ss
     * @param date
     * @return
     */
    public static String dateToStringHMS (Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        return simpleDateFormat.format(date);
    }
    /**
     * 把日期格式转换为字符串格式HH:mm
     * @param date
     * @return
     */
    public static String dateToStringHM (Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        return simpleDateFormat.format(date);
    }
    /**
     * 把日期格式转换为字符串格式HHmm
     * @param date
     * @return
     */
    public static int dateToIntHM (Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HHmm");
        return Integer.parseInt(simpleDateFormat.format(date));
    }
    /**
     * 指定格式日期往后加多少分钟
     * @param format
     * @param StrDate
     * @param min
     * @return
     */
    @SuppressWarnings("static-access")
    public static String addMin(String format,String StrDate,int min){
            Calendar   cal = Calendar.getInstance(); 
            SimpleDateFormat sFmt = new SimpleDateFormat(format); 
            cal.setTime(sFmt.parse( (StrDate), new ParsePosition(0))); 
 
            if (min != 0) { 
                cal.add(cal.MINUTE,min); 
            } 
            return sFmt.format(cal.getTime()); 
    }
    /**
     * 指定格式日期往后加多少分钟
     * @param date
     * @param min
     * @return
     */
    public static Date addMin(Date date,int min){
            Calendar   cal = Calendar.getInstance(); 
            cal.setTime(date); 
            if (min != 0) { 
                cal.add(Calendar.MINUTE,min); 
            } 
            return cal.getTime(); 
    }
    /**
     * HH:mm:ss上加秒
     * @param date
     * @param seconds
     * @return
     */
    public static String addSecond(Date date, int seconds) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, seconds);
        date =calendar.getTime();
        return dateToStringHMS(date);
    }
 
    /**
     * 产生2位
     */
    public static String getThreeRandom(){
        int random1 = (int) (Math.random() * 900 + 100);
        String random = String.valueOf(random1);
        return random;
    }
    
    /**
     * 把日期格式转换为字符串格式yyyy-MM-dd
     * @param
     * @return
     */
    public static String dateToStringYMD (){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(new Date());
    }
    /**
     * 把日期格式转换为字符串格式yyyy-MM-dd
     * @param
     * @return
     */
    public static String dateToStringYMD (Date data){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(data);
    }
    
    /**
       * String类型时间转为Timestamp,str与format必须格式一致
       * 
        * @param str
       * @param
       * @return
     */
    public static Timestamp str2Timestamp(String str) {
          Timestamp ts = null;
          try {
             if (null != str && !"".equals(str)) {
                DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
                ts = new Timestamp(sdf.parse(str).getTime());
             }
          } catch (Exception e) {
             // TODO: handle exception
             e.printStackTrace();
          }
          return ts;
     }
 
    /**
       * Date类型时间转为String格式,format为需要返回的格式
       * 
       * @param
       * @param
       * @return
    */
    public static String timeStamp2Str(Date dateTime) {
        /*SimpleDateFormat oldFormat  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        try {
            date = oldFormat.parse(dateTime);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return sdf.format(dateTime);
    }
       
    /**
     * 获取一个月的天数
     * @param year
     * @param month
     * @return
     */
    public static int dateToString (String year,String month){
        Calendar c= Calendar.getInstance();
             c.set(Calendar.YEAR, Integer.parseInt(year));
             c.set(Calendar.MONTH, Integer.parseInt(month)+1);
        return c.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
    
    /**
     * 把日期格式转换为字符串格式
     * @param date
     * @return
     */
    public static String dateToString (Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }
    /**
     * 把日期格式转换为字符串格式
     * @param date
     * @return
     */
    public static String dateToString (Date date,String dateFormat){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
        return simpleDateFormat.format(date);
    }
 
    /**
     * 把日期格式转换为YYMMDDHHMMSS格式
     * @param
     * @return
     */
    public static String dateToNumber (String time){
        SimpleDateFormat oldFormat  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat newFormat  = new SimpleDateFormat("yyMMddHHmmss");
        Date date = new Date();
        try {
            date = oldFormat.parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return newFormat.format(date);
    }
 
    /**
     * 根据有效期格式化时间
     * @param type  1每年 2 每月 3 每天 4 每小时 5 每分钟
     * @param time  yyMMddHHmmss
     * @return
     */
    public static String termvalidityFormat(int type,String time){
        String retime = null;
        try {
 
            if(type == 1){
                retime = "00"+time.substring(2, time.length());
            }else if(type == 2){
                retime = "0000"+time.substring(4, time.length());
            }else if(type == 3){
                retime = "000000"+time.substring(6, time.length());
            }else if(type == 4){
                retime = "00000000"+time.substring(8, time.length());
            }else if(type == 5){
                retime = "0000000000"+time.substring(10, time.length());
            }else {
                retime = time;
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retime;
 
    }
 
    /**
     * 把原始字符串扩展成自定的长度
     * @param oriStr 原始字符串
     * @param fillChar 要填充的字符
     * @param setLength 要扩展成的长度
     * @param preFill 前扩充还是后扩充 true-在前面填充,false-后面填充
     * @return
     */
    public static String fixStringToSetLength(String oriStr, String fillChar, int setLength, boolean preFill) {
        if(oriStr == null || fillChar.equals("")) {
            return oriStr;
        }
 
        int oriLength = oriStr.length();
        StringBuilder sb = null;
        if(preFill == true) {
            sb = new StringBuilder();
            for(int i = oriLength; i < setLength; i++) {
                sb.append(fillChar);
            }
            sb.append(oriStr);
        } else {
            sb = new StringBuilder(oriStr);
            for(int i = oriLength; i < setLength; i++) {
                sb.append(fillChar);
            }
        }
 
        return sb.toString();
    }
 
 
 
    //日期往后加多少个月,多少天,多少年
    @SuppressWarnings("static-access")
    public static String addMonth(String format,String StrDate,int year,int month,int day){
 
        Calendar   cal = Calendar.getInstance(); 
        SimpleDateFormat sFmt = new SimpleDateFormat(format); 
        cal.setTime(sFmt.parse( (StrDate), new ParsePosition(0))); 
 
        if (day != 0) { 
            cal.add(cal.DATE,day); 
        } 
        if (month != 0) { 
            cal.add(cal.MONTH, month); 
        } 
        if (year != 0) { 
            cal.add(cal.YEAR, year); 
        } 
        return sFmt.format(cal.getTime()); 
    }
 
    /**
     * 在当前时间增加或减少N个小時
     * @param
     * @param month
     * @return
     */
    public static Date addHour(int hour){
        Calendar   cal = Calendar.getInstance();
        cal.add( Calendar.HOUR, hour);
        return cal.getTime();
    }
    
    /**
     * 在当前时间增加或减少N个月
     * @param
     * @param month
     * @return
     */
    public static Date addMonth(int month){
        Calendar   cal = Calendar.getInstance();
        cal.add( Calendar.MONTH, month);
        return cal.getTime();
    }
    
    /**
     * 增加days天
     * @param date
     * @param days
     * @return
     */
    public static Date addDays(Date date ,int days){
        Calendar   cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, days);
        return cal.getTime();
    }
    /**
     * 判断两个时间字符串之差
     * @param date1
     * @param date2
     * @return 毫秒数
     */
    public static Long dateDiff(String date1,String date2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return df.parse(date1).getTime() - df.parse(date2).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 判断两个指定格式时间字符串之差
     * @param format
     * @param date1
     * @param date2
     * @return 毫秒数
     */
    public static Long dateDiff(String format,String date1,String date2) {
        DateFormat df = new SimpleDateFormat(format);
        try {
            return df.parse(date1).getTime() - df.parse(date2).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 计算时间差,相差多少天、小时、分、秒
     * @param stime
     * @param etime
     * @return
     */
    public static String dateDiffStr(String stime,String etime){
        String diff = "";
        try {
            SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            java.util.Date begin=dfs.parse(stime);
            java.util.Date end = dfs.parse(etime);
            long between=(end.getTime()-begin.getTime())/1000;//除以1000是为了转换成秒
            long day1=between/(24*3600);
            long hour1=between%(24*3600)/3600;
            long minute1=between%3600/60;
            long second1=between%60;
            System.out.println(""+day1+"天"+hour1+"小时"+minute1+"分"+second1+"秒");
            if(day1>0){
                diff = ""+day1+"天"+hour1+"小时"+minute1+"分"+second1+"秒";
                return diff;
            }
 
            if(hour1>0){
                diff = ""+hour1+"小时"+minute1+"分"+second1+"秒";
                return diff;
            }
 
            if(minute1>0){
                diff = ""+minute1+"分"+second1+"秒";
                return diff;
            }
 
            if(second1>=0){
                diff = ""+second1+"秒";
                return diff;
            }
 
            return diff;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return diff;
 
    }
 
    /**
     * 和当前时间差 
     * @param date
     * @return 当前时间 - date 毫秒数
     */
    public static Long currentTimeDiff(String date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return System.currentTimeMillis() -  df.parse(date).getTime();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 和当前时间差 
     * @param date
     * @return 当前时间 - date 返回相差小时
     */
    public static int currentTimeDiffToHour(String date) {
        if(date == null || date.equals("")){
            return 25;
        }
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Long longss = System.currentTimeMillis() -  df.parse(date).getTime();
            int hour = Integer.parseInt(longss/(1000*3600)+"");
            return hour;
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 25;
    }
 
    /**
     * 和当前时间差 分钟
     * @param date
     * @return 当前时间 - 返回分钟
     */
    public static int currentTimeDiffToMin(String date) {
        if(date == null || date.equals("")){
            return 25;
        }
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Long longss = System.currentTimeMillis() -  df.parse(date).getTime();
            int hour = Integer.parseInt(longss/(1000*60)+"");
            return hour;
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 0;
    }
    /**
     * 和当前时间差(不需要时分秒) 
     * @param date
     * @return 当前时间 - date 
     * 
     *      */
    public static Long currentTimeDiff2(String date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return System.currentTimeMillis() -  df.parse(date).getTime();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
 
 
    /**
     * 日期比较
     * @param DATE1
     * @param DATE2
     * @return DATE1 > DATE2 返回1 
     */
    public static int compare_date(String DATE1, String DATE2) {
 
 
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }
    
    
    /**
     * 日期比较
     * @param DATE1
     * @param DATE2
     * @return DATE1 > DATE2 返回1 
     */
    public static int compareDate(String DATE1, String DATE2) {
 
        if(DATE1 == null || DATE1.equals("")){
            return -1;
        }
        if(DATE2 == null || DATE2.equals("")){
            return 1;
        }
        if(DATE1.equals(DATE2)){
            return 0;
        }
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }
    
 
    /**
     * 日期比较
     * @param DATE1
     * @param DATE2
     * @return DATE1 > DATE2 返回1 
     */
    public static int compareDate(String DATE1, String DATE2,String formate) {
 
        if(DATE1 == null || DATE1.equals("")){
            return -1;
        }
        if(DATE2 == null || DATE2.equals("")){
            return 1;
        }
        if(DATE1.equals(DATE2)){
            return 0;
        }
        DateFormat df = new SimpleDateFormat(formate);
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }
 
 
    /**
     * 将日期yyyy-MM-dd HH:mm:ss 格式化为 yyyyMMddHHmmss
     * @param date
     * @return
     */
    public static String fomatDate(String date){
        String dateStr = null;
        if(date == null || "".equals(date)){
            dateStr = "";
        }else{
            try {
                dateStr = date.replaceAll(":", "").replaceAll("-", "").replaceAll(" ", "");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
 
        return dateStr;
    }
 
    /**
     * 获取插入数据库格式的时间
     * @return
     */
    public static String getSQLDate() {
        String systemdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        .format(Calendar.getInstance().getTime()); // 获取系统当前时间
        return systemdate;
    }
 
    /**
     * 字符串转日期 yyyy-MM-dd HH:mm:ss
     * @param date
     * @return
     */
    public static Date getStringToDate(String date){
        if (StringUtils.isEmpty(date)) {
            return null;
        }
        Date date2 = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date2 = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date2;
    }
    /**
     * 字符串转日期 yyyy-MM-dd HH:mm:ss
     * @param date
     * @return
     */
    public static Date getStringToDate(String date,String format){
        Date date2 = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        try {
            date2 = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date2;
    }
    
    /**
     * 获取插入数据库格式的时间yyyy-MM-dd
     * @return
     */
    public static String getSQLDateYMD() {
        String systemdate = new SimpleDateFormat("yyyy-MM-dd")
        .format(Calendar.getInstance().getTime()); // 获取系统当前时间
        return systemdate;
    }
 
    public static Integer getSQLDateMonth() {
        String systemdate = new SimpleDateFormat("MM")
        .format(Calendar.getInstance().getTime()); // 获取系统当前时间
        return Integer.parseInt(systemdate);
    }
 
    /**
     * 对月份进行加减
     * @param i
     * @return
     * @throws ParseException 
     */
    public static Date datePlusOrMinus(String systime ,Integer i) throws ParseException{
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date systDate =df.parse(systime);
        GregorianCalendar gc =new GregorianCalendar();
        gc.setTime(systDate);
        gc.add(2,i);
        return gc.getTime();
    }
 
    /**
     * 得到月日时分秒10位时间戳字符串
     */
    public static String getTenTimestamp() {
        String systemdate = new SimpleDateFormat("MMddHHmmss").format(Calendar.getInstance().getTime());
        return systemdate;
    }
 
 
    /**
     * 得到年月日
     */
    public static String getDateTime() {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        String systemdate = sdf.format(Calendar.getInstance().getTime());
        return systemdate.substring(0, systemdate.length());
    }
 
    /**
     * 得到16位时间戳字符串,前面14位年月日时分秒,第15位毫秒,第16位随机数
     */
    public static String getUniqueTimestamp() {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String systemdate = sdf.format(Calendar.getInstance().getTime());
        return systemdate.substring(0, systemdate.length()-2)+getOneRandom();
    }
 
    /**
     * 产生6位随机数
     */
    public static String getSixRandom(){
        int random1 = (int) (Math.random() * 900000 + 100000);
        String random = String.valueOf(random1);
        return random;
    }
    
    /**
     * 产生4位随机数
     */
    public static String getFourRandom(){
        int random1 = (int) (Math.random() * 9000 + 1000);
        String random = String.valueOf(random1);
        return random;
    }
    /**
     * 产生2位随机数
     */
    public static String getTwoRandom(){
        int random1 = (int) (Math.random() * 90 + 10);
        String random = String.valueOf(random1);
        return random;
    }
    /**
     * 产生1位随机数
     */
    public static String getOneRandom(){
        int random1 = (int) (Math.random() * 9 + 1);
        String random = String.valueOf(random1);
        return random;
    }
 
    /**
     * 产生2位小于60的随机数
     */
    public static String getTwoRandomSixth(){
        int random1 = (int) (Math.random() * 90 + 10);
        String random = String.valueOf(random1);
        return random;
    }
    /**
     * 产生-10——10之间的数字
     * @return
     */
    public static Integer randomZF(){
        int a=(int)(Math.random()*2+1);
        //System.out.println(a);
        int aa=(int)(Math.pow(-1, a));
        //System.out.println(aa);
        int aaa=(int)(Math.random()*10+1);
        int num=aa*aaa;
        //System.out.println(num);
        return num;
    }
 
    /**
     * 计算两个日期之间相差的月数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int getMonths(Date date1, Date date2) {
        int iMonth = 0;
        int flag = 0;
        try {
            Calendar objCalendarDate1 = Calendar.getInstance();
            objCalendarDate1.setTime(date1);
 
            Calendar objCalendarDate2 = Calendar.getInstance();
            objCalendarDate2.setTime(date2);
 
            if (objCalendarDate2.equals(objCalendarDate1))
                return 0;
            if (objCalendarDate1.after(objCalendarDate2)) {
                Calendar temp = objCalendarDate1;
                objCalendarDate1 = objCalendarDate2;
                objCalendarDate2 = temp;
            }
            if (objCalendarDate2.get(Calendar.DAY_OF_MONTH) < objCalendarDate1
                    .get(Calendar.DAY_OF_MONTH))
                flag = 1;
 
            if (objCalendarDate2.get(Calendar.YEAR) > objCalendarDate1
                    .get(Calendar.YEAR))
                iMonth = ((objCalendarDate2.get(Calendar.YEAR) - objCalendarDate1
                        .get(Calendar.YEAR))
                        * 12 + objCalendarDate2.get(Calendar.MONTH) - flag)
                        - objCalendarDate1.get(Calendar.MONTH);
            else
                iMonth = objCalendarDate2.get(Calendar.MONTH)
                - objCalendarDate1.get(Calendar.MONTH) - flag;
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return iMonth;
    }
 
    /**
     * 计算两个日期之间相差的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static final int daysBetween(Date date1, Date date2) { 
 
        java.util.Calendar time1 = java.util.Calendar.getInstance();   
        java.util.Calendar time2 = java.util.Calendar.getInstance();  
        time1.setTime(date1);
        time2.setTime(date2);
        int days = 0;
        if(time1.getTime().getTime() >= time2.getTime().getTime()){
            days = ((int) (time1.getTime().getTime() / 1000) - (int) (time2.getTime().getTime() / 1000)) / 3600 / 24;   
        }else{
            days = ((int) (time2.getTime().getTime() / 1000) - (int) (time1.getTime().getTime() / 1000)) / 3600 / 24;   
        }
        return days;   
    } 
 
    /**
     * 计算两个日期之间相差秒
     *
     * @param sdate1
     * @param sdate2
     * @return
     */
    public static final int secBetween(String sdate1, String sdate2) { 
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
        Date date1 = null;
        try {
            date1 = df.parse(sdate1);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Date date2 = null;
        try {
            date2 = df.parse(sdate2);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
        java.util.Calendar time1 = java.util.Calendar.getInstance();   
        java.util.Calendar time2 = java.util.Calendar.getInstance();  
        time1.setTime(date1);
        time2.setTime(date2);
 
        int sec = 0;
        if(time1.getTime().getTime() >= time2.getTime().getTime()){
            sec = ((int) (time1.getTime().getTime() / 1000) - (int) (time2.getTime().getTime() / 1000));   
        }else{
            sec = ((int) (time2.getTime().getTime() / 1000) - (int) (time1.getTime().getTime() / 1000));   
        }
        return sec;   
    } 
 
    /**
     * 
     * @param date1 <String>
     * @param date2 <String>
     * @return int
     * @throws ParseException
     */
    public static int getMonthSpace(String date1, String date2)
    throws ParseException {
 
        int result = 0;
 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
 
        c1.setTime(sdf.parse(date1));
        c2.setTime(sdf.parse(date2));
 
        result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
 
        return result == 0 ? 1 : Math.abs(result);
 
    }
 
    /**
     * 根据毫秒数计算时分秒
     * @param timeMillis
     * @return
     */
    public static String myTimeStr(long timeMillis) {  
        int timezone = 8;  
        long totalSeconds = timeMillis / 1000;  
        totalSeconds += 60 * 60 * timezone;  
        int second = (int)(totalSeconds % 60);// 秒  
        long totalMinutes = totalSeconds / 60;  
        int minute = (int)(totalMinutes % 60);// 分  
        long totalHours = totalMinutes / 60;  
        int hour = (int)(totalHours % 24);// 时  
        int totalDays = (int)(totalHours / 24);  
 
        int _year = 1970;  
 
        int year = _year + totalDays / 366;  
        int month = 1;  
        int day = 1;  
 
        int diffDays;  
        boolean leapYear;  
        while (true) {  
            int diff = (year - _year) * 365;  
            diff += (year - 1) / 4 - (_year - 1) / 4;  
            diff -= ((year - 1) / 100 - (_year - 1) / 100);  
            diff += (year - 1) / 400 - (_year - 1) / 400;  
 
            diffDays = totalDays - diff;  
 
            leapYear = (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);  
            if (!leapYear && diffDays < 365 || leapYear && diffDays < 366) {  
                break;  
            } else {  
                year++;  
            }  
        }  
 
        int[] monthDays;  
        if (diffDays >= 59 && leapYear) {  
            monthDays = new int[]{-1,0,31,60,91,121,152,182,213, 244, 274, 305, 335 };  
        } else {  
            monthDays = new int[]{-1,0,31,59,90,120,151,181,212, 243, 273, 304, 334 };  
        }  
        for (int i = monthDays.length - 1; i >= 1; i--) {  
            if (diffDays >= monthDays[i]) {  
                month = i;  
                day = diffDays - monthDays[i] + 1;  
                break;  
            }  
        }  
 
        return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;  
 
    }  
    /**
     * 根据秒数计算小时
     * @param
     * @return
     */
    public static String secondToHour(String second) {  
        if(second == null || "".equals(second) || "null".equals(second)){
            return null;
        }
        Double sec = Double.parseDouble(second);
        Double hour = sec/3600;
        DecimalFormat df=new DecimalFormat("0.0");  
        return df.format(hour).toString();  
    }
    /**
     * 根据秒数计算分钟
     * @param
     * @return
     */
    public static String secondToMinute(String second) {  
        if(second == null || "".equals(second) || "null".equals(second)){
            return null;
        }
        Double sec = Double.parseDouble(second);
        Double minute = sec/60;
        DecimalFormat df=new DecimalFormat("0.0");  
        return df.format(minute).toString();  
    }
    /**
     * 根据小时数计算秒
     * @param
     * @return
     */
    public static String hourToSecond(String hour) {  
        if(hour == null || "".equals(hour) || "null".equals(hour)){
            return null;
        }
        DecimalFormat df=new DecimalFormat("0"); 
        return df.format(Double.parseDouble(hour)*3600).toString();
    }
    /**
     * 根据分钟计算秒
     * @param
     * @return
     */
    public static String MinuteToSecond(String minute) {  
        if(minute == null || "".equals(minute) || "null".equals(minute)){
            return null;
        }
        DecimalFormat df=new DecimalFormat("0"); 
        return df.format(Double.parseDouble(minute)*60).toString();
    }
 
    /**
     * 将日期yyyy-MM-dd HH:mm:ss 格式化为 yyMMddHHmmss
     * @param date
     * @return
     */
    public static String fomatDateToNumber(String date){
        String dateStr = null;
        if(date == null || "".equals(date)){
            dateStr = "";
        }else{
            try {
                dateStr = date.replaceAll(":", "").replaceAll("-", "").replaceAll(" ", "");
                dateStr = dateStr.substring(2);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
 
        return dateStr;
    }
 
    /**
     * 把YYMMDDHHMMSS格式转换为date格式
     * @param
     * @return
     */
    public static String numberToDate (String number){
        SimpleDateFormat oldFormat  = new SimpleDateFormat("yyMMddHHmmss");
        SimpleDateFormat newFormat  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        try {
            date = oldFormat.parse(number);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        return newFormat.format(date);
    }
 
    /**
     * 产生8位HHmmss
     */
    public static String getEightRandom(){
        String systemdate = new SimpleDateFormat("HHmmss")
        .format(Calendar.getInstance().getTime()); // 获取系统当前时间
        int random1 = (int) (Math.random() * 90 + 10);
        String random = String.valueOf(random1);
        return systemdate + random;
    }
 
    /**
     * 时间戳转换成时间
     * @param time
     * @return
     */
    public static String TimeStampToDateTime(long time){
        String datetime = null;
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
            datetime = sdf.format(new Date(time*1000)); 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return datetime;
    }
    
 
    /**
     * 时间转成时间戳 毫秒
     * @param datetime
     * @return
     */
    public static long DateTimeToTimeStampSS(String datetime){
         try {
             SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
             Date date=simpleDateFormat.parse(datetime);
             long timeStemp = date.getTime();
             return timeStemp;
        } catch (Exception e) {
            e.printStackTrace();
            return 0L ;
        }
        
    }
    
    /**
     * 时间转成时间戳 秒
     * @param datetime
     * @return
     */
    public static long DateTimeToTimeStampS(String datetime){
         try {
             SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
             Date date=simpleDateFormat.parse(datetime);
             long timeStemp = date.getTime()/1000;
             return timeStemp;
        } catch (Exception e) {
            e.printStackTrace();
            return System.currentTimeMillis()/1000 ;
        }
        
    }
    
    /**
     * 转成时间戳
     * @param datetime
     * @return
     */
    public static long NumberTimeToTimeStamp(String datetime){
        try {
            SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyMMddHHmmss");
            Date date=simpleDateFormat.parse(datetime);
            long timeStemp = date.getTime()/1000;
            return timeStemp;
        } catch (Exception e) {
            e.printStackTrace();
            return System.currentTimeMillis()/1000 ;
        }
        
    }
 
    /** 
     * 得到UTC时间,类型为字符串,格式为"yyyy-MM-dd HH:mm"<br /> 
     * 如果获取失败,返回null 
     * @return 
     */  
    public static String getUTCTimeStr() {
        SimpleDateFormat foo = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        System.out.println("foo:"+foo.format(new Date()));
 
        Calendar gc = GregorianCalendar.getInstance();
        System.out.println("gc.getTime():"+gc.getTime());
        System.out.println("gc.getTimeInMillis():"+new Date(gc.getTimeInMillis()));
 
        //当前系统默认时区的时间:
        Calendar calendar=new GregorianCalendar();
        System.out.print("时区:"+calendar.getTimeZone().getID()+"  ");
        System.out.println("时间:"+calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE));
        //美国洛杉矶时区
        TimeZone tz=TimeZone.getTimeZone("America/Los_Angeles");
        //时区转换
        calendar.setTimeZone(tz);
        System.out.print("时区:"+calendar.getTimeZone().getID()+"  ");
        System.out.println("时间:"+calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE));
 
        //1、取得本地时间:
        java.util.Calendar cal = java.util.Calendar.getInstance();
 
        //2、取得时间偏移量:
        int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
 
        //3、取得夏令时差:
        int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
 
        //4、从本地时间里扣除这些差量,即可以取得UTC时间:
        cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
 
        //之后调用cal.get(int x)或cal.getTimeInMillis()方法所取得的时间即是UTC标准时间。
        System.out.println("UTC:"+new Date(cal.getTimeInMillis()));
 
        Calendar calendar1 = Calendar.getInstance();
        TimeZone tztz = TimeZone.getTimeZone("GMT");       
        calendar1.setTimeZone(tztz);
        System.out.println(calendar.getTime());
        System.out.println("9999:"+calendar.getTimeInMillis()/1000);
 
        System.out.println("utc1="+System.currentTimeMillis()/1000);
        System.out.println("utc="+new Date().getTime()/1000);
 
 
        System.out.println(TimeStampToDateTime(1421724351));
        System.out.println("=="+DateTimeToTimeStampS("2015-01-20 11:35:06"));
        System.out.println(TimeStampToDateTime(DateTimeToTimeStampS("2015-01-20 11:35:06")));
        return null;
    }  
 
    /** 
     * 获得指定日期的前/后n天,小时
     *  
     * @param specifiedDay 
     * @return 
     */  
    public static String getSpecifiedDayAbove(String specifiedDay,int n,int h) {  
        Calendar c = Calendar.getInstance();  
        Date date = null;  
        try {  
            date = new SimpleDateFormat("yy-MM-dd HH:mm:ss").parse(specifiedDay);  
        } catch (ParseException e) {  
            e.printStackTrace();  
        }  
        c.setTime(date);  
        int day = c.get(Calendar.DATE);  
        c.set(Calendar.DATE, day + n);  
        int hour = c.get(Calendar.HOUR);
        c.set(Calendar.HOUR,hour + h);
  
        String dayAfter = new SimpleDateFormat("yy-MM-dd HH:mm:ss")  
                .format(c.getTime());  
        return dayAfter;  
    } 
    
    /**
     * 两个时间的小时差
     * @param st
     * @param ed
     * @return
     */
    public static long dateSpaceHours(Date st,Date ed){
        return (long) Math.ceil((ed.getTime() - st.getTime())/3600000.0);
    }
    
    public static String formatDate(Date date,  String format) throws ParseException {
        DateFormat df = new SimpleDateFormat(format);
        df.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        return df.format(date);
    }
    public static Date parseDate(String date,  String format) {
        DateFormat df = new SimpleDateFormat(format);
        df.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        try {
            return df.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}