TimeUtils.java
30.2 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
package com.skua.modules.quartz.util;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
@SuppressWarnings({"rawtypes", "unchecked"})
public class TimeUtils {
public static final String DATE_FOREVER = "9999-12-31";
public static final String FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";
public static final String FORMAT_DATE_CN = "yyyy年MM月dd日";
public static final String FORMAT_TIMESTAMP = "yyyy-MM-dd HH:mm:ss:SSS";
public static final String FORMAT_DATETIME_HM = "yyyy-MM-dd HH:mm";
public static final String FORMAT_FULLTIME = "yyMMddHHmmssSSS";
public static final String FORMAT_DATE = "yyyy-MM-dd";
public static final String FORMAT_YEARMONTH = "yyyy-MM";
public static final String FORMAT_TIME = "HH:mm:ss";
public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
public static final String PATTERN_RFC1036 = "EEEE, dd-MMM-yy HH:mm:ss zzz";
public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy";
private static final Collection<String> DEFAULT_HTTP_CLIENT_PATTERNS = Arrays.asList(
new String[]{"EEE MMM d HH:mm:ss yyyy", "EEEE, dd-MMM-yy HH:mm:ss zzz", "EEE, dd MMM yyyy HH:mm:ss zzz"});
private static final Date DEFAULT_TWO_DIGIT_YEAR_START;
private static final TimeZone GMT;
public static final Collection<String> DEFAULT_DATE_FORMATS;
public static final TimeZone UTC = TimeZone.getTimeZone("UTC");
private static ThreadLocalDateFormat fmtThreadLocal = new ThreadLocalDateFormat();
public static int compareOnlyByTime(String firstTime, String secondTime) {
try {
String timeDelm = ":";
int pos = firstTime.indexOf(timeDelm);
int iFirst = Integer.parseInt(firstTime.substring(0, pos)) * 10000;
firstTime = firstTime.substring(pos + 1);
pos = firstTime.indexOf(timeDelm);
if (pos > 0) {
iFirst = iFirst + Integer.parseInt(firstTime.substring(0, pos)) * 100
+ Integer.parseInt(firstTime.substring(pos + 1));
} else {
iFirst += Integer.parseInt(firstTime) * 100;
}
pos = secondTime.indexOf(timeDelm);
int iSecond = Integer.parseInt(secondTime.substring(0, pos)) * 10000;
secondTime = secondTime.substring(pos + 1);
pos = secondTime.indexOf(timeDelm);
if (pos > 0) {
iSecond = iSecond + Integer.parseInt(secondTime.substring(0, pos)) * 100
+ Integer.parseInt(secondTime.substring(pos + 1));
} else {
iSecond += Integer.parseInt(secondTime) * 100;
}
if (iFirst == iSecond) return 0;
if (iFirst > iSecond) {
return 1;
}
return -1;
} catch (Exception e) {
}
return -2;
}
public static Calendar getCalendar(String dateString) {
Calendar calendar = Calendar.getInstance();
String[] items = dateString.split("[-| |:]");
int len = items.length;
if (len > 0) {
calendar.set(1, Integer.parseInt(items[0]));
}
if (len > 1)
calendar.set(2, Integer.parseInt(items[1]) - 1);
else {
calendar.set(2, 0);
}
if (len > 2)
calendar.set(5, Integer.parseInt(items[2]));
else {
calendar.set(5, 1);
}
if (len > 3)
calendar.set(11, Integer.parseInt(items[3]));
else {
calendar.set(11, 0);
}
if (len > 4)
calendar.set(12, Integer.parseInt(items[4]));
else {
calendar.set(12, 0);
}
if (len > 5)
calendar.set(13, Integer.parseInt(items[5]));
else {
calendar.set(13, 0);
}
return calendar;
}
public static String getCertainDate(int days) {
Calendar calendar = Calendar.getInstance();
calendar.add(5, days);
return getStringFromDate(calendar.getTime(), "yyyy-MM-dd");
}
public static String getCertainDate(String dateString, int days) {
Calendar calendar = getCalendar(dateString);
calendar.add(5, days);
return getStringFromDate(calendar.getTime(), "yyyy-MM-dd");
}
public static String getCertainDate(String dateString, int period, int periodType) {
Calendar calendar = getCalendar(dateString);
switch (periodType) {
case 1:
calendar.add(5, period);
break;
case 2:
calendar.add(2, period);
break;
case 3:
calendar.add(2, period * 12);
break;
}
return getStringFromDate(calendar.getTime(), "yyyy-MM-dd");
}
public static String getCertainDatetime(String datetime, int days) {
Date curDate = getDateFromString(datetime, "yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTime(curDate);
calendar.add(5, days);
return getStringFromDate(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
}
public static String getCertainMonth(int dif) {
Calendar calendar = Calendar.getInstance();
calendar.add(2, dif);
return getStringFromDate(calendar.getTime(), "yyyy-MM-dd");
}
public static String getCertainMonth(int dif, String format) {
Calendar calendar = Calendar.getInstance();
calendar.add(2, dif);
return getStringFromDate(calendar.getTime(), format);
}
public static String getChineseDate() {
return getChineseDate(getDate());
}
public static String getChineseDate(String date) {
if (date.length() < Integer.valueOf("10").intValue()) {
return "";
}
String year = date.substring(0, 4);
String month = date.substring(5, 7);
String day = date.substring(8, 10);
String y1 = year.substring(0, 1);
String y2 = year.substring(1, 2);
String y3 = year.substring(2, 3);
String y4 = year.substring(3, 4);
String m2 = month.substring(1, 2);
String d1 = day.substring(0, 1);
String d2 = day.substring(1, 2);
String cy1 = getChineseNum(y1);
String cy2 = getChineseNum(y2);
String cy3 = getChineseNum(y3);
String cy4 = getChineseNum(y4);
String cm2 = getChineseNum(m2);
String cd1 = getChineseNum(d1);
String cd2 = getChineseNum(d2);
String cYear = new StringBuilder().append(cy1).append(cy2).append(cy3).append(cy4).append("年").toString();
String cMonth = "月";
if (Integer.parseInt(month) > 9)
cMonth = new StringBuilder().append("十").append(cm2).append(cMonth).toString();
else {
cMonth = new StringBuilder().append(cm2).append(cMonth).toString();
}
String cDay = "日";
if (Integer.parseInt(day) > 9)
cDay = new StringBuilder().append(cd1).append("十").append(cd2).append(cDay).toString();
else {
cDay = new StringBuilder().append(cd2).append(cDay).toString();
}
String chineseday = new StringBuilder().append(cYear).append(cMonth).append(cDay).toString();
return chineseday;
}
public static String getChineseDayOfWeek() {
return getChineseDayOfWeek(getDate(), null);
}
public static String getChineseDayOfWeek(String strDate, Integer offset) {
Calendar calendar = getCalendar(strDate);
int week = offset == null ? calendar.get(7) : calendar.get(7) - offset.intValue();
if (week < 0) {
week = 0;
}
String strWeek = "";
switch (week) {
case 1:
strWeek = "星期日";
break;
case 2:
strWeek = "星期一";
break;
case 3:
strWeek = "星期二";
break;
case 4:
strWeek = "星期三";
break;
case 5:
strWeek = "星期四";
break;
case 6:
strWeek = "星期五";
break;
case 7:
strWeek = "星期六";
break;
default:
strWeek = "星期一";
}
return strWeek;
}
public static String getChineseNum(String number) {
String chinese = "";
int x = Integer.parseInt(number);
switch (x) {
case 0:
chinese = "零";
break;
case 1:
chinese = "一";
break;
case 2:
chinese = "二";
break;
case 3:
chinese = "三";
break;
case 4:
chinese = "四";
break;
case 5:
chinese = "五";
break;
case 6:
chinese = "六";
break;
case 7:
chinese = "七";
break;
case 8:
chinese = "八";
break;
case 9:
chinese = "九";
break;
}
return chinese;
}
public static String getChineseTwoDate(String date) {
if (date.length() < 10) {
return "";
}
String year = date.substring(0, 4);
String month = date.substring(5, 7);
String day = date.substring(8, 10);
return new StringBuilder().append(year).append("年").append(month).append("月").append(day).append("日")
.toString();
}
public static String getCustomDateTime(String customFormat) {
Calendar calendar = Calendar.getInstance();
return getStringFromDate(calendar.getTime(), customFormat);
}
public static String getDate() {
return getDate(Calendar.getInstance());
}
public static String getDate(Calendar calendar) {
return getStringFromDate(calendar.getTime(), "yyyy-MM-dd");
}
public static String getDateCN() {
return getStringFromDate(Calendar.getInstance().getTime(), "yyyy年MM月dd日");
}
public static String getDateCN(String dateStr) {
Date date = getDateFromString(dateStr);
return getStringFromDate(date, "yyyy年MM月dd日");
}
public static String getDateAdded(int addNum, String getDate) {
return getCertainDate(getDate, addNum);
}
public static Date getDateFromString(String s) {
int len = s.length();
String format;
switch (len) {
case 19:
format = "yyyy-MM-dd HH:mm:ss";
break;
case 24:
format = "yyyy-MM-dd HH:mm:ss:SSS";
break;
default:
format = "yyyy-MM-dd";
}
return getDateFromString(s, format);
}
public static Date getDateFromString(String s, String format) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.parse(s);
} catch (Exception e) {
}
return null;
}
public static String getDatetime() {
Calendar calendar = Calendar.getInstance();
return getStringFromDate(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
}
public static String getDatetime(long millTimes) {
return getStringFromDate(new Date(millTimes), "yyyy-MM-dd HH:mm:ss");
}
public static String getDatetime(long millTimes, String format) {
return getStringFromDate(new Date(millTimes), format);
}
public static String getDatetime(Date time) {
return getStringFromDate(time, "yyyy-MM-dd HH:mm:ss");
}
public static String getDatetime(Date time, String format) {
return getStringFromDate(time, format);
}
public static String getDateTimeHm() {
Calendar calendar = Calendar.getInstance();
return getStringFromDate(calendar.getTime(), "yyyy-MM-dd HH:mm");
}
public static String getDatetimeW3C() {
return new StringBuilder().append(getDate()).append("T").append(getTime()).toString();
}
public static String getDatetimeZd() {
Calendar calendar = Calendar.getInstance();
calendar.set(12, 0);
calendar.set(13, 0);
return getStringFromDate(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
}
public static int getDay() {
Calendar calendar = Calendar.getInstance();
return calendar.get(5);
}
public static long getDiffTime(String beginTime, String endTime) {
return getDiffTime(beginTime, endTime, "yyyy-MM-dd HH:mm:ss");
}
public static long getDiffTime(String beginTime, String endTime, String format) {
if (beginTime.length() == 0) {
beginTime = getDatetime();
}
if (endTime.length() == 0) {
endTime = getDatetime();
}
Date eTime = getDateFromString(endTime, format);
Date bTime = getDateFromString(beginTime, format);
return eTime.getTime() - bTime.getTime();
}
public static boolean isBefore(String begin, String end) {
Date beginDate = getDateFromString(begin);
Date endDate = getDateFromString(end);
return beginDate.before(endDate);
}
public static String getDiffString(String beginTime, String endTime) {
try {
if ((endTime == null) || (endTime.length() == 0)) {
endTime = getDatetime();
}
Date eTime = getDateFromString(endTime, "yyyy-MM-dd HH:mm:ss");
Date bTime = getDateFromString(beginTime, "yyyy-MM-dd HH:mm:ss");
long time = eTime.getTime() - bTime.getTime();
StringBuilder sb = new StringBuilder();
int day = (int) Math.floor(time / 86400000L);
if (day > 0) {
sb.append(day).append("天");
}
time %= 86400000L;
int hour = (int) Math.floor(time / 3600000L);
if (hour > 0) {
sb.append(hour).append("小时");
}
time %= 3600000L;
int minute = (int) Math.ceil(time / 60000L);
if (minute > 0) {
sb.append(minute).append("分钟");
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static String getFirstDateOfWeek() {
return getFirstDateOfWeek(getDate());
}
public static String getFirstDateOfWeek(String dateString) {
Calendar calendar = getCalendar(dateString);
int iCount;
if (calendar.get(7) == 1)
iCount = -6;
else {
iCount = 2 - calendar.get(7);
}
return getCertainDate(dateString, iCount);
}
public static String getFulltime() {
Calendar calendar = Calendar.getInstance();
return getStringFromDate(calendar.getTime(), "yyMMddHHmmssSSS");
}
public static int getMonth() {
Calendar calendar = Calendar.getInstance();
return calendar.get(2) + 1;
}
public static int getMonthDiff(String startDate, String endDate) {
String[] startArray = startDate.split("-");
String[] endArray = endDate.split("-");
int startYear = Integer.parseInt(startArray[0]);
int startMonth = Integer.parseInt(startArray[1]);
int endYear = Integer.parseInt(endArray[0]);
int endMonth = Integer.parseInt(endArray[1]);
return Math.abs((endYear - startYear) * 12 + endMonth - startMonth);
}
public static Date getWeekFirstDay(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
int weekDay = c.get(7) == 1 ? 8 : c.get(7);
c.add(5, 2 - weekDay);
Date start = c.getTime();
return start;
}
public static Date getMonthFirstDay(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
String month = String.valueOf(cal.get(2) + 1);
String year = String.valueOf(cal.get(1));
if (Integer.parseInt(month) < 10) {
month = new StringBuilder().append("0").append(month).toString();
}
String resultStr = new StringBuilder().append(year).append("-").append(month).append("-01").append(" 00:00:00")
.toString();
Date result = null;
result = getDateFromString(resultStr, "yyyy-MM-dd hh:mm:ss");
return result;
}
public static String getMonthFirstDay(String date) {
Calendar cal = getCalendar(date);
String month = String.valueOf(cal.get(2) + 1);
String year = String.valueOf(cal.get(1));
if (Integer.parseInt(month) < 10) {
month = new StringBuilder().append("0").append(month).toString();
}
return new StringBuilder().append(year).append("-").append(month).append("-01").toString();
}
public static String getMonthLastDay(String date) {
Calendar cal = getCalendar(date);
int month = cal.get(2) + 1;
int year = cal.get(1);
int nextMonth = month + 1;
int nextYear = year;
if (nextMonth == 13) {
nextMonth = 1;
nextYear += 1;
}
String nextMonthFirstDay = new StringBuilder().append(nextYear).append("-").append(nextMonth).append("-01")
.toString();
return getCertainDate(nextMonthFirstDay, -1);
}
/**
* 判断日期是否合法
* @param date yyyy-mm-dd
* @return
*/
public static Boolean isExistDate(String date) {
Boolean flag = true;
String str[] = date.split("-");
int year = Integer.parseInt(str[0]);
String month = str[1];
int day = Integer.parseInt(str[2]);
if (month.equals("04") || month.equals("06") || month.equals("09") || month.equals("11") || month.equals("4")
|| month.equals("6") || month.equals("9")) {
if (day > 30) {
flag = false;
}
} else if (month.equals("02") || month.equals("2")) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
if (day > 29) {
flag = false;
}
} else {
if (day > 28) {
flag = false;
}
}
} else {
if (day > 31) {
flag = false;
}
}
return flag;
}
public static String getStringFromDate(Date d, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(d);
}
public static String formatTime(Date d) {
return getStringFromDate(d, "HH:mm:ss");
}
public static String formatDate(Date d) {
return getStringFromDate(d, "yyyy-MM-dd");
}
public static String formatDatetime(Date d) {
return getStringFromDate(d, "yyyy-MM-dd HH:mm:ss");
}
public static String getTime() {
Calendar calendar = Calendar.getInstance();
return getStringFromDate(calendar.getTime(), "HH:mm:ss");
}
public static String getWorkDate(String date) {
Date curDate = getDateFromString(date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(curDate);
int week = calendar.get(7);
if (week == 7)
calendar.add(5, 2);
else if (week == 1) {
calendar.add(5, 1);
}
return getDate(calendar);
}
public static int getYear() {
Calendar calendar = Calendar.getInstance();
return calendar.get(1);
}
public static String getYearMonth() {
return getYearMonth(0);
}
public static String getYearMonth(int monthDiff) {
Calendar calendar = Calendar.getInstance();
calendar.set(2, calendar.get(2) + monthDiff);
return getStringFromDate(calendar.getTime(), "yyyy-MM");
}
public static int selectDateDiff(String dateinfo) {
return selectDateDiff(dateinfo, getDate());
}
public static int selectDateDiff(String first, String second) {
int dif = 0;
try {
Date fDate = getDateFromString(first, "yyyy-MM-dd");
Date sDate = getDateFromString(second, "yyyy-MM-dd");
dif = (int) ((fDate.getTime() - sDate.getTime()) / 86400000L);
} catch (Exception e) {
dif = 0;
}
return dif;
}
public static double getDiffHoure(String beginTime, String endTime) {
double dif = 0.0D;
try {
Date eDatetime = getDateFromString(endTime, "yyyy-MM-dd HH:mm:ss");
Date bDatetime = getDateFromString(beginTime, "yyyy-MM-dd HH:mm:ss");
double ed = eDatetime.getTime();
double sd = bDatetime.getTime();
BigDecimal et = new BigDecimal(ed / 1000.0D / 3600.0D - sd / 1000.0D / 3600.0D);
dif = et.setScale(2, 4).doubleValue();
} catch (Exception e) {
dif = 0.0D;
}
return dif;
}
public static String getDatetimeTS() {
Calendar calendar = Calendar.getInstance();
return getStringFromDate(calendar.getTime(), "yyyy-MM-dd HH:mm:ss:SSS");
}
/**
*
* @Title: getTimestamp
* @Description: 获取时间戳(符合本平台要求)
* @param date
* @return 时间戳
* @return: Long
*/
public static Long getTimestamp(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str_date = sdf.format(date);
Date datetime = null;
try {
datetime = sdf.parse(str_date);
} catch (ParseException e) {
e.printStackTrace();
}
Long time = datetime.getTime() / 1000;
return time;
}
/**
*
* @Title: getStringFromTimestamp
* @Description: 从时间戳获取字符串
* @param value 数据值
* @param format 格式化字符串
* @return 时间字符串
* @return: String
* @author yangboran
*/
public static String getStringFromTimestamp(long value, String format) {
return getStringFromDate(new Date(value), format);
}
/**
*
* @Title: getStringFromTimestamp
* @Description: 从时间戳获取字符串
* @param value 数据值
* @return 时间字符串
* @return: String
* @author yangboran
*/
public static String getStringFromTimestamp(long value) {
return getStringFromDate(new Date(value), "yyyy-MM-dd HH:mm:ss");
}
public static Timestamp getTimestamp() {
return new Timestamp(System.currentTimeMillis());
}
public static Timestamp getTimestamp(String timestamp) {
return new Timestamp(getDateFromString(timestamp, "yyyy-MM-dd HH:mm:ss:SSS").getTime());
}
public static String getByTimestamp(Timestamp tm) {
if (tm == null) {
return "";
}
Date date = new Date(tm.getTime());
return getStringFromDate(date, "yyyy-MM-dd HH:mm:ss:SSS");
}
public static Date parseDate(String d) throws ParseException {
return parseDate(d, DEFAULT_DATE_FORMATS);
}
public static Date parseDate(String d, Collection<String> fmts) throws ParseException {
if ((d.endsWith("Z")) && (d.length() > 20)) {
return getThreadLocalDateFormat().parse(d);
}
return parseDate(d, fmts, null);
}
public static String getUTCTime(Date date) throws IOException {
StringBuilder dateBuilder = new StringBuilder();
formatDate(date, null, dateBuilder);
return dateBuilder.toString();
}
@SuppressWarnings("unused")
public static Date parseDate(String dateValue, Collection<String> dateFormats, Date startDate)
throws ParseException {
if (dateValue == null) {
throw new IllegalArgumentException("dateValue is null");
}
if (dateFormats == null) {
dateFormats = DEFAULT_HTTP_CLIENT_PATTERNS;
}
if (startDate == null) {
startDate = DEFAULT_TWO_DIGIT_YEAR_START;
}
if ((dateValue.length() > 1) && (dateValue.startsWith("'")) && (dateValue.endsWith("'"))) {
dateValue = dateValue.substring(1, dateValue.length() - 1);
}
SimpleDateFormat dateParser = null;
Iterator formatIter = dateFormats.iterator();
if (formatIter.hasNext()) {
String format = (String) formatIter.next();
if (dateParser == null) {
dateParser = new SimpleDateFormat(format, Locale.US);
dateParser.setTimeZone(GMT);
dateParser.set2DigitYearStart(startDate);
} else {
dateParser.applyPattern(format);
}
return dateParser.parse(dateValue);
}
throw new ParseException(new StringBuilder().append("Unable to parse the date ").append(dateValue).toString(),
0);
}
public static DateFormat getThreadLocalDateFormat() {
return (DateFormat) fmtThreadLocal.get();
}
private static Calendar formatDate(Date date, Calendar cal, Appendable out) throws IOException {
StringBuilder sb = (out instanceof StringBuilder) ? (StringBuilder) out : new StringBuilder();
if (cal == null) {
cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.US);
}
cal.setTime(date);
int i = cal.get(1);
sb.append(i);
sb.append('-');
i = cal.get(2) + 1;
if (i < 10) {
sb.append('0');
}
sb.append(i);
sb.append('-');
i = cal.get(5);
if (i < 10) {
sb.append('0');
}
sb.append(i);
sb.append('T');
i = cal.get(11);
if (i < 10) {
sb.append('0');
}
sb.append(i);
sb.append(':');
i = cal.get(12);
if (i < 10) {
sb.append('0');
}
sb.append(i);
sb.append(':');
i = cal.get(13);
if (i < 10) {
sb.append('0');
}
sb.append(i);
i = cal.get(14);
if (i != 0) {
sb.append('.');
if (i < 100) {
sb.append('0');
}
if (i < 10) {
sb.append('0');
}
sb.append(i);
int lastIdx = sb.length() - 1;
if (sb.charAt(lastIdx) == '0') {
lastIdx--;
if (sb.charAt(lastIdx) == '0') {
lastIdx--;
}
sb.setLength(lastIdx + 1);
}
}
sb.append('Z');
if (out != sb) {
out.append(sb);
}
return cal;
}
public static Date createDate() {
return Calendar.getInstance().getTime();
}
private static Date add(Date date, int calendarField, int amount) {
if (date == null) {
throw new IllegalArgumentException("The date must not be null");
}
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(calendarField, amount);
return c.getTime();
}
public static Date addMinutes(Date date, int amount) {
return add(date, 12, amount);
}
public static Date addHours(Date date, int amount) {
return add(date, 11, amount);
}
public static Date addDays(Date date, int amount) {
return add(date, 5, amount);
}
public static Date addWeeks(Date date, int amount) {
return add(date, 3, amount);
}
public static Date addMonths(Date date, int amount) {
return add(date, 2, amount);
}
public static Date addYears(Date date, int amount) {
return add(date, 1, amount);
}
public static int getDayOfMonth(int month, int year) {
int days = 0;
if (month == 2) {
if (year % 4 != 0) {
days = 28;
} else if ((year % 100 == 0) && (year % 400 != 0))
days = 28;
else {
days = 29;
}
} else {
switch (month) {
case 1:
days = 31;
break;
case 3:
days = 31;
break;
case 4:
days = 30;
break;
case 5:
days = 31;
break;
case 6:
days = 30;
break;
case 7:
days = 31;
break;
case 8:
days = 31;
break;
case 9:
days = 30;
break;
case 10:
days = 31;
break;
case 11:
days = 30;
break;
case 12:
days = 31;
break;
case 2:
}
}
return days;
}
static {
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.US);
calendar.set(2000, 0, 1, 0, 0);
DEFAULT_TWO_DIGIT_YEAR_START = calendar.getTime();
GMT = TimeZone.getTimeZone("GMT");
DEFAULT_DATE_FORMATS = new ArrayList();
DEFAULT_DATE_FORMATS.add("yyyy-MM-dd'T'HH:mm:ss'Z'");
DEFAULT_DATE_FORMATS.add("yyyy-MM-dd'T'HH:mm:ss");
DEFAULT_DATE_FORMATS.add("yyyy-MM-dd");
DEFAULT_DATE_FORMATS.add("yyyy-MM-dd hh:mm:ss");
DEFAULT_DATE_FORMATS.add("yyyy-MM-dd HH:mm:ss");
DEFAULT_DATE_FORMATS.add("EEE MMM d hh:mm:ss z yyyy");
DEFAULT_DATE_FORMATS.addAll(DEFAULT_HTTP_CLIENT_PATTERNS);
}
private static class ThreadLocalDateFormat extends ThreadLocal<DateFormat> {
private DateFormat proto;
public ThreadLocalDateFormat() {
SimpleDateFormat tmp = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
tmp.setTimeZone(TimeUtils.UTC);
this.proto = tmp;
}
protected DateFormat initialValue() {
return (DateFormat) this.proto.clone();
}
}
// 获得本周一 年月日
public static Date getTimesWeekmorning() {
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return cal.getTime();
}
// 获得本周日年月日
public static Date getTimesWeeknight() {
Calendar cal = Calendar.getInstance();
cal.setTime(getTimesWeekmorning());
cal.add(Calendar.DAY_OF_WEEK, 7);
return cal.getTime();
}
// 获得本月第一天年月日
public static Date getTimesMonthmorning() {
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
return cal.getTime();
}
// 获得本月最后一天年月日
public static Date getTimesMonthnight() {
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
cal.set(Calendar.HOUR_OF_DAY, 24);
return cal.getTime();
}
/**
* 获取当年的第一天 年月日
* @param year
* @return
*/
public static Date getCurrYearFirst() {
Calendar currCal = Calendar.getInstance();
int currentYear = currCal.get(Calendar.YEAR);
/**
* 获取某年第一天日期
* @param year 年份
*/
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, currentYear);
Date currYearFirst = calendar.getTime();
return currYearFirst;
}
/**
* 获取昨天日期
* @return
*/
public static Date yesterDay() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
return cal.getTime();
}
/**
*
* @Title ------- getNowAfterYear
* @Description ------- 获取本年月(包含)到过去时间为一年的年月
* @Company ------- Copyright © 2017北京金控数据 All rights reserved
* @Author ------- WangXD
* @Date ------- 2017年1月3日 下午6:41:27
* @param date yyyy-mm
* @return ------- String yyyy-mm
*
*/
public static String getNowAfterYear(String date) {
int year = Integer.parseInt(date.substring(0, 4));
int month = Integer.parseInt(date.substring(5, 7));
String start = date;
if (month < 12) {
year = year - 1;
month = month + 1;
if (month > 12) {
month--;
}
} else {
month = 12 - month + 1;
}
start = String.valueOf(year) + "-" + String.valueOf(month);
if (month < 10) {
start = String.valueOf(year) + "-0" + String.valueOf(month);
}
return start;
}
/**
* 时间戳转时间
* @param time
* @param formart
* @return
*/
public static String TimeToString(Double time, String formart) {
SimpleDateFormat sdf = new SimpleDateFormat(formart);
String date = sdf.format(new Date((long) (time * 1000L)));
return date;
}
public static String timeStampToDate(long value, String format) {
// log.info("date=" + value + "format=" + format);
SimpleDateFormat sdf = new SimpleDateFormat(format);
String sd = sdf.format(new Date(value * 1000));
return sd;
}
public static String dateTostring(Date date, String format) {
if (format.equals("")) {
format = FORMAT_DATE;
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
String str = sdf.format(date);
return str;
}
public static Long dateToTimeStamp(String dateStr, String format) {
if (format.equals("")) {
format = "yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date datetime = null;
try {
datetime = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
Long time = datetime.getTime() / 1000;
return time;
}
/**
* 两个日期之间的相差天数
* @Title: getDayDiff
* @param startDate
* @param endDate
* @return
* @return: int
*/
public static int getDayDiff(String startDate, String endDate) {
int days = 0;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(startDate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(endDate));
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
days = Integer.parseInt(String.valueOf(between_days));
} catch (ParseException e) {
e.printStackTrace();
}
return days;
}
/**
* 计算两个时间相差多少个年
*
* @param early
* @param late
* @return
* @throws ParseException
*/
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
private static Calendar startDate = Calendar.getInstance();
private static Calendar endDate = Calendar.getInstance();
public static int yearsBetween(String start, String end) throws ParseException {
startDate.setTime(sdf.parse(start));
endDate.setTime(sdf.parse(end));
return (endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR) + 1);
}
// 获得当年第一天
public static String getYearFirstDay(String date) throws ParseException {
startDate.setTime(sdf.parse(date.substring(0, 4) + "-01-01"));
return startDate.get(Calendar.YEAR) + "-01-01";
}
// 获得当年最后一天
public static String getYearLastDay(String date) throws ParseException {
endDate.setTime(sdf.parse(date.substring(0, 4) + "-12-31"));
return endDate.get(Calendar.YEAR) + "-12-31";
}
}