-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateUtils.java
More file actions
executable file
·739 lines (667 loc) · 19.3 KB
/
DateUtils.java
File metadata and controls
executable file
·739 lines (667 loc) · 19.3 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
package com.youth.xframe.utils;
import com.youth.xframe.entity.DateDifference;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
/**
* 日期操作工具类.
* SimpleDateFormat函数语法:
* G 年代标志符
* y 年
* M 月
* d 日
* h 时 在上午或下午 (1~12)
* H 时 在一天中 (0~23)
* m 分
* s 秒
* S 毫秒
* E 星期
* D 一年中的第几天
* F 一月中第几个星期几
* w 一年中第几个星期
* W 一月中第几个星期
* a 上午 / 下午 标记符
* k 时 在一天中 (1~24)
* K 时 在上午或下午 (0~11)
* z 时区
*/
public class XDateUtils {
private XDateUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* 秒与毫秒的倍数
*/
public static final long SEC = 1000;
/**
* 分与毫秒的倍数
*/
public static final long MIN = SEC * 60;
/**
* 时与毫秒的倍数
*/
public static final long HOUR = MIN * 60;
/**
* 天与毫秒的倍数
*/
public static final long DAY = HOUR * 24;
/**
* 周与毫秒的倍数
*/
public static final long WEEK = DAY * 7;
/**
* 月与毫秒的倍数
*/
public static final long MONTH = DAY * 30;
/**
* 年与毫秒的倍数
*/
public static final long YEAR = DAY * 365;
/**
* 默认格式
*/
public static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
public static final String PATTERN_UTC = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
/**
* SimpleDateFormat不是线程安全的,以下是线程安全实例化操作
*/
private static final ThreadLocal<SimpleDateFormat> local = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat();
}
};
/**
* 获取SimpleDateFormat实例
*
* @param pattern 模式串
* @return
*/
public static SimpleDateFormat getSimpleDateFormat(String pattern) {
SimpleDateFormat format = local.get();
format.applyPattern(pattern);
return format;
}
/**
* 获取当前时间的字符串
*
* @return
*/
public static String getCurrentDate() {
return format(new Date(), DEFAULT_PATTERN);
}
/**
* 获取表示当前时间的字符串
*
* @param pattern 模式串
* @return
*/
public static String getCurrentDate(String pattern) {
return format(new Date(), pattern);
}
/**
* 日期时间格式化
*
* @param date Date
* @return
*/
public static String format(Date date) {
SimpleDateFormat format = getSimpleDateFormat(DEFAULT_PATTERN);
return format.format(date);
}
/**
* 日期时间格式化
*
* @param date Date
* @param pattern 模式串
* @return
*/
public static String format(Date date, String pattern) {
SimpleDateFormat format = getSimpleDateFormat(pattern);
return format.format(date);
}
/**
* 将时间戳转为时间字符串
* <p>格式为yyyy-MM-dd HH:mm:ss</p>
*
* @param millis 毫秒时间戳
* @return 时间字符串
*/
public static String millis2String(long millis) {
SimpleDateFormat format = getSimpleDateFormat(DEFAULT_PATTERN);
return format.format(new Date(millis));
}
/**
* 将时间戳转为时间字符串
* <p>格式为pattern</p>
*
* @param millis 毫秒时间戳
* @param pattern 时间格式
* @return 时间字符串
*/
public static String millis2String(long millis, String pattern) {
SimpleDateFormat format = getSimpleDateFormat(pattern);
return format.format(new Date(millis));
}
/**
* 将时间字符串转为时间戳
* <p>time格式为yyyy-MM-dd HH:mm:ss</p>
*
* @param time 时间字符串
* @return 毫秒时间戳
*/
public static long string2Millis(String time) {
return string2Millis(time, DEFAULT_PATTERN);
}
/**
* 将时间字符串转为时间戳
* <p>time格式为pattern</p>
*
* @param time 时间字符串
* @param pattern 时间格式
* @return 毫秒时间戳
*/
public static long string2Millis(String time, String pattern) {
SimpleDateFormat format = getSimpleDateFormat(pattern);
try {
return format.parse(time).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return -1;
}
/**
* 将时间字符串转为Date类型
* <p>time格式为yyyy-MM-dd HH:mm:ss</p>
*
* @param time 时间字符串
* @return Date类型
*/
public static Date string2Date(String time) {
return string2Date(time, DEFAULT_PATTERN);
}
/**
* 将时间字符串转为Date类型
* <p>time格式为pattern</p>
*
* @param time 时间字符串
* @param pattern 时间格式
* @return Date类型
*/
public static Date string2Date(String time, String pattern) {
return new Date(string2Millis(time, pattern));
}
/**
* 将时间字符串转为Date类型
* <p>time格式为UTC</p>
*
* @param time 时间字符串
* @return
*/
public static Date utcString2Date(String time) throws ParseException {
SimpleDateFormat format = getSimpleDateFormat(PATTERN_UTC);
format.setTimeZone(TimeZone.getTimeZone("GMT"));
return format.parse(time);
}
/**
* 将Date类型转为时间字符串
* <p>格式为yyyy-MM-dd HH:mm:ss</p>
*
* @param date Date类型时间
* @return 时间字符串
*/
public static String date2String(Date date) {
return date2String(date, DEFAULT_PATTERN);
}
/**
* 将Date类型转为时间字符串
* <p>格式为pattern</p>
*
* @param date Date类型时间
* @param pattern 时间格式
* @return 时间字符串
*/
public static String date2String(Date date, String pattern) {
SimpleDateFormat format = getSimpleDateFormat(pattern);
return format.format(date);
}
/**
* 将Date类型转为时间戳
*
* @param date Date类型时间
* @return 毫秒时间戳
*/
public static long date2Millis(Date date) {
return date.getTime();
}
/**
* 将时间戳转为Date类型
*
* @param millis 毫秒时间戳
* @return Date类型时间
*/
public static Date millis2Date(long millis) {
return new Date(millis);
}
/**
* 获取与当前时间的时间差
*
* @param date 需要计算的时间,应小于当前时间
* @return DateDifference实体类,内封装有获取相差的毫秒、秒、分钟、小时、天的方法
*/
public static DateDifference getTwoDataDifference(Date date) {
return getTwoDataDifference(new Date(), date);
}
/**
* 获取与当前时间的时间差
*
* @param str 需要计算的时间,应小于当前时间
* @return DateDifference实体类,内封装有获取相差的毫秒、秒、分钟、小时、天的方法
*/
public static DateDifference getTwoDataDifference(String str) {
return getTwoDataDifference(new Date(), string2Date(str));
}
/**
* 得到二个日期间的时间差
*
* @param str1 两个时间中较大的那个
* @param str2 两个时间中较小的那个
* @return DateDifference实体类,内封装有获取相差的毫秒、秒、分钟、小时、天的方法
*/
public static DateDifference getTwoDataDifference(String str1, String str2) {
return getTwoDataDifference(string2Date(str1), string2Date(str2));
}
/**
* 得到二个日期间的时间差
*
* @param date1 两个时间中较大的那个
* @param date2 两个时间中较小的那个
* @return DateDifference实体类,内封装有获取相差的毫秒、秒、分钟、小时、天的方法
*/
public static DateDifference getTwoDataDifference(Date date1, Date date2) {
DateDifference difference = new DateDifference();
long millis = date1.getTime() - date2.getTime();
difference.setMillisecond(millis);
difference.setSecond(millis/SEC);
difference.setMinute(millis/MIN);
difference.setHour(millis/HOUR);
difference.setDay(millis/DAY);
return difference;
}
/**
* 判断是否同一天
* <p>time格式为yyyy-MM-dd HH:mm:ss</p>
*
* @param time 时间字符串
* @return {@code true}: 是<br>{@code false}: 否
*/
public static boolean isSameDay(String time) {
return isSameDay(string2Millis(time, DEFAULT_PATTERN));
}
/**
* 判断是否同一天
* <p>time格式为pattern</p>
*
* @param time 时间字符串
* @param pattern 时间格式
* @return {@code true}: 是<br>{@code false}: 否
*/
public static boolean isSameDay(String time, String pattern) {
return isSameDay(string2Millis(time, pattern));
}
/**
* 判断是否同一天
*
* @param date Date类型时间
* @return {@code true}: 是<br>{@code false}: 否
*/
public static boolean isSameDay(Date date) {
return isSameDay(date.getTime());
}
/**
* 判断是否同一天
*
* @param millis 毫秒时间戳
* @return {@code true}: 是<br>{@code false}: 否
*/
public static boolean isSameDay(long millis) {
long wee = (System.currentTimeMillis() / DAY) * DAY;
return millis >= wee && millis < wee + DAY;
}
/**
* 判断是否闰年
* <p>time格式为yyyy-MM-dd HH:mm:ss</p>
*
* @param time 时间字符串
* @return {@code true}: 闰年<br>{@code false}: 平年
*/
public static boolean isLeapYear(String time) {
return isLeapYear(string2Date(time, DEFAULT_PATTERN));
}
/**
* 判断是否闰年
* <p>time格式为pattern</p>
*
* @param time 时间字符串
* @param pattern 时间格式
* @return {@code true}: 闰年<br>{@code false}: 平年
*/
public static boolean isLeapYear(String time, String pattern) {
return isLeapYear(string2Date(time, pattern));
}
/**
* 判断是否闰年
*
* @param date Date类型时间
* @return {@code true}: 闰年<br>{@code false}: 平年
*/
public static boolean isLeapYear(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int year = cal.get(Calendar.YEAR);
return isLeapYear(year);
}
/**
* 判断是否闰年
*
* @param millis 毫秒时间戳
* @return {@code true}: 闰年<br>{@code false}: 平年
*/
public static boolean isLeapYear(long millis) {
return isLeapYear(millis2Date(millis));
}
/**
* 判断是否闰年
*
* @param year 年份
* @return {@code true}: 闰年<br>{@code false}: 平年
*/
public static boolean isLeapYear(int year) {
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
/**
* 获取星期
* <p>time格式为yyyy-MM-dd HH:mm:ss</p>
*
* @param time 时间字符串
* @return 星期
*/
public static String getWeek(String time) {
return getWeek(string2Date(time, DEFAULT_PATTERN));
}
/**
* 获取星期
* <p>time格式为pattern</p>
*
* @param time 时间字符串
* @param pattern 时间格式
* @return 星期
*/
public static String getWeek(String time, String pattern) {
return getWeek(string2Date(time, pattern));
}
/**
* 获取星期
*
* @param date Date类型时间
* @return 星期
*/
public static String getWeek(Date date) {
return new SimpleDateFormat("EEEE", Locale.getDefault()).format(date);
}
/**
* 获取星期
*
* @param millis 毫秒时间戳
* @return 星期
*/
public static String getWeek(long millis) {
return getWeek(new Date(millis));
}
/**
* 获取星期
* <p>注意:周日的Index才是1,周六为7</p>
* <p>time格式为yyyy-MM-dd HH:mm:ss</p>
*
* @param time 时间字符串
* @return 1...5
*/
public static int getWeekIndex(String time) {
return getWeekIndex(string2Date(time, DEFAULT_PATTERN));
}
/**
* 获取星期
* <p>注意:周日的Index才是1,周六为7</p>
* <p>time格式为pattern</p>
*
* @param time 时间字符串
* @param pattern 时间格式
* @return 1...7
*/
public static int getWeekIndex(String time, String pattern) {
return getWeekIndex(string2Date(time, pattern));
}
/**
* 获取星期
* <p>注意:周日的Index才是1,周六为7</p>
*
* @param date Date类型时间
* @return 1...7
*/
public static int getWeekIndex(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.DAY_OF_WEEK);
}
/**
* 获取星期
* <p>注意:周日的Index才是1,周六为7</p>
*
* @param millis 毫秒时间戳
* @return 1...7
*/
public static int getWeekIndex(long millis) {
return getWeekIndex(millis2Date(millis));
}
/**
* 获取月份中的第几周
* <p>注意:国外周日才是新的一周的开始</p>
* <p>time格式为yyyy-MM-dd HH:mm:ss</p>
*
* @param time 时间字符串
* @return 1...5
*/
public static int getWeekOfMonth(String time) {
return getWeekOfMonth(string2Date(time, DEFAULT_PATTERN));
}
/**
* 获取月份中的第几周
* <p>注意:国外周日才是新的一周的开始</p>
* <p>time格式为pattern</p>
*
* @param time 时间字符串
* @param pattern 时间格式
* @return 1...5
*/
public static int getWeekOfMonth(String time, String pattern) {
return getWeekOfMonth(string2Date(time, pattern));
}
/**
* 获取月份中的第几周
* <p>注意:国外周日才是新的一周的开始</p>
*
* @param date Date类型时间
* @return 1...5
*/
public static int getWeekOfMonth(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.WEEK_OF_MONTH);
}
/**
* 获取月份中的第几周
* <p>注意:国外周日才是新的一周的开始</p>
*
* @param millis 毫秒时间戳
* @return 1...5
*/
public static int getWeekOfMonth(long millis) {
return getWeekOfMonth(millis2Date(millis));
}
/**
* 获取年份中的第几周
* <p>注意:国外周日才是新的一周的开始</p>
* <p>time格式为yyyy-MM-dd HH:mm:ss</p>
*
* @param time 时间字符串
* @return 1...54
*/
public static int getWeekOfYear(String time) {
return getWeekOfYear(string2Date(time, DEFAULT_PATTERN));
}
/**
* 获取年份中的第几周
* <p>注意:国外周日才是新的一周的开始</p>
* <p>time格式为pattern</p>
*
* @param time 时间字符串
* @param pattern 时间格式
* @return 1...54
*/
public static int getWeekOfYear(String time, String pattern) {
return getWeekOfYear(string2Date(time, pattern));
}
/**
* 获取年份中的第几周
* <p>注意:国外周日才是新的一周的开始</p>
*
* @param date Date类型时间
* @return 1...54
*/
public static int getWeekOfYear(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.WEEK_OF_YEAR);
}
/**
* 获取年份中的第几周
* <p>注意:国外周日才是新的一周的开始</p>
*
* @param millis 毫秒时间戳
* @return 1...54
*/
public static int getWeekOfYear(long millis) {
return getWeekOfYear(millis2Date(millis));
}
private static final String[] CHINESE_ZODIAC = {"猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊"};
/**
* 获取生肖
* <p>time格式为yyyy-MM-dd HH:mm:ss</p>
*
* @param time 时间字符串
* @return 生肖
*/
public static String getChineseZodiac(String time) {
return getChineseZodiac(string2Date(time, DEFAULT_PATTERN));
}
/**
* 获取生肖
* <p>time格式为pattern</p>
*
* @param time 时间字符串
* @param pattern 时间格式
* @return 生肖
*/
public static String getChineseZodiac(String time, String pattern) {
return getChineseZodiac(string2Date(time, pattern));
}
/**
* 获取生肖
*
* @param date Date类型时间
* @return 生肖
*/
public static String getChineseZodiac(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return CHINESE_ZODIAC[cal.get(Calendar.YEAR) % 12];
}
/**
* 获取生肖
*
* @param millis 毫秒时间戳
* @return 生肖
*/
public static String getChineseZodiac(long millis) {
return getChineseZodiac(millis2Date(millis));
}
/**
* 获取生肖
*
* @param year 年
* @return 生肖
*/
public static String getChineseZodiac(int year) {
return CHINESE_ZODIAC[year % 12];
}
private static final String[] ZODIAC = {"水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座"};
private static final int[] ZODIAC_FLAGS = {20, 19, 21, 21, 21, 22, 23, 23, 23, 24, 23, 22};
/**
* 获取星座
* <p>time格式为yyyy-MM-dd HH:mm:ss</p>
*
* @param time 时间字符串
* @return 生肖
*/
public static String getZodiac(String time) {
return getZodiac(string2Date(time, DEFAULT_PATTERN));
}
/**
* 获取星座
* <p>time格式为pattern</p>
*
* @param time 时间字符串
* @param pattern 时间格式
* @return 生肖
*/
public static String getZodiac(String time, String pattern) {
return getZodiac(string2Date(time, pattern));
}
/**
* 获取星座
*
* @param date Date类型时间
* @return 星座
*/
public static String getZodiac(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
return getZodiac(month, day);
}
/**
* 获取星座
*
* @param millis 毫秒时间戳
* @return 星座
*/
public static String getZodiac(long millis) {
return getZodiac(millis2Date(millis));
}
/**
* 获取星座
*
* @param month 月
* @param day 日
* @return 星座
*/
public static String getZodiac(int month, int day) {
return ZODIAC[day >= ZODIAC_FLAGS[month - 1]
? month - 1
: (month + 10) % 12];
}
}