zhanghua
2023-03-20 2d1d03a0d5c8abbe721524d39f7772633064577b
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
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
package com.ycl.utils.common;
 
import com.ycl.api.CommonResult;
import com.ycl.exception.ApiException;
import io.netty.util.internal.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.prefs.BackingStoreException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * 时间工具
 */
public final class DateUtil {
 
    private static final Logger LOGGER = LoggerFactory.getLogger(DateUtil.class);
 
    /**
     * 日期时分秒
     */
 
    public static final String SF = "\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{2}:\\d{2}:\\d{2}";
 
    /**
     * 日期时分
     */
    public static final String SFM = "\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{2}:\\d{2}";
 
    //ThreadDateUtil
    /**
     * yyyy-MM-dd
     */
    public static final String sf1 = "yyyy-MM-dd";                                     // new SimpleDateFormat("");
    /**
     * yyyy-MM-dd HH:mm
     */
    public static final String sfm = "yyyy-MM-dd HH:mm";                               //new SimpleDateFormat("");
    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static final String sf = "yyyy-MM-dd HH:mm:ss";
 
    /**
     * yyyyMMdd 用于带日期的SN生成
     */
    public static final String sfPreSn = "yyyyMMdd";
 
    /**
     * HH:mm:ss
     */
    public static final String sfH = "HH:mm:ss";
 
    /**
     * HH:mm
     */
    public static final String sf2 = "HH:mm";
 
 
    public DateUtil() {
    }
 
    public static String getNow() {
        return ThreadDateUtil.getDateFormat(sf).format(new Date());
    }
 
    public static String getNowDate() {
        return ThreadDateUtil.getDateFormat(sf1).format(new Date());
    }
 
    public static String getNowDateForPreSn() {
        return ThreadDateUtil.getDateFormat(sfPreSn).format(new Date());
    }
 
    /**
     * 根据时间类型比较时间大小
     *
     * @param source
     * @param traget
     * @param "YYYY-MM-DD" "yyyyMMdd HH:mm:ss"  类型可自定义
     * @param
     * @return 0 :source和traget时间相同
     * 1 :source比traget时间大
     * -1:source比traget时间小
     * @throws Exception
     */
    public static int DateCompare(long source, long traget) throws Exception {
        if (source == traget)
            return 0;
        else if (source > traget) {
            return 1;
        } else {
            return -1;
        }
    }
 
    /**
     * 方法名: getTime
     * 方法描述: 获取当前时间时分
     * 修改时间:2016年12月2日 上午10:12:46
     * 参数 @return 参数说明
     * 返回类型 String 返回类型
     */
    public static String getTime(long time) {
        return ThreadDateUtil.getDateFormat("HH:mm").format(time);
    }
 
    /**
     * 方法名: getTime
     * 方法描述: 获取当前时间时分
     * 修改时间:2016年12月2日 上午10:12:46
     * 参数 @return 参数说明
     * 返回类型 String 返回类型
     */
    public static String getTime() {
        return ThreadDateUtil.getDateFormat("HH:mm").format(getNowTime());
    }
 
    /**
     * 方法名: getEndTime
     * 方法描述: (这里用一句话描述这个方法的作用)
     * 修改时间:2016年11月24日 下午9:17:18
     * 参数 @param date
     * 参数 @return 参数说明
     * 返回类型 Date 返回类型
     */
    public static Date getEndTime(Date date) {
        if (date == null) {
            return null;
        }
        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, 999);
        return calendar.getTime();
    }
 
 
    /**
     * 方法名: getDate
     * 方法描述: 将long型的时间 字符串 转换为yyyy-MM-dd格式的日期字符串
     * 修改时间:2017年6月19日 下午3:24:45
     * 参数 @param str
     * 参数 @return 参数说明
     * 返回类型 String 返回类型
     */
    public static String getDate(String str) {
        Date d = new Date(parseLong(str));
        return ThreadDateUtil.getDateFormat(sf1).format(d);
    }
 
    /**
     * 将字符串类型转换为整型
     *
     * @param s
     * @return
     */
    public static Long parseLong(String s) {
        if (s != null && s.trim().matches("^(-)?[0-9]+$"))
            return Long.parseLong(s.trim());
        return 0L;
    }
 
    public static Date getSfDate(String str) {
        Date date;
        try {
            if (str.matches(SF)) {
                date = ThreadDateUtil.getDateFormat(sf).parse(str);
            } else if (str.matches(SFM)) {
                date = ThreadDateUtil.getDateFormat(sfm).parse(str);
            } else
                date = ThreadDateUtil.getDateFormat(sf1).parse(str);
            return date;
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 方法名: getDate
     * 方法描述: long 转date
     * 修改时间:2017年6月13日 上午10:50:34
     * 参数 @param dateTime
     * 参数 @return 参数说明
     * 返回类型 Date 返回类型
     */
    public static Date getDate(long dateTime) {
        String date = getDateTime(dateTime);
        Date d = null;
        try {
            d = ThreadDateUtil.getDateFormat(sf).parse(date);
            return d;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 方法名: getDateByLong
     * 方法描述: long 转成String YYYY-MM-dd
     * 修改时间:2017年6月13日 上午11:41:43
     * 参数 @param dateTime
     * 参数 @return 参数说明
     * 返回类型 String 返回类型
     *
     * @return
     */
    public static String getDateByLong(long dateTime) {
        Date date = getDate(dateTime);
        return ThreadDateUtil.getDateFormat(sf1).format(date);
    }
 
    public static String getDateTime(Long date) {
        if (null == date) {
            return "";
        }
        try {
            Date d = new Date(date);
            return ThreadDateUtil.getDateFormat(sf).format(d);
        } catch (Exception e) {
            LOGGER.error(" getTime E: {}", date, e);
        }
        return "";
    }
 
    public static String getTime(Long date) {
        if (null == date) {
            return "";
        }
        try {
            Date d = new Date(date);
            return ThreadDateUtil.getDateFormat(sf).format(d);
        } catch (Exception e) {
            LOGGER.error(" getTime E: {}", date, e);
        }
        return "";
    }
 
    /**
     * 方法名: getStartTime
     * 方法描述: 获取当前0点
     * 修改时间:2017年5月8日 下午5:03:49
     * 参数 @return 参数说明
     * 返回类型 Long 返回类型
     */
    public static long getStartTime() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime().getTime();
    }
 
    /**
     * 方法名: getEndTime
     * 方法描述: 获取当天结束时间
     * 修改时间:2017年5月8日 下午5:04:12
     * 参数 @return 参数说明
     * 返回类型 long 返回类型
     */
    public static long getEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime().getTime();
    }
 
    /**
     * 获取某天最大时间
     *
     * @param date
     * @return
     */
    public static Date getEndOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        ;
        LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
        return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }
 
    /**
     * 获取某天最小时间
     *
     * @param date
     * @return
     */
    public static Date getStartOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
        return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }
 
    /**
     * 方法名: getStartTime
     * 方法描述: 获取当前0点
     * 修改时间:2017年5月8日 下午5:03:49
     * 参数 @return 参数说明
     * 返回类型 Long 返回类型
     *
     * @throws ParseException
     */
    public static long getStartTime(String dateStr) throws ParseException {
        if (StringUtils.isBlank(dateStr)) {
            return 0;
        } else {
            Date date = ThreadDateUtil.getDateFormat(sf1).parse(dateStr);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            return cal.getTime().getTime();
        }
 
    }
 
    /**
     * 方法名: getEndTime
     * 方法描述: 获取当天结束时间
     * 修改时间:2017年5月8日 下午5:04:12
     * 参数 @return 参数说明
     * 返回类型 long 返回类型
     *
     * @throws ParseException
     */
    public static long getEndTime(String dateStr) throws ParseException {
        if (StringUtils.isBlank(dateStr)) {
            return 0;
        } else {
            Date date = ThreadDateUtil.getDateFormat(sf1).parse(dateStr);
            Calendar todayEnd = Calendar.getInstance();
            todayEnd.setTime(date);
            todayEnd.set(Calendar.HOUR_OF_DAY, 23);
            todayEnd.set(Calendar.MINUTE, 59);
            todayEnd.set(Calendar.SECOND, 59);
            todayEnd.set(Calendar.MILLISECOND, 999);
            return todayEnd.getTime().getTime();
        }
    }
 
    /**
     * 获取 指定时间毫秒数的0点
     *
     * @param time
     * @return
     */
    public static long getStartTime(long time) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(time);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime().getTime();
    }
 
    /**
     * 获取 指定时间毫秒数的23点59分59秒999毫秒
     *
     * @param time
     * @return
     */
    public static long getEndTime(long time) {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.setTimeInMillis(time);
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime().getTime();
    }
 
    public static String getDatem(Long date) {
        try {
            Date d = new Date(date);
            return ThreadDateUtil.getDateFormat(sfm).format(d);
        } catch (Exception e) {
            LOGGER.error(" getDatem E: {}", date, e);
        }
        return "";
    }
 
    public static Long getNowTime() {
        return System.currentTimeMillis();
 
    }
 
    /**
     * 方法名: getTodayEndTime
     * 方法描述: 获取当天结束时间
     * 修改时间:2016年11月2日 下午6:53:23
     * 参数 @return 参数说明
     * 返回类型 Long 返回类型
     */
    public static int getDiffTodayEndTime() {
        return (int) ((getTodayEndTime() - getNowTime()) / 1000);
    }
 
    /**
     * 方法名: getStartDateTime
     * 方法描述: 获取一天开始time
     * 修改时间:2016年12月5日 上午11:57:01
     * 参数 @param time
     * 参数 @return 参数说明
     * 返回类型 String 返回类型
     */
    public static String getStartDateTime(long time) {
        Date d = new Date(time);
        return ThreadDateUtil.getDateFormat(sf).format(d);
    }
 
    /**
     * 方法名: getTodayEndTime
     * 方法描述: 获取当天时间差
     * 修改时间:2016年11月2日 下午6:53:23
     * 参数 @return 参数说明
     * 返回类型 Long 返回类型
     */
    public static Long getTodayEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime().getTime();
    }
 
    /**
     * 获取参数时间的开始时间
     *
     * @param time
     * @return
     */
    public static Date getBeginTimeByDate(long time) {
        Date d = new Date(time);
        Calendar todayBegin = Calendar.getInstance();
        todayBegin.setTime(d);
        todayBegin.set(Calendar.HOUR_OF_DAY, 00);
        todayBegin.set(Calendar.MINUTE, 00);
        todayBegin.set(Calendar.SECOND, 00);
        todayBegin.set(Calendar.MILLISECOND, 000);
        return todayBegin.getTime();
 
    }
 
    /**
     * 获取参数时间的结束时间
     *
     * @param time
     * @return
     */
    public static Date getEndTimeByDate(long time) {
        Date d = new Date(time);
        Calendar todayBegin = Calendar.getInstance();
        todayBegin.setTime(d);
        todayBegin.set(Calendar.HOUR_OF_DAY, 23);
        todayBegin.set(Calendar.MINUTE, 59);
        todayBegin.set(Calendar.SECOND, 59);
        todayBegin.set(Calendar.MILLISECOND, 999);
        return todayBegin.getTime();
    }
 
 
    /**
     * 判断当前时间是否在[startTime, endTime]区间,注意时间格式要一致
     *
     * @param nowTime   当前时间
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);
        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }
 
    public static Long getLongTime(String str) {
        if (StringUtils.isBlank(str)) {
            return getNowTime();
        }
        Date date = null;
        try {
            if (str.matches(SF)) {
                date = ThreadDateUtil.getDateFormat(sf).parse(str);
            } else if (str.matches(SFM)) {
                date = ThreadDateUtil.getDateFormat(sfm).parse(str);
            } else {
                date = ThreadDateUtil.getDateFormat(sf1).parse(str);
            }
        } catch (ParseException e) {
            LOGGER.error(" getLongTime E: {}", str, e);
        }
        if (null == date) {
            return 0L;
        }
        return date.getTime();
 
    }
 
    /**
     * 把时间参数 转换为 long
     *
     * @param dateStr
     * @return
     * @throws BackingStoreException
     */
    public static long getTime(String dateStr) throws ApiException {
        try {
            Date date = ThreadDateUtil.getDateFormat(sf).parse(dateStr);
            return date.getTime();
        } catch (ParseException e) {
            LOGGER.error(" getTime E: {}", dateStr, e);
            throw new ApiException(" time parese error: " + dateStr);
        }
    }
 
    /**
     * 把时间参数 转换为 long
     *
     * @param dateStr yyyy-MM-dd
     * @return
     * @throws BackingStoreException
     */
    public static long getTimeByStringDate(String dateStr) throws ApiException {
        try {
            Date date = ThreadDateUtil.getDateFormat(sf1).parse(dateStr);
            return date.getTime();
        } catch (ParseException e) {
            LOGGER.error(" getTime E: {}", dateStr, e);
            throw new ApiException(" time parese error: " + dateStr);
        }
    }
 
    /**
     * 方法名: getNowYear
     * 方法描述: 获取当前年份
     * 修改时间:2016年6月22日 下午6:52:24
     * 参数 @return 参数说明
     * 返回类型 String 返回类型
     */
    public static String getNowYear() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();
        return sdf.format(date);
    }
 
    /**
     * 方法名: getNowMonth
     * 方法描述: 获取当前年月yyyy/MM
     * 修改时间:2016年6月22日 下午6:52:24
     * 参数 @return 参数说明
     * 返回类型 String 返回类型
     */
    public static String getNowMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM");
        Date date = new Date();
        return sdf.format(date);
    }
 
    /**
     * 方法名: getNowMonthDaySn
     * 方法描述: 获取当前年月yyyyMMdd
     * 修改时间:2017年6月28日 上午10:52:24
     * 参数 @return 参数说明
     * 返回类型 String 返回类型
     */
    public static String getNowMonthDay() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        return sdf.format(date);
    }
 
 
    /**
     * 获取明天的日期字符串
     *
     * @return
     */
    public static String tomorrowDateStr(int day) {
        Date date = new Date();//取时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //把日期往后增加一天.整数往后推,负数往前移动
        calendar.add(calendar.DATE, day);
        //这个时间就是日期往后推一天的结果
        date = calendar.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String tomorrowStr = formatter.format(date);
        return tomorrowStr;
    }
 
    /**
     * dq
     * 获取指定年月yyyyMMddHHmmss
     *
     * @param dateTime
     * @return
     */
    public static String getNowMonthDay(long dateTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date(dateTime);
        return sdf.format(date);
    }
 
    public static String getAge(String birthDate) {
        if (null == birthDate) {
            throw new RuntimeException("出生日期不能为null");
        }
 
        int age = 0;
        String[] str = birthDate.split("-");
        if (str.length == 3) {
            Date now = new Date();
            SimpleDateFormat format_y = new SimpleDateFormat("yyyy");
            SimpleDateFormat format_M = new SimpleDateFormat("MM");
            String birth_year = str[0];
            String this_year = format_y.format(now);
            String birth_month = str[1];
            String this_month = format_M.format(now);
            // 初步,估算
            age = Integer.parseInt(this_year) - Integer.parseInt(birth_year);
 
            // 如果未到出生月份,则age - 1
 
            if (this_month.compareTo(birth_month) < 0) {
                age -= 1;
            }
            if (age < 0) {
                age = 0;
            }
            return age + "." + (parseInt(this_month) - parseInt(this_month));
        }
 
        return "0";
    }
 
    /**
     * 将字符串类型转换为整型
     *
     * @param s
     * @return
     */
    public static Integer parseInt(String s) {
        if (s != null && s.trim().matches("^(-)?[0-9]+$"))
            return Integer.parseInt(s.trim());
        return 0;
    }
 
    /**
     * 查询前一天
     *
     * @param date
     * @return
     */
    public static String getPreDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        date = calendar.getTime();
        return ThreadDateUtil.getDateFormat(sf1).format(date);
    }
 
    /**
     * 方法名: isBirthday
     * 方法描述: 判断输入的字符串是否是合法的生日 生日不能大于当前日期
     * 修改时间:2017年3月22日 上午9:16:09
     * 参数 @param birthday
     * 参数 @return 参数说明
     * 返回类型 boolean 返回类型
     */
    public static boolean isBirthday(String birthday) {
        if (StringUtils.isBlank(birthday)) {
            return false;
        }
        if (birthday.length() != 10) {
            return false;
        }
        Pattern pattern = Pattern
                .compile("^[1,2]\\d{3}(-){1}(0[1-9]||1[0-2])(-){1}(0[1-9]||[1,2][0-9]||3[0,1])$");
        Matcher matcher = pattern.matcher(birthday);
        if (!matcher.matches()) {
            return false;
        }
        Date birth = null;
        try {
            birth = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (!new SimpleDateFormat("yyyy-MM-dd").format(birth).equals(birthday)) {
            return false;
        }
        // 获取当前日期的毫秒数
        long currentTime = System.currentTimeMillis();
        // 获取生日的毫秒数
        long birthTime = birth.getTime();
        // 如果当前时间小于生日,生日不合法。反之合法
        if (birthTime > currentTime) {
            return false;
        }
        return true;
    }
 
    /**
     * 方法名: isAfterHour
     * 方法描述: 当前时间7点之后可以显示
     * 修改时间:2017年5月11日 下午3:39:34
     * 参数 @return 参数说明
     * 返回类型 boolean 返回类型
     */
    public static boolean isAfterHour() {
        Calendar todayStart = Calendar.getInstance();
        //用d.getHour()可以获取当前小时数。
        int hour = todayStart.get(Calendar.HOUR_OF_DAY);
        return hour >= 7;
    }
 
    /**
     * wye
     * 获取某天开始时间0:0:0:0
     *
     * @param date
     * @param addDay
     * @return
     */
    public static long getStartTime(Date date, int addDay) {
        Calendar dayTimeStart = Calendar.getInstance();
        dayTimeStart.setTime(date);
        if (addDay != 0) {
            dayTimeStart.add(Calendar.DATE, addDay);
        }
        dayTimeStart.set(Calendar.HOUR_OF_DAY, 0);
        dayTimeStart.set(Calendar.MINUTE, 0);
        dayTimeStart.set(Calendar.SECOND, 0);
        dayTimeStart.set(Calendar.MILLISECOND, 0);
        return dayTimeStart.getTime().getTime();
 
    }
 
    /**
     * wye
     * 获取某天结束时间23:59:59:999
     *
     * @param date
     * @param addDay
     * @return
     */
    public static long getEndTime(Date date, int addDay) {
        Calendar dayTimeEnd = Calendar.getInstance();
        dayTimeEnd.setTime(date);
        if (addDay != 0) {
            dayTimeEnd.add(Calendar.DATE, addDay);
        }
        dayTimeEnd.set(Calendar.HOUR_OF_DAY, 23);
        dayTimeEnd.set(Calendar.MINUTE, 59);
        dayTimeEnd.set(Calendar.SECOND, 59);
        dayTimeEnd.set(Calendar.MILLISECOND, 999);
        return dayTimeEnd.getTime().getTime();
    }
 
    /**
     * wye
     * 获取当前时间 之后或之前的几天
     *
     * @param
     * @return
     * @throws ParseException
     */
    public static long getLongTimeByDay(long dateTime, int day) {
        Calendar start = Calendar.getInstance();
        start.setTimeInMillis(dateTime);
        start.add(Calendar.DAY_OF_MONTH, day);
        return start.getTime().getTime();
    }
 
 
    /**
     * 日期格式化字符串
     * 默认格式   yyyy-MM-dd HH:mm:ss
     */
    public static String format(Date date) {
        if (date == null) {
            return null;
        }
        return format(date, DatePattern.YYYY_MM_DD_HHmmss);
    }
 
    /**
     * 日期格式化字符串
     */
    public static String format(Date date, DatePattern datePattern) {
        if (date == null || datePattern == null) {
            return null;
        }
        return format(date, datePattern.pattern);
    }
 
    /**
     * 日期格式化字符串
     *
     * @param date    日期对象
     * @param pattern 日期格式
     * @return
     */
    public static String format(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        return ThreadDateUtil.getDateFormat(pattern).format(date);
    }
 
    /**
     * 日期字符串  转换为日期
     * 默认格式   yyyy-MM-dd HH:mm:ss
     *
     * @throws ParseException
     */
    public static Date parse(String source) throws ParseException {
        return parse(source, DatePattern.YYYY_MM_DD_HHmmss);
    }
 
    /**
     * 日期格式化字符串
     *
     * @param source      时间字符串
     * @param datePattern 日期格式
     * @return
     * @throws ParseException
     */
    public static Date parse(String source, DatePattern datePattern) throws ParseException {
        return parse(source, datePattern.pattern);
    }
 
    /**
     * 日期字符串  转换为日期
     *
     * @param source  时间字符串
     * @param pattern 日期格式
     * @return
     * @throws ParseException
     */
    public static Date parse(String source, String pattern) throws ParseException {
        return ThreadDateUtil.getDateFormat(pattern).parse(source);
    }
 
    /**
     * 日期字符串  转换为日期毫秒数
     * 默认格式   yyyy-MM-dd HH:mm:ss
     *
     * @throws ParseException
     */
    public static Long parseToLong(String source) throws ParseException {
        return parse(source, DatePattern.YYYY_MM_DD_HHmmss).getTime();
    }
 
    /**
     * 日期字符串  转换为日期毫秒数
     *
     * @param source      时间字符串
     * @param datePattern 日期格式
     * @return
     * @throws ParseException
     */
    public static Long parseToLong(String source, DatePattern datePattern) throws ParseException {
        return parse(source, datePattern.pattern).getTime();
    }
 
    /**
     * 日期字符串  转换为日期毫秒数
     *
     * @param source  时间字符串
     * @param pattern 日期格式
     * @return
     * @throws ParseException
     */
    public static Long parseToLong(String source, String pattern) throws ParseException {
        return ThreadDateUtil.getDateFormat(pattern).parse(source).getTime();
    }
 
 
    /**
     * 获取日期年份
     *
     * @param date 日期
     * @return
     */
    public static String getYear(Date date) {
        return format(date).substring(0, 4);
    }
 
    /**
     * 功能描述:返回月
     *
     * @param date Date 日期
     * @return 返回月份
     */
    public static int getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }
 
    /**
     * 功能描述:返回日
     *
     * @param date Date 日期
     * @return 返回日份
     */
    public static int getDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }
 
    /**
     * 功能描述:返回小
     *
     * @param date 日期
     * @return 返回小时
     */
    public static int getHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }
 
    /**
     * 功能描述:返回分
     *
     * @param date 日期
     * @return 返回分钟
     */
    public static int getMinute(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }
 
    /**
     * 返回秒钟
     *
     * @param date Date 日期
     * @return 返回秒钟
     */
    public static int getSecond(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.SECOND);
    }
 
    /**
     * 功能描述:返回毫
     *
     * @param date 日期
     * @return 返回毫
     */
    public static long getMillis(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getTimeInMillis();
    }
 
 
    /**
     * 验证请求是否在合法时间内
     *
     * @param timestamp
     * @return
     */
    public static boolean verifyRequestTime(long timestamp) {
        long now = System.currentTimeMillis();
        long time = now - timestamp;
        // System.out.println(now);
        // System.out.println(time);
        // System.out.println(time / 1000);
        if (time / 1000 > 60) {
            return false;
        }
        return true;
    }
 
    public static String formatDate(String format) {
        return formatDate(new Date(), format);
    }
 
    public static String formatDate(Date date, String format) {
        if ((null == date) || (StringUtils.isEmpty(format))) {
            return null;
        }
        return new SimpleDateFormat(format).format(date);
    }
 
    public static long getYesterDay() {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DATE, -1);
        date = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        String dateString = format.format(date);
        long time = 0;
        try {
            time = format.parse(dateString).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }
 
    public static long get1MonthByNow() {
        Calendar c = Calendar.getInstance();
        //过去一月
        c.setTime(new Date());
        c.add(Calendar.MONTH, -1);
        Date m = c.getTime();
        long time = m.getTime();
//        String mon = format.format(m);
        return time;
    }
 
    public static int dateDiff(Date date1, Date date2) {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
        return days;
    }
 
    public static int dateDiff(Long date1, Long date2) {
        int days = (int) ((date2 - date1) / (1000 * 3600 * 24));
        return days;
    }
 
 
    public static List te() {
        List<String> list = new LinkedList<>();
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH) + 1;
        int year = calendar.get(Calendar.YEAR);
        for (int i = 1; i <= 12; i++) {
            if (month == 1) {
                list.add(year + "-" + month);
                break;
            }
            list.add(year + "-" + month);
            month--;
        }
        return list;
    }
 
    /**
     * 获取上个月的第一天
     *
     * @return
     */
    public static long getLastMonthStart() {
        //获取前月的第一天
        Calendar start = Calendar.getInstance();
        //获取当前日期
        start.add(Calendar.MONTH, -1);
        //设置为1号,当前日期既为本月第一天
        start.set(Calendar.DAY_OF_MONTH, 1);
        return DateUtil.getStartTime(start.getTimeInMillis());
    }
 
    /**
     * 获取本月第一天
     *
     * @return
     */
    public static long getCurrentMonthStart() {
        //获取前月的第一天
        Calendar start = Calendar.getInstance();
        //设置为1号,当前日期既为本月第一天
        start.set(Calendar.DAY_OF_MONTH, 1);
        return DateUtil.getStartTime(start.getTimeInMillis());
    }
 
    /**
     * 获取月份开始时间
     *
     * @param date
     * @return
     */
    public static long getMonthStart(Date date) {
        //获取前月的第一天
        Calendar start = Calendar.getInstance();
        start.setTime(date);
        //设置为1号,当前日期既为本月第一天
        start.set(Calendar.DAY_OF_MONTH, 1);
        return DateUtil.getStartTime(start.getTimeInMillis());
    }
 
    /**
     * 获取月份结束时间
     *
     * @param date
     * @return
     */
    public static long getMonthEnd(Date date) {
        Calendar end = Calendar.getInstance();
        end.setTime(date);
        end.set(Calendar.DAY_OF_MONTH, end.getActualMaximum(Calendar.DAY_OF_MONTH));
        return DateUtil.getEndTime(end.getTimeInMillis());
    }
 
    /**
     * 获取上个月的最后一天
     *
     * @return
     */
    public static long getLastMonthEnd() {
        Calendar cale = Calendar.getInstance();
        cale.set(Calendar.DAY_OF_MONTH, 0);
        return DateUtil.getEndTime(cale.getTimeInMillis());
    }
 
    /**
     * 上个月
     *
     * @return
     */
    public static String getLastMonth() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月
        date = calendar.getTime();
        String accDate = format.format(date);
        return accDate;
    }
 
    /**
     * 前2个月
     *
     * @return
     */
    public static String getLast2Month() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 2); // 设置为上一个月
        date = calendar.getTime();
        String accDate = format.format(date);
        return accDate;
    }
 
    public static String getYearMoney(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        String format1 = format.format(date);
        return format1;
    }
 
    /**
     * 获取精确到秒的时间戳
     *
     * @param date
     * @return
     */
    public static int getSecondTimestamp(Date date) {
        if (null == date) {
            return 0;
        }
        String timestamp = String.valueOf(date.getTime() / 1000);
        return Integer.valueOf(timestamp);
    }
 
    /**
     * 获取门店营业时间
     *
     * @param storeLocalTime 门店时间
     * @param date
     * @return
     */
    public static Date getStoreDate(Long storeLocalTime, Date date) {
        Date storeDate = getDate(storeLocalTime);
        String format = ThreadDateUtil.getDateFormat(sfH).format(storeDate);
        String dateStr = ThreadDateUtil.getDateFormat(sf1).format(date);
//        Date userDate = getDate(receiveTime);
//        String nowDate = ThreadDateUtil.getDateFormat(sf1).format(userDate);
        String concat = dateStr.concat(" ").concat(format);
        Date newDate = null;
        try {
            newDate = ThreadDateUtil.getDateFormat(sf).parse(concat);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return newDate;
    }
 
    /**
     * 获取指定年月的第一天
     *
     * @param year
     * @param month
     * @return
     */
    public static String getFirstDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最小天数
        int firstDay = cal.getMinimum(Calendar.DATE);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }
 
    /**
     * 获取指定年月的最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public static String getLastDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DATE);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
        return sdf.format(cal.getTime());
    }
 
    /**
     * 计算时间相差多少天
     *
     * @param startDate
     * @param endDate
     * @return
     * @throws ParseException
     */
    public static int getDayDiffer(Date startDate, Date endDate) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long startDateTime = dateFormat.parse(dateFormat.format(startDate)).getTime();
        long endDateTime = dateFormat.parse(dateFormat.format(endDate)).getTime();
        return (int) ((endDateTime - startDateTime) / (1000 * 3600 * 24));
    }
 
    /**
     * 计算时间差
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static int getOrderDayDiffer(Date startDate, Date endDate) {
        int dayDiffer = 0;
        try {
            dayDiffer = getDayDiffer(startDate, endDate);
            if (dayDiffer < 0) {
                throw new ApiException(CommonResult.failed().getMessage());
            }
            if (dayDiffer == 0) {
                dayDiffer = 1;
            }
        } catch (ParseException e) {
            e.printStackTrace();
            throw new ApiException(e);
        }
        return dayDiffer;
    }
 
    /**
     * 获取时分
     *
     * @param date
     * @return
     */
    public static String getHm(Date date) {
        DateFormat dateFormat = ThreadDateUtil.getDateFormat(sf2);
        return dateFormat.format(date);
    }
 
    /**
     * 获取年月日
     *
     * @param date
     * @return
     */
    public static String getYmd(Date date) {
        DateFormat dateFormat = ThreadDateUtil.getDateFormat(sf1);
        return dateFormat.format(date);
    }
 
    public static long swapYmd(Long localTime, Long receiveTime) {
        Date localDate = DateUtil.getDate(localTime);
        String hms = ThreadDateUtil.getDateFormat(sfH).format(localDate);
        Date receiveDate = DateUtil.getDate(receiveTime);
        String ymd = ThreadDateUtil.getDateFormat(sf1).format(receiveDate);
        String concat = ymd.concat(" ").concat(hms);
        Date newDate = null;
        try {
            newDate = ThreadDateUtil.getDateFormat(sf).parse(concat);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return newDate.getTime();
 
    }
 
    /**
     *     * 获取指定日期所在周的周一
     * <p>
     *     *
     * <p>
     *     * @param date      日期
     * <p>
     *    
     */
    public static Date getFirstDayOfWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            c.add(Calendar.DAY_OF_MONTH, -1);
        }
        c.add(Calendar.DATE, c.getFirstDayOfWeek() - c.get(Calendar.DAY_OF_WEEK) + 1);
        return c.getTime();
    }
 
    /**
     *     * 获取指定日期所在周的周日
     * <p>
     *     *
     * <p>
     *     * @param date      日期
     * <p>
     *    
     */
    public static Date getLastDayOfWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);// 如果是周日直接返回
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            return date;
        }
        // System.out.println(c.get(Calendar.DAY_OF_WEEK));
        c.add(Calendar.DATE, 7 - c.get(Calendar.DAY_OF_WEEK) + 1);
        return c.getTime();
    }
 
}