DateTimeUtil.java
21.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
package com.cnlive.share.util;
import android.annotation.SuppressLint;
import android.util.Log;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 时间、日期工具类
* Created by fanji on 2017/7/19.
*/
public class DateTimeUtil {
static SimpleDateFormat format;
/**
* 日期格式:yyyy-MM-dd HH:mm:ss
**/
public static final String DF_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
/**
* 日期格式:yyyy-MM-dd HH:mm
**/
public static final String DF_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
// yyyy-MM-dd-HH-mm
/**
* 日期格式:yyyy-MM-dd
**/
public static final String DF_YYYY_MM_DD = "yyyy-MM-dd";
/**
* 日期格式:HH:mm:ss
**/
public static final String DF_HH_MM_SS = "HH:mm:ss";
/**
* 日期格式:HH:mm
**/
public static final String DF_HH_MM = "HH:mm";
private final static long minute = 60 * 1000;// 1分钟
private final static long hour = 60 * minute;// 1小时
private final static long day = 24 * hour;// 1天
private final static long month = 31 * day;// 月
private final static long year = 12 * month;// 年
public DateTimeUtil() {
}
/**
* 将日期格式化成友好的字符串:几分钟前、几小时前、几天前、几月前、几年前、刚刚
*
* @param date
* @return
*/
public static String formatFriendly(Date date) {
if (date == null) {
return null;
}
long diff = new Date().getTime() - date.getTime();
long r = 0;
if (diff > year) {
r = (diff / year);
return r + "年前";
}
if (diff > month) {
r = (diff / month);
return r + "个月前";
}
if (diff > day) {
r = (diff / day);
return r + "天前";
}
if (diff > hour) {
r = (diff / hour);
return r + "个小时前";
}
if (diff > minute) {
r = (diff / minute);
return r + "分钟前";
}
return "刚刚";
}
/**
* 生活圈模仿微信时间展示样式
*
* @param date
* @return
*/
public static String formatNewFriendly(Date date) {
if (date == null) {
return null;
}
long diff = new Date().getTime() - date.getTime();
long r = 0;
if (diff > day) {
r = (diff / day);
if (r==1){
return"昨天";
}else {
return r + "天前";
}
}
if (diff > hour) {
r = (diff / hour);
return r + "小时前";
}
if (diff > minute) {
r = (diff / minute);
return r + "分钟前";
}
return "1分钟前";
}
/**
* 将日期以yyyy-MM-dd HH:mm:ss格式化
*
* @param dateL 日期
* @return
*/
@SuppressLint("SimpleDateFormat")
public static String formatDateTime(long dateL) {
SimpleDateFormat sdf = new SimpleDateFormat(DF_YYYY_MM_DD_HH_MM_SS);
Date date = new Date(dateL);
return sdf.format(date);
}
/**
* 将日期以yyyy-MM-dd HH:mm:ss格式化
*
* @param dateL 日期
* @return
*/
@SuppressLint("SimpleDateFormat")
public static String formatDateTime(long dateL, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date date = new Date(dateL);
return sdf.format(date);
}
/**
* 将日期以yyyy-MM-dd HH:mm:ss格式化
*
* @param date 日期
* @param formater
* @return
*/
@SuppressLint("SimpleDateFormat")
public static String formatDateTime(Date date, String formater) {
SimpleDateFormat sdf = new SimpleDateFormat(formater);
return sdf.format(date);
}
/**
* 将日期字符串转成日期
*
* @param strDate 字符串日期
* @return java.util.date日期类型
*/
@SuppressLint("SimpleDateFormat")
public static Date parseDate(String strDate) {
DateFormat dateFormat = new SimpleDateFormat(DF_YYYY_MM_DD_HH_MM_SS);
Date returnDate = null;
try {
returnDate = dateFormat.parse(strDate);
} catch (ParseException e) {
}
return returnDate;
}
/**
* 获取系统当前日期
*
* @return
*/
public static Date gainCurrentDate() {
return new Date();
}
/**
* 验证日期是否比当前日期早
*
* @param target1 比较时间1
* @param target2 比较时间2
* @return true 则代表target1比target2晚或等于target2,否则比target2早
*/
public static boolean compareDate(Date target1, Date target2) {
boolean flag = false;
try {
String target1DateTime = formatDateTime(target1, DF_YYYY_MM_DD_HH_MM_SS);
String target2DateTime = formatDateTime(target2, DF_YYYY_MM_DD_HH_MM_SS);
if (target1DateTime.compareTo(target2DateTime) <= 0) {
flag = true;
}
} catch (Exception e) {
System.out.println("比较失败,原因:" + e.getMessage());
}
return flag;
}
/**
* 对日期进行增加操作
*
* @param target 需要进行运算的日期
* @param hour 小时
* @return
*/
public static Date addDateTime(Date target, double hour) {
if (null == target || hour < 0) {
return target;
}
return new Date(target.getTime() + (long) (hour * 60 * 60 * 1000));
}
/**
* 对日期进行相减操作
*
* @param target 需要进行运算的日期
* @param hour 小时
* @return
*/
public static Date subDateTime(Date target, double hour) {
if (null == target || hour < 0) {
return target;
}
return new Date(target.getTime() - (long) (hour * 60 * 60 * 1000));
}
/**
* 获取系统时间的方法:月/日 时:分:秒
*/
public static String getFormateDate() {
Calendar calendar = Calendar.getInstance();
int month = (calendar.get(Calendar.MONTH) + 1);
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
String systemTime = (month < 10 ? "0" + month : month) + "/" + (day < 10 ? "0" + day : day) + " " + (hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute) + ":" + (second < 10 ? "0" + second : second);
return systemTime;
}
/**
* 获取系统时间的方法:时:分:秒
*/
public static String getHourAndMinute() {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
return (hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute);
}
/**
* 获取分钟
*/
public static int getMinute() {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
return minute;
}
/**
* 获取系统时间的方法:时
*/
public static String getHour() {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
return ((hour < 10 ? "0" + hour : hour) + "");
}
/**
* 将2017-07-10 00:00:00 换2017-07-10
*
* @param strDate
* @return
*/
public static String strFormatStr(String strDate) {
if (strDate.equals("")) {
return "";
}
return dateToStr(strToDate(strDate));
}
/**
* 2015-01-07 15:05:34
*
* @param strDate
* @return
*/
@SuppressLint("SimpleDateFormat")
public static Date strToDateHHMMSS(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
/**
* 2015-01-07 15:05
*
* @param strDate
* @return
*/
@SuppressLint("SimpleDateFormat")
public static Date strToDateHHMM(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
/**
* 2015-01-07
*
* @param strDate
* @return
*/
@SuppressLint("SimpleDateFormat")
public static Date strToDate(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
/**
* 2015.01.07
*
* @param strDate
* @return
*/
@SuppressLint("SimpleDateFormat")
public static Date strToDateDorp(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
@SuppressLint("SimpleDateFormat")
public static String dateToStr(Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(dateDate);
return dateString;
}
/**
* 传入一个String转化为long
*/
@SuppressLint("SimpleDateFormat")
public static Long stringParserLong(String param) throws ParseException {
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.parse(param).getTime();
}
/**
* 传入一个String转化为long
*/
@SuppressLint("SimpleDateFormat")
public static Long stringParserLong1(String param) throws ParseException {
format = new SimpleDateFormat("yyyyMMddHHmmss");
return format.parse(param).getTime();
}
/**
* 当前时间转换为long
*/
@SuppressLint("SimpleDateFormat")
public static Long currentDateParserLong() throws ParseException {
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.parse(format.format(Calendar.getInstance().getTime())).getTime();
}
/**
* 当前时间 如: 2013-04-22 10:37:00
*/
@SuppressLint("SimpleDateFormat")
public static String getCurrentDate() {
format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return format.format(Calendar.getInstance().getTime());
}
/**
* 当前时间 如: 10:37
*/
@SuppressLint("SimpleDateFormat")
public static String getCurrentDateHHMM() {
format = new SimpleDateFormat("HH:mm");
return format.format(Calendar.getInstance().getTime());
}
/**
* 当前时间 如: 10:37
*
* @throws ParseException
*/
@SuppressLint("SimpleDateFormat")
public static String getCurrentDateHHMMSS() {
format = new SimpleDateFormat("HH:mm:ss");
return format.format(Calendar.getInstance().getTime());
}
/**
* 当前时间 如: 20130422
*/
@SuppressLint("SimpleDateFormat")
public static String getCurrentDateString() {
format = new SimpleDateFormat("yyyyMMddHHmm");
return format.format(Calendar.getInstance().getTime());
}
/**
* 当前时间 如: 2013-04-22
*/
@SuppressLint("SimpleDateFormat")
public static String getCurrentTime() {
format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(Calendar.getInstance().getTime());
}
@SuppressLint("SimpleDateFormat")
public static String getSWAHDate() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(Calendar.getInstance().getTime());
}
@SuppressLint("SimpleDateFormat")
public static Long stringToLongD(String param) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.parse(param.substring(0, param.length() - 4)).getTime();
}
@SuppressLint("SimpleDateFormat")
public static Long stringToLong(String param) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
return format.parse(param).getTime();
}
/**
* 获取两个日期之间的间隔天数
*
* @return
*/
@SuppressLint("SimpleDateFormat")
public static int getGapCount(Date startDate, Date endDate) {
Calendar fromCalendar = Calendar.getInstance();
fromCalendar.setTime(startDate);
fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
fromCalendar.set(Calendar.MINUTE, 0);
fromCalendar.set(Calendar.SECOND, 0);
fromCalendar.set(Calendar.MILLISECOND, 0);
Calendar toCalendar = Calendar.getInstance();
toCalendar.setTime(endDate);
toCalendar.set(Calendar.HOUR_OF_DAY, 0);
toCalendar.set(Calendar.MINUTE, 0);
toCalendar.set(Calendar.SECOND, 0);
toCalendar.set(Calendar.MILLISECOND, 0);
return (int) ((toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24));
}
/**
* 日期转换成Java字符串
*
* @param date
* @return str
*/
@SuppressLint("SimpleDateFormat")
public static String DateToStr(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = format.format(date);
return str;
}
/**
* 字符串转换成日期
*
* @param str
* @return date
*/
@SuppressLint("SimpleDateFormat")
public static Date StrToDate(String str) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 字符串转换成日期
*
* @param str
* @return date
*/
@SuppressLint("SimpleDateFormat")
public static Date StrToDateDrop(String str) {
SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
Date date = null;
try {
date = format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* @param time
* @return
*/
@SuppressLint("SimpleDateFormat")
public static long getLongTime(String time) {
long ct = 0;
try {
format = new SimpleDateFormat("HH:mm:ss");
ct = format.parse(time).getTime();
} catch (Exception e) {
e.printStackTrace();
}
return ct;
}
/**
* 判断两日期是否同一天
*
* @param str1
* @param str2
* @return
*/
@SuppressLint("SimpleDateFormat")
public static boolean isSameDay(String str1, String str2) {
Date day1 = null, day2 = null;
day1 = DateTimeUtil.strToDate(str1);
day2 = DateTimeUtil.strToDate(str2);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String ds1 = sdf.format(day1);
String ds2 = sdf.format(day2);
if (ds1.equals(ds2)) {
return true;
} else {
return false;
}
}
/**
* 获取两个日期的时间差
*/
@SuppressLint("SimpleDateFormat")
public static int getTimeInterval(String date) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int interval = 0;
try {
Date currentTime = new Date();// 获取现在的时间
Date beginTime = dateFormat.parse(date);
interval = (int) ((beginTime.getTime() - currentTime.getTime()) / (1000));// 时间差
// 单位秒
} catch (ParseException e) {
e.printStackTrace();
}
return interval;
}
/**
* 获取两个日期的时间差 yyyy.MM.dd HH.mm.ss
*/
@SuppressLint("SimpleDateFormat")
public static int getInterval(String bDate, String eDate) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
int interval = 0;
try {
Date currentTime = dateFormat.parse(eDate);// 获取现在的时间
Date beginTime = dateFormat.parse(bDate);
interval = (int) ((beginTime.getTime() - currentTime.getTime()));// 时间差
// 单位秒
} catch (ParseException e) {
e.printStackTrace();
}
return interval;
}
/**
* 两个时间之差 求出一个long Time
*
* @param date
* @return
*/
@SuppressLint("SimpleDateFormat")
public static long getTime(String date) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long diff = 0;
try {
Date currentTime = new Date();// 获取现在的时间
Date getdate = df.parse(date);
diff = getdate.getTime() - currentTime.getTime();
} catch (Exception e) {
}
return diff;
}
/**
* 日期转换成Java字符串
*
* @param DATE1
* @param DATE2
* @return
*/
@SuppressLint("SimpleDateFormat")
public static int compare_date(String DATE1, String DATE2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
Date dt1 = df.parse(DATE1);
Date dt2 = df.parse(DATE2);
if (dt1.getTime() >= dt2.getTime()) {
return 1;
} else if (dt1.getTime() < dt2.getTime()) {
return -1;
} else {
return 0;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return 0;
}
/**
* 传入时间 算出星期几
*
* @param str 2014年1月3日
* @param days 1:2014年1月4日 类推
* @return
*/
@SuppressLint("SimpleDateFormat")
public static String formatDate(String str, int days) {
String dateStr = "";
try {
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
Date date = df.parse(str);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
Date d = dateFormat.parse(dateFormat.format(date));
c.setTime(d);
c.add(Calendar.DAY_OF_MONTH, days);
switch (c.get(Calendar.DAY_OF_WEEK) - 1) {
case 0:
dateStr = "周日";
break;
case 1:
dateStr = "周一";
break;
case 2:
dateStr = "周二";
break;
case 3:
dateStr = "周三";
break;
case 4:
dateStr = "周四";
break;
case 5:
dateStr = "周五";
break;
case 6:
dateStr = "周六";
break;
default:
break;
}
} catch (Exception e) {
e.printStackTrace();
}
return dateStr;
}
/**
* 根据提供的年月日获取该月份的第一天
*/
public static String getSupportBeginDayofMonth(int year, int monthOfYear) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, monthOfYear - 1);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.DAY_OF_MONTH, 1);
return cal.getTimeInMillis() + "";
}
/**
* 根据提供的年月获取该月份的最后一天
*
* @return
*/
public static String getSupportEndDayofMonth(int year, int monthOfYear) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, monthOfYear);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.add(Calendar.DAY_OF_MONTH, -1);
return cal.getTimeInMillis() + "";
}
public static String getYear(long millseconds) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date(millseconds);
return sdf.format(date).substring(0, 4);
}
public static String getMonth(long millseconds) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date(millseconds);
return sdf.format(date).substring(4, 6);
}
public static String formatFriendly(String timeStr) {
String time = "";
try {
time = DateTimeUtil.formatFriendly(new Date(Long.valueOf(timeStr) * 1000L));
} catch (Exception e) {
Log.e("FriendCircleMessage", "Time format exception.", e);
}
return time;
}
}