DateUtil.java
41.4 KB
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
1386
1387
1388
1389
1390
1391
package com.viontech.util;
import org.apache.commons.lang3.StringUtils;
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
@SuppressWarnings({"unchecked","deprecation"})
public class DateUtil {
private static DateFormatSymbols dateformatSymbols = new DateFormatSymbols(Locale.getDefault());
static {
dateformatSymbols.setShortWeekdays(new String[]{"", "周日", "周一", "周二", "周三", "周四", "周五", "周六"});
}
/**
* 英文简写(默认)如:2010
*/
public static SimpleDateFormat FORMAT_YYYY = new SimpleDateFormat("yyyy");
/**
* 英文简写(默认)如:2010
*/
public static SimpleDateFormat FORMAT_YYYY_CN = new SimpleDateFormat("yyyy年");
/**
* 英文简写(默认)如:2010-12
*/
public static SimpleDateFormat FORMAT_YYYY_MM = new SimpleDateFormat("yyyy-MM");
/**
* 英文简写(默认)如:2010-12
*/
public static SimpleDateFormat FORMAT_YYYY_MM_CN = new SimpleDateFormat("yyyy年MM月");
/**
* 英文简写(默认)如:2010-12-01
*/
public static SimpleDateFormat FORMAT_YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd");
/**
* 中文简写 如:2010年12月01日
*/
public static SimpleDateFormat FORMAT_YYYY_MM_DD_CN = new SimpleDateFormat("yyyy年MM月dd日");
/**
* 中文简写 如:2010年12月01日(周日)
*/
public static SimpleDateFormat FORMAT_SHORT_CN_WEEK = new SimpleDateFormat("yyyy年MM月dd日(E)", dateformatSymbols);
/**
* 英文简写 如:2010-12-01(sunday)
*/
public static SimpleDateFormat FORMAT_SHORT_WEEK = new SimpleDateFormat("yyyy-MM-dd(E)", dateformatSymbols);
/**
* 英文简写(默认)如:2010-12-31 24
*/
public static SimpleDateFormat FORMAT_YYYY_MM_DD_HH = new SimpleDateFormat("yyyy-MM-dd HH");
/**
* 英文全称 如:2010-12-01 23:15
*/
public static SimpleDateFormat FORMAT_YYYY_MM_DD_HH_MM = new SimpleDateFormat("yyyy-MM-dd HH:mm");
/**
* 英文全称 如:2010-12-01 23:15:06
*/
public static SimpleDateFormat FORMAT_YYYY_MM_DD_HH_MM_SS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* 中文全称 如:2010年12月01日 23时15分06秒
*/
public static SimpleDateFormat FORMAT_YYYY_MM_DD_HH_MM_SS_CN = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
/**
* 精确到毫秒的完整时间 如:2010-12-01 23:15:06.999
*/
public static SimpleDateFormat FORMAT_YYYY_MM_DD_HH_MM_SS_S = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
/**
* 精确到毫秒的完整中文时间 如:2010年12月01日 23时15分06秒999毫秒
*/
public static SimpleDateFormat FORMAT_YYYY_MM_DD_HH_MM_SS_S_CN = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒SSS毫秒");
/**
* 精确到毫秒的完整UTC时间 如:2017-07-04T15:50:18.223
*/
public static SimpleDateFormat FORMAT_YYYY_MM_DD_HH_MM_SS_S_UTC = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
/**
* 精确到毫秒的完整UTC时间 如:2017-07-04T15:50:18.223
*/
public static SimpleDateFormat FORMAT_YYYY_MM_DD_HH_MM_SS_SSS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
/**
* 英文简写(默认)如:12-31
*/
public static SimpleDateFormat FORMAT_MM_DD = new SimpleDateFormat("MM-dd");
/**
* 英文简写带星期(默认)如:12-31(周三)
*/
public static SimpleDateFormat FORMAT_MM_DD_E = new SimpleDateFormat("MM-dd(E)", dateformatSymbols);
/**
* 英文简写(默认)如:12-31 24
*/
public static SimpleDateFormat FORMAT_MM_DD_HH = new SimpleDateFormat("MM-dd HH");
/**
* 英文简写(默认)如:31
*/
public static SimpleDateFormat FORMAT_DD = new SimpleDateFormat("dd");
/**
* 英文简写(默认)如:31 24
*/
public static SimpleDateFormat FORMAT_DD_HH = new SimpleDateFormat("dd HH");
/**
* 英文简写(默认)如:24
*/
public static SimpleDateFormat FORMAT_HH = new SimpleDateFormat("HH");
public static SimpleDateFormat FORMAT_HHMMSSS = new SimpleDateFormat("HH:mm:ss.S");
public static SimpleDateFormat FORMAT_HHMMSS = new SimpleDateFormat("HH:mm:ss");
public static SimpleDateFormat FORMAT_HHMM = new SimpleDateFormat("HH:mm");
/**
* 星期的简写 周一 周二 周三 周四等
*/
public static SimpleDateFormat FORMAT_DAY_OF_WEEK_SORT = new SimpleDateFormat("E", dateformatSymbols);
public static final int FIRST_DAY_OF_WEEK = Calendar.MONDAY; // 中国周一是一周的第一天
/**
* 获取上周同期
*
* @param date
* @return 上周同天
*/
public static Date getLastWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.WEEK_OF_MONTH, -1);
return calendar.getTime();
}
/**
* 获取上月同期
*
* @param date 当日
* @return
*/
public static Date getLastMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, -1);
return calendar.getTime();
}
/**
* 获取下月同期
*
* @param date 当日
* @return
*/
public static Date getNextMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, 1);
return calendar.getTime();
}
/**
* 获取去年同期
*
* @param date 当日
* @return
*/
public static Date getLastYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.YEAR, -1);
return calendar.getTime();
}
/**
* 获取昨日日期
*
* @param date 当日
* @return
*/
public static Date getYesterday(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_YEAR, -1);
return calendar.getTime();
}
/**
* 获取明天日期
*
* @param date 当日
* @return
*/
public static Date getTorromow(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_YEAR, 1);
return calendar.getTime();
}
/**
* 获取下一小时
*
* @param date 日期
* @return
*/
public static Date getNextHour(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR_OF_DAY, 8);
return calendar.getTime();
}
/**
* 获取前一小时
*
* @param date 日期
* @return
*/
public static Date getPreviousHour(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR_OF_DAY, -1);
return calendar.getTime();
}
/**
* 获取下10分钟--数字可以任意取值
* @param date 日期
* @return
*/
public static Date getNextMinute(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MINUTE, 10);
return calendar.getTime();
}
public static Date getNextMin(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MINUTE, -30);
return calendar.getTime();
}
/**
* 判断 yesterDay 是否是 today 的昨天
*
* @param today 今天日期
* @param yesterDay 昨天日期
* @return
*/
public static boolean isYesterday(Date today, Date yesterDay) {
return isSameDay(getYesterday(today), yesterDay);
}
/**
* 判断 lastWeekDay 是否是 today 的上周同期
*
* @param today 今天日期
* @param lastWeekDay 上周同期日期
* @return
*/
public static boolean isLastWeekDay(Date today, Date lastWeekDay) {
return isSameDay(getLastWeek(today), lastWeekDay);
}
/**
* 判断 lastMonthDay 是否是 today 的上月同期
*
* @param today 今天日期
* @param lastMonthDay 上月同期
* @return
*/
public static boolean isLastMonthDay(Date today, Date lastMonthDay) {
return isSameDay(getLastMonth(today), lastMonthDay);
}
/**
* 判断 lastYearDay 是否是 today 的去年同期
*
* @param today 今天日期
* @param lastYearDay 去年同期日期
* @return
*/
public static boolean isLastYearDay(Date today, Date lastYearDay) {
return isSameDay(getLastYear(today), lastYearDay);
}
/**
* 判断两个日期是否是同一天
*
* @param date1 日期1
* @param date2 日期2
* @return
*/
public static boolean isSameHour(Date date1, Date date2) {
if (!isSameDay(date1, date2)) {
return false;
}
if (date1.getHours() != date2.getHours()) {
return false;
}
return true;
}
/**
* 判断两个日期是否是同一天
*
* @param date1 日期1
* @param date2 日期2
* @return
*/
public static boolean isSameDay(Date date1, Date date2) {
if (!isSameMonth(date1, date2)) {
return false;
}
if (date1.getDate() != date2.getDate()) {
return false;
}
return true;
}
/**
* 判断两个日期是否是同一周
*
* @param week 日期1
* @param otherWeek 日期2
* @return
*/
public static boolean isSameWeek(Date week, Date otherWeek) {
return isSameDay(getMondayOfWeek(week), getMondayOfWeek(otherWeek));
}
/**
* 判断两个日期是否是同一月
*
* @param date1 日期1
* @param date2 日期2
* @return
*/
public static boolean isSameMonth(Date date1, Date date2) {
if (date1.getYear() != date2.getYear()) {
return false;
}
if (date1.getMonth() != date2.getMonth()) {
return false;
}
return true;
}
/***
* 判断日期2是否是日期1的上周
* @param date1
* @param date2
* @return
*/
public static boolean isLastWeek(Date date1, Date date2) {
return isSameWeek(getLastWeek(date1), date2);
}
/***
/***
* 判断日期2是否是日期1的上月
* @param date1
* @param date2
* @return
*/
public static boolean isLastMonth(Date date1, Date date2) {
return isSameMonth(getLastMonth(date1), date2);
}
/***
* 判断日期2是否是日期1的去年同月
* @param date1
* @param date2
* @return
*/
public static boolean isLastYearMonth(Date date1, Date date2) {
return isSameMonth(getLastYear(date1), date2);
}
/**
* 判断两个日期是否是同一季度
*
* @param date1 日期1
* @param date2 日期2
* @return
*/
public static boolean isSameSeason(Date date1, Date date2) {
return getSeason(date1) == getSeason(date2);
}
/**
* 判断 日期2 是否是 日期1的 上一季度
*
* @param date1 日期1
* @param date2 日期2
* @return
*/
public static boolean isLastSeason(Date date1, Date date2) {
return getSeason(date1) == getSeason(date2) + 1;
}
/**
* 判断两个日期是否是同一年
*
* @param date1 日期1
* @param date2 日期2
* @return
*/
public static boolean isSameYear(Date date1, Date date2) {
if (date1.getYear() != date2.getYear()) {
return false;
}
return true;
}
/**
* 将HH:mm解析成整点对象
*
* @param HMStr
* @param before true 往前取整 false 往后取整
* @return
* @throws ParseException 解析异常
*/
public static Date parseHMDate2NowTime(String HMStr, boolean before) throws ParseException {
Calendar calendar = Calendar.getInstance();
Date HMTime = FORMAT_HHMM.parse(HMStr);
calendar.setTime(HMTime);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int milliSecond = calendar.get(Calendar.MILLISECOND);
if (before) {
hour = hour;
minute = 0;
second = 0;
milliSecond = 0;
} else {
hour = hour;
minute = 59;
second = 59;
milliSecond = 999;
}
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, second);
calendar.set(Calendar.MILLISECOND, milliSecond);
return calendar.getTime();
}
/**
* 将日期设置为整点
*
* @param date 需要设置的日期
* @param HMTime 小时分钟信息
* @param- before true 是指向前取整 false 向后取整 小时会+1
* @return
*/
public static Date setDayHMS(Date date, Date HMTime) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, HMTime.getHours());
calendar.set(Calendar.MINUTE, HMTime.getMinutes());
calendar.set(Calendar.SECOND, HMTime.getSeconds());
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
/**
* 将日期设置为对应的时分信息
*
* @param date 需要设置的日期
* @param HMTimeStr 小时分钟信息
* @return
* @throws ParseException
*/
public static Date setDayHM(Date date, String HMTimeStr) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);//HH:mm:ss.S
Date HMTime = null;
try {
HMTime = FORMAT_HHMM.parse(HMTimeStr);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar hmCalendar = Calendar.getInstance();
hmCalendar.setTime(HMTime);//HH:mm:ss.S
calendar.set(Calendar.HOUR_OF_DAY, hmCalendar.get(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, hmCalendar.get(Calendar.MINUTE));
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
/**
* 将日期设置为对应的时分秒
*
* @param date 需要设置的日期
* @param HMSTimeStr 小时分钟秒信息
* @return
* @throws ParseException
*/
public static Date setDayHMS(Date date, String HMSTimeStr) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);//HH:mm:ss.S
Date HMSTime = null;
try {
HMSTime = FORMAT_HHMMSS.parse(HMSTimeStr);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar hmsCalendar = Calendar.getInstance();
hmsCalendar.setTime(HMSTime);//HH:mm:ss.S
calendar.set(Calendar.HOUR_OF_DAY, hmsCalendar.get(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, hmsCalendar.get(Calendar.MINUTE));
calendar.set(Calendar.SECOND, hmsCalendar.get(Calendar.SECOND));
return calendar.getTime();
}
/**
* 将日期设置为对应的时分秒
*
* @param date 需要设置的日期
* @param HTimeStr 小时分钟秒信息
* @return
* @throws ParseException
*/
public static Date setDayH(Date date, String HTimeStr) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);//HH:mm:ss.S
Date HTime = null;
try {
HTime = FORMAT_HH.parse(HTimeStr);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar hCalendar = Calendar.getInstance();
hCalendar.setTime(HTime);//HH:mm:ss.S
calendar.set(Calendar.HOUR_OF_DAY, hCalendar.get(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
/**
* 获取时间戳
* 如:2017-06-02 17:42:20.350
*/
public static String getTimeString() {
Calendar calendar = Calendar.getInstance();
return FORMAT_YYYY_MM_DD_HH_MM_SS_S.format(calendar.getTime());
}
/**
* 获取时间戳
* 如:2017-06-02T17:42:20.350
*/
public static String getTstring() {
Calendar calendar = Calendar.getInstance();
return FORMAT_YYYY_MM_DD_HH_MM_SS_S_UTC.format(calendar.getTime());
}
/**
* 获取时间戳13位
* 如:1496396907810
*/
public static Long getTimestamp() {
Calendar calendar = Calendar.getInstance();
return calendar.getTime().getTime();
}
/**
* 获取服务器UTC时间
* @return
*/
public static Calendar getCurrentUTC() {
Calendar cal = Calendar.getInstance(Locale.CHINA);
int zoneOffset = cal.get(Calendar.ZONE_OFFSET);
int dstOffset = cal.get(Calendar.DST_OFFSET);
cal.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));
return cal;
}
/**
* UTC转本地时间
* @return
*/
public static String UTC2LocalTime(Date date) {
try {
Calendar cal = Calendar.getInstance(Locale.CHINA);
cal.setTime(date);
int zoneOffset = cal.get(Calendar.ZONE_OFFSET);
int dstOffset = cal.get(Calendar.DST_OFFSET);
cal.add(Calendar.MILLISECOND, zoneOffset + dstOffset);
return FORMAT_YYYY_MM_DD_HH_MM_SS_CN.format(cal.getTime());
} catch (Exception e) {
System.out.println("字符串格式不正确yyyy-MM-dd HH:mm:ss.S");
}
return "error";
}
/**
* 获取日期年份
*
* @param date 日期
* @return
*/
public static int getYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.YEAR);
}
/**
* 功能描述:返回月
*
* @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 日期
* @return 返回分钟
*/
public static int getMinuteOfDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.HOUR_OF_DAY) * 60 + 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 int getMillis(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(calendar.MILLISECOND);
}
/**
* 取得当天日期是周几
*
* @param date
* @return
*/
public static int getDayOfWeek(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.setFirstDayOfWeek(FIRST_DAY_OF_WEEK);
int day_of_week = c.get(Calendar.DAY_OF_WEEK);
day_of_week -= 1;
if (day_of_week == 0) {
day_of_week = 7;
}
return day_of_week;
}
/**
* 获取一个月 周一 到 周日 的个数 用count存储
*
* @param date
* @return 下标0 代表周一的天数 下表1代表周二的天数 依次类推
*/
public static int[] getCountDayOfWeekInMonth(Date date) {
int[] count = new int[7];
Date firstDate = getFirstDateOfMonth(date);
int dayOfWeek = getDayOfWeek(firstDate);
int dayOfMonth = getPassDayOfMonth(date);
for (int i = 0; i < dayOfMonth; i++) {
count[dayOfWeek - 1]++;
dayOfWeek++;
if (dayOfWeek > 7) {
dayOfWeek = 1;
}
}
return count;
}
/**
* 取得一月的第几周
*
* @param date
* @return
*/
public static int getWeekOfMonth(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
int week_of_year = c.get(Calendar.WEEK_OF_MONTH);
return week_of_year;
}
/**
* 取得一年的第几周
*
* @param date
* @return
*/
public static int getWeekOfYear(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
int week_of_year = c.get(Calendar.WEEK_OF_YEAR);
return week_of_year;
}
/**
* 根据日期取得对应周周一日期
*
* @param date
* @return
*/
public static Date getMondayOfWeek(Date date) {
Calendar monday = Calendar.getInstance();
monday.setTime(date);
monday.setFirstDayOfWeek(FIRST_DAY_OF_WEEK);
monday.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return monday.getTime();
}
/**
* 根据日期取得对应周周日日期
*
* @param date
* @return
*/
public static Date getSundayOfWeek(Date date) {
Calendar sunday = Calendar.getInstance();
sunday.setTime(date);
sunday.setFirstDayOfWeek(FIRST_DAY_OF_WEEK);
sunday.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
return sunday.getTime();
}
/**
* 根据日期取得对应月的第一个相同周几
*
* @param date
* @return
*/
public static Date getFirsSametWeekOfMonth(Date date) {
Calendar c = Calendar.getInstance();
date = setDayMinTime(date);
c.setTime(date);
int weekIndex = c.get(Calendar.DAY_OF_WEEK);
c.set(Calendar.DAY_OF_MONTH, 1);
while (true) {
if (weekIndex == c.get(Calendar.DAY_OF_WEEK))
break;
c.add(Calendar.DAY_OF_MONTH, 1);
}
return c.getTime();
}
/**
* 取得月的剩余天数
*
* @param date
* @return
*/
public static int getRemainDayOfMonth(Date date) {
int dayOfMonth = getDayOfMonth(date);
int day = getPassDayOfMonth(date);
return dayOfMonth - day;
}
/**
* 取得月已经过的天数
*
* @param date
* @return
*/
public static int getPassDayOfMonth(Date date) {
Date monthStart = setDayMinTime(getFirstDateOfMonth(new Date()));
if (monthStart.after(date))
return getDayOfMonth(date);
Date monthEnd = setDayMaxTime(getLastDateOfMonth(new Date()));
if (monthEnd.before(date))
return 0;
Calendar c = Calendar.getInstance();
c.setTime(new Date());
return c.get(Calendar.DAY_OF_MONTH);
}
/**
* 取得月天数
*
* @param date
* @return
*/
public static int getDayOfMonth(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.getActualMaximum(Calendar.DAY_OF_MONTH);
}
/**
* 取得月第一天
*
* @param date
* @return
*/
public static Date getFirstDateOfMonth(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
return c.getTime();
}
/**
* 取得月最后一天
*
* @param date
* @return
*/
public static Date getLastDateOfMonth(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
return c.getTime();
}
/**
* 取得季度第一天
*
* @param date
* @return
*/
public static Date getFirstDateOfSeason(Date date) {
return getFirstDateOfMonth(getSeasonDate(date)[0]);
}
/**
* 取得季度最后一天
*
* @param date
* @return
*/
public static Date getLastDateOfSeason(Date date) {
return getLastDateOfMonth(getSeasonDate(date)[2]);
}
/**
* 取得季度天数
*
* @param date
* @return
*/
public static int getDayOfSeason(Date date) {
int day = 0;
Date[] seasonDates = getSeasonDate(date);
for (Date date2 : seasonDates) {
day += getDayOfMonth(date2);
}
return day;
}
/**
* 取得季度剩余天数
*
* @param date
* @return
*/
public static int getRemainDayOfSeason(Date date) {
return getDayOfSeason(date) - getPassDayOfSeason(date);
}
/**
* 取得季度已过天数
*
* @param date
* @return
*/
public static int getPassDayOfSeason(Date date) {
int day = 0;
Date[] seasonDates = getSeasonDate(date);
Calendar c = Calendar.getInstance();
c.setTime(date);
int month = c.get(Calendar.MONTH);
if (month == Calendar.JANUARY || month == Calendar.APRIL
|| month == Calendar.JULY || month == Calendar.OCTOBER) {// 季度第一个月
day = getPassDayOfMonth(seasonDates[0]);
} else if (month == Calendar.FEBRUARY || month == Calendar.MAY
|| month == Calendar.AUGUST || month == Calendar.NOVEMBER) {// 季度第二个月
day = getDayOfMonth(seasonDates[0])
+ getPassDayOfMonth(seasonDates[1]);
} else if (month == Calendar.MARCH || month == Calendar.JUNE
|| month == Calendar.SEPTEMBER || month == Calendar.DECEMBER) {// 季度第三个月
day = getDayOfMonth(seasonDates[0]) + getDayOfMonth(seasonDates[1])
+ getPassDayOfMonth(seasonDates[2]);
}
return day;
}
/**
* 取得季度月
*
* @param date
* @return
*/
public static Date[] getSeasonDate(Date date) {
Date[] season = new Date[3];
Calendar c = Calendar.getInstance();
c.setTime(date);
int nSeason = getSeason(date);
if (nSeason == 1) {// 第一季度
c.set(Calendar.MONTH, Calendar.JANUARY);
season[0] = c.getTime();
c.set(Calendar.MONTH, Calendar.FEBRUARY);
season[1] = c.getTime();
c.set(Calendar.MONTH, Calendar.MARCH);
season[2] = c.getTime();
} else if (nSeason == 2) {// 第二季度
c.set(Calendar.MONTH, Calendar.APRIL);
season[0] = c.getTime();
c.set(Calendar.MONTH, Calendar.MAY);
season[1] = c.getTime();
c.set(Calendar.MONTH, Calendar.JUNE);
season[2] = c.getTime();
} else if (nSeason == 3) {// 第三季度
c.set(Calendar.MONTH, Calendar.JULY);
season[0] = c.getTime();
c.set(Calendar.MONTH, Calendar.AUGUST);
season[1] = c.getTime();
c.set(Calendar.MONTH, Calendar.SEPTEMBER);
season[2] = c.getTime();
} else if (nSeason == 4) {// 第四季度
c.set(Calendar.MONTH, Calendar.OCTOBER);
season[0] = c.getTime();
c.set(Calendar.MONTH, Calendar.NOVEMBER);
season[1] = c.getTime();
c.set(Calendar.MONTH, Calendar.DECEMBER);
season[2] = c.getTime();
}
return season;
}
/**
* 1 第一季度 2 第二季度 3 第三季度 4 第四季度
*
* @param date
* @return
*/
public static int getSeason(Date date) {
int season = 0;
Calendar c = Calendar.getInstance();
c.setTime(date);
int month = c.get(Calendar.MONTH);
switch (month) {
case Calendar.JANUARY:
case Calendar.FEBRUARY:
case Calendar.MARCH:
season = 1;
break;
case Calendar.APRIL:
case Calendar.MAY:
case Calendar.JUNE:
season = 2;
break;
case Calendar.JULY:
case Calendar.AUGUST:
case Calendar.SEPTEMBER:
season = 3;
break;
case Calendar.OCTOBER:
case Calendar.NOVEMBER:
case Calendar.DECEMBER:
season = 4;
break;
default:
break;
}
return season;
}
/**
* 取得年第一天
*
* @param date
* @return
*/
public static Date getFirstDateOfYear(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_YEAR, c.getActualMinimum(Calendar.DAY_OF_YEAR));
return setDayMinTime(c.getTime());
}
/**
* 取得年最后一天
*
* @param date
* @return
*/
public static Date getLastDateOfYear(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_YEAR, c.getActualMaximum(Calendar.DAY_OF_YEAR));
return setDayMaxTime(c.getTime());
}
/**
* String转Date
* 支持:2010-12-01 23:15:06.999
* 2010-12-01 23:15:06
* 2010-12-01 23:15
* 2010-12-01
*
* @param dateStr
* @return
* @throws ParseException
*/
public static Date dateFormat(String dateStr) throws ParseException {
if (dateStr.length() == 23||dateStr.length() ==22){
if(dateStr.contains("T")){
return DateUtil.FORMAT_YYYY_MM_DD_HH_MM_SS_S_UTC.parse(dateStr);
}
return DateUtil.FORMAT_YYYY_MM_DD_HH_MM_SS_S.parse(dateStr);
}
if (dateStr.length() == 19)
return DateUtil.FORMAT_YYYY_MM_DD_HH_MM_SS.parse(dateStr);
else if (dateStr.length() == 16)
return DateUtil.FORMAT_YYYY_MM_DD_HH_MM.parse(dateStr);
else if (dateStr.length() == 10)
return DateUtil.FORMAT_YYYY_MM_DD.parse(dateStr);
else
return null;
}
/**
* @Title: longToString
* @Description:把13位的毫秒值转化为String类型格式化的时间格式
*/
public static String longToString(long time) {
Date date = new Date(time);
String newDate = FORMAT_YYYY_MM_DD_HH_MM_SS_S_UTC.format(date);
return newDate;
}
/**
* 设置一天的最大值
*
* @param date
* @return
*/
public static Date setDayMaxTime(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, c.getActualMaximum(Calendar.HOUR_OF_DAY));
c.set(Calendar.MINUTE, c.getActualMaximum(Calendar.MINUTE));
c.set(Calendar.SECOND, c.getActualMaximum(Calendar.SECOND));
c.set(Calendar.MILLISECOND, c.getActualMaximum(Calendar.MILLISECOND));
return c.getTime();
}
/**
* 设置一天的最小值
*
* @param date
* @return
*/
public static Date setDayMinTime(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, c.getActualMinimum(Calendar.HOUR_OF_DAY));
c.set(Calendar.MINUTE, c.getActualMinimum(Calendar.MINUTE));
c.set(Calendar.SECOND, c.getActualMinimum(Calendar.SECOND));
c.set(Calendar.MILLISECOND, c.getActualMinimum(Calendar.MILLISECOND));
return c.getTime();
}
/**
* 当前日期加n毫秒
*
* @param date
* @param num
* @return
*/
public static Date dateAddMillisecond(Date date, int num) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MILLISECOND, num);
return c.getTime();
}
/**
* 获取两个时间点之间的天
*
* @param startDate 开始时间
* @param endDate 结束时间
* @return
*/
public static List<Date> getDaysBetweenStartDateAndEndDate(Date startDate, Date endDate) {
List<Date> dates = new ArrayList<>();
Calendar c = Calendar.getInstance();
c.setTime(startDate);
while (c.getTime().before(endDate)) {
dates.add(c.getTime());
c.add(Calendar.DAY_OF_MONTH, 1);
}
dates.add(endDate);
return dates;
}
/**
* 将字符串解析成时间,如果无法解析返回null
*
* @param sdf
* @param dateStr
* @return
*/
public static Date parseDate(SimpleDateFormat sdf, String dateStr) {
try {
return sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static Date maxDate(Date date1, Date date2) {
if (date1.before(date2)) {
return date2;
} else {
return date1;
}
}
public static Date minDate(Date date1, Date date2) {
if (date1.before(date2)) {
return date1;
} else {
return date2;
}
}
/**
* 获取月的所有天数,不返回未到达的日期
*
* @param date
* @return
*/
public static List<Date> getMonthDyas(Date date) {
Date monthStart = getFirstDateOfMonth(date);
Date monthEnd = getLastDateOfMonth(date);
Date currentDate = new Date();
monthEnd = monthEnd.before(currentDate) ? monthEnd : currentDate;
List<Date> dates = getDaysBetweenStartDateAndEndDate(monthStart, monthEnd);
return dates;
}
public static long getDistanceSecond(Date date1, Date date2) {
return (date1.getTime() - date2.getTime()) / 1000;
}
public static long getDistanceMillisecond(Date date1, Date date2) {
return (date1.getTime() - date2.getTime());
}
/**
* 日期转星期
*
* @param datetime
* @return
*/
public static String dateToWeek(String datetime) {
SimpleDateFormat f = null;
if (datetime.contains("T")) {
f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
}else{
f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
}
String[] weekDays = { "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday" };
Calendar cal = Calendar.getInstance(); // 获得一个日历
Date datet = null;
try {
datet = f.parse(datetime);
cal.setTime(datet);
} catch (ParseException e) {
try {
datet = f.parse("yyyy-MM-dd'T'HH:mm:ss.SSS");
cal.setTime(datet);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
if (w < 0)
w = 0;
return weekDays[w];
}
/**
* 时区转换
* @param time 时间字符串
* @param pattern 格式 "yyyy-MM-dd HH:mm"
* @param nowTimeZone eg:+8,0,+9,-1 等等
* @param targetTimeZone 同nowTimeZone
* @return
*/
public static String timeZoneTransfer(String time, String pattern, String nowTimeZone, String targetTimeZone) {
if(StringUtils.isBlank(time)){
return "";
}
SimpleDateFormat simpleDateFormat = null;
if (time.contains("T")) {
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
}else{
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
}
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT" + nowTimeZone));
Date date=null;
try {
date = simpleDateFormat.parse(time);
} catch (ParseException e) {
return "";
}
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT" + targetTimeZone));
return simpleDateFormat.format(date);
}
public static int[] gethourminutesecond(String pattern,String baseTime){
try {
int[] times = new int[3];
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
calendar.setTime(sdf.parse(baseTime));
int hour = calendar.get(Calendar.HOUR_OF_DAY);
times[0]=hour;
int minute = calendar.get(Calendar.MINUTE);
times[1]=minute;
int second = calendar.get(Calendar.SECOND);
times[2]=second;
return times;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) throws ParseException {
/*Date date1 = new Date();
Date date2 = DateUtils.addSeconds(date1, 10);
System.out.println(Math.abs(getDistanceMillisecond(date2,date1)));*/
// SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
// Date date = format.parse("20170427105959");
// System.out.println(FORMAT_YYYY_MM_DD_HH_MM_SS.format(date));
// Date time = getLastWeek(new Date());
// String format = FORMAT_YYYY_MM_DD_HH_MM_SS_S_UTC.format(time);
// System.out.println(format);
/*String dd ="2017-03-21 12:11:56.123";
Date date = dateFormat("2018-01-29");
System.out.println(FORMAT_YYYY_MM_DD_HH_MM_SS_S_UTC.format(date));
System.out.println(FORMAT_YYYY_MM_DD_HH_MM_SS_SSS.format(date));
System.out.println(date.getTime());
long a=1490069516*1000l;
String s = longToString(a);
System.out.println(s);*/
/*String date_dt = "2018-10-07 16:01:00.000";
System.out.println(DateUtil.dateToWeek(date_dt));
System.out.println(date_dt.split(" ")[1]);
date_dt = timeZoneTransfer(date_dt,"yyyy-MM-dd HH:mm:ss.SSS", "0", "+8");
System.out.println(date_dt);
Calendar calendar = Calendar.getInstance();
Date HMTime = FORMAT_YYYY_MM_DD_HH_MM_SS_SSS.parse(date_dt);
calendar.setTime(HMTime);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int milliSecond = calendar.get(Calendar.MILLISECOND);
System.out.println(hour);
System.out.println(minute);
System.out.println(second);
System.out.println(milliSecond);
Date time = FORMAT_HHMMSS.parse("08:00:00");
HMTime = FORMAT_YYYY_MM_DD_HH_MM_SS_SSS.parse(date_dt);
calendar.setTime(time);
hour = calendar.get(Calendar.HOUR_OF_DAY);
minute = calendar.get(Calendar.MINUTE);
second = calendar.get(Calendar.SECOND);
System.out.println("=========");
System.out.println(hour);
System.out.println(minute);
System.out.println(second);
System.out.println(milliSecond);
Date d = new Date();
System.out.println(FORMAT_YYYY_MM_DD_HH_MM_SS_SSS.format(d));
Date nowdate = setDayHMS(d,"16:01:00");
System.out.println(FORMAT_YYYY_MM_DD_HH_MM_SS_SSS.format(nowdate));*/
System.out.println(gethourminutesecond("yyyy-MM-dd HH:mm:ss.SSS",timeZoneTransfer("2018-10-07 16:01:00.000","yyyy-MM-dd HH:mm:ss.SSS", "0", "+8")));
System.out.println(gethourminutesecond("HH:mm:ss","08:00:00"));
}
}