DateUtils帮助类

Android时间格式相关的帮助类

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
package com.android.contacts.interactions;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import com.android.contacts.R;
import android.content.Context;
import android.text.format.DateUtils;
import android.util.Log;

/**
* 时间格式化帮助类
*/
public class DateUtils {

public static final long SECOND = 1000;
public static final long MINUTE = SECOND * 60;
/**
* 一小时的毫秒数
*/
public static final long HOUR = MINUTE * 60;
/**
* 一天的毫秒数
*/
public static final long DAY = HOUR * 24;
/**
* 一星期的毫秒数
*/
public static final long WEEK = DAY * 7;

/**
* 一个月的毫秒数
*/
public static final long MONTH = 30 * DAY;
/**
* 半年的毫秒数
*/
public static final long HALF_YEAR = 6 * MONTH;
/**
* 一年的毫秒数
*/
public static final long YEAR = DAY * 365; // or 366 ???

final static public String FULL_ST_FORMAT = "yyyy-MM-dd HH:mm:ss";
final static public String FULL_UTC_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
final static public String FULL_JSON_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
final static public String FULL_J_FORMAT = "yyyy/MM/dd HH:mm:ss";
final static public String CURRENCY_ST_FORMAT = "yyyy-MM-dd HH:mm";
final static public String CURRENCY_J_FORMAT = "yyyy/MM/dd HH:mm";
final static public String DATA_FORMAT = "yyyyMMddHHmmss";
final static public String FULL_DATA_FORMAT = "yyyyMMddHHmmssSSS";
final static public String ST_FORMAT = "yyyy-MM-dd HH:mm";
final static public String ST_CN_FORMAT = "yyyy年MM月dd日 HH:mm";
final static public String CN_FORMAT = "yy年MM月dd日HH:mm";
final static public String CN_FULL_FORMAT = "yyyy年MM月dd日 HH:mm:ss";
final static public String DAY_FORMAT = "yyyy-MM-dd";
final static public String SHORT_DATE_FORMAT = "yy-MM-dd";
final static public String YEAR_FORMAT = "yyyy";
final static public String MOUTH_DATE_TIME = "MM 月dd 日 HH:mm";
final static public String MOUTH_DATE = "MM 月dd 日";
final static public String CN_MOUTH = "yyyy年MM月";
final static public String MOUTH_FORMAT = "yyyy-MM";
final static public String TIME_FORMAT = "HH:mm";
final static public String CHINESE_MOUTH_FORMAT = "yyyy年MM月";
final static public String CHINESE_YYYYMM = "yyyy年MM月";
final static public String CHINESE_YYYYMMDD = "yyyy年MM月dd日";

/**
* 将秒格式化为00:00形式
*
* @param sec
* @return
*/
public static String formatSecond(int sec) {
StringBuffer buf = new StringBuffer();
buf.append(sec / 600);
buf.append(sec % 600 / 60);
buf.append(":");
buf.append(sec % 60 / 10);
buf.append(sec % 10);
return buf.toString();
}


/**
* @param format 格式
* @return String 格式化日期时间
*/
public static String Format(String format) {
return Format(format, new Date());
}

/**
* 得到指定时间的时间日期格式
*
* @param date 指定的时间
* @param format 时间日期格式
* @return
*/
public static String getFormatDateTime(Date date, String format) {
DateFormat df = new SimpleDateFormat(format);
return df.format(date);
}

/**
* @param format 格式
* @param date 日期
* @return String 格式化日期时间
*/
public static String Format(String format, Date date) {
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
return dateFormat.format(date);
}

/**
* 得到年
*
* @return int
*/
public static int getYear() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
return calendar.get(Calendar.YEAR);
}

/**
* 得到月
*
* @return int
*/
public static int getMonth() {
Calendar calendar = Calendar.getInstance();
Date trialTime = new Date();
calendar.setTime(trialTime);
return calendar.get(Calendar.MONTH) + 1;
}

/**
* 得到号数
*
* @return int
*/

public static int getDate() {
Calendar calendar = Calendar.getInstance();
Date trialTime = new Date();
calendar.setTime(trialTime);
return calendar.get(Calendar.DATE);
}

public static int getHour() {
Calendar calendar = Calendar.getInstance();
Date trialTime = new Date();
calendar.setTime(trialTime);
return calendar.get(Calendar.HOUR);
}

public static int getMinute() {
Calendar calendar = Calendar.getInstance();
Date trialTime = new Date();
calendar.setTime(trialTime);
return calendar.get(Calendar.MINUTE);
}

public static int getSecond() {
Calendar calendar = Calendar.getInstance();
Date trialTime = new Date();
calendar.setTime(trialTime);
return calendar.get(Calendar.SECOND);
}

/**
* 得到今天是月中的第几天
*
* @return int
*/
public static int getDAY_OF_MONTH() {
return getDAY_OF_MONTH(new Date());
}

/**
* @param trialTime 日期
* @return 得到月中的第几天
*/
public static int getDAY_OF_MONTH(Date trialTime) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(trialTime);
return calendar.get(Calendar.DAY_OF_MONTH);
}

/**
* @return int 得到今天周中的第几天,7 为星期天
*/
public static int getDAY_OF_WEEK() {
return getDAY_OF_WEEK(new Date());
}

/**
* @param trialTime 日期
* @return 得到周中的第几天 ,7 为星期天
*/
public static int getDAY_OF_WEEK(Date trialTime) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(trialTime);
return calendar.get(Calendar.DAY_OF_WEEK) - 1 == 0 ? 7 : calendar
.get(Calendar.DAY_OF_WEEK) - 1;
}

/**
* @return int 得到今天早上下午
*/
public static int getAM_PM() {
return getAM_PM(new Date());
}

/**
* AM = 0 PM = 1;
*
* @param trialTime 日期
* @return int 得到早上下午
*/
public static int getAM_PM(Date trialTime) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(trialTime);
return calendar.get(Calendar.AM_PM);
}

/**
* @param trialTime 日期
* @return 得到月分的最大天数
*/
public static int getCountMonthDay(Date trialTime) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(trialTime); // 放入你的日期
return calendar.getMaximum(Calendar.DATE);
}

/**
* @param date 日期
* @return 取得当前日期所在周的第一天
*/
public static Date getFirstDayOfWeek(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.SUNDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Sunday
c.set(Calendar.HOUR, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
return c.getTime();
}

/**
* @param date 日期
* @return 取得当前日期所在周的最后一天
*/
public static Date getLastDayOfWeek(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.SUNDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Saturday
c.add(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
return c.getTime();
}

/**
* @param date 日期
* @return 取得当前日期是第几周
*/
public static int getWeekOfYear(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setMinimalDaysInFirstWeek(7);
c.setTime(date);
return c.get(Calendar.WEEK_OF_YEAR);
}

/**
* @param year 年
* @return 得到某一年周的总数
*/
public static int getCountWeekOfYear(int year) {
Calendar c = new GregorianCalendar();
c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
return getWeekOfYear(c.getTime());
}

/**
* @param trialTime 日期
* @return 得到一个月开始的时间日期
*/
public static Date getBeginMonthDate(Date trialTime) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(trialTime); // 放入你的日期
calendar.set(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}

/**
* @param trialTime 日期
* @return 得到一个月结束的时间日期
*/
public static Date getEndMonthDate(Date trialTime) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(trialTime); // 放入你的日期
calendar.set(Calendar.DATE,
calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
return calendar.getTime();
}

/**
* @param trialTime 日期
* @return 得到一个年开始的时间日期
*/
public static Date getBeginYearDate(Date trialTime) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(trialTime); // 放入你的日期
calendar.set(Calendar.MONTH, Calendar.JANUARY);
calendar.set(Calendar.DATE, 0);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}

/**
* @param year 年份
* @return 得到一个年开始的时间日期
*/
public static Date getBeginYearDate(int year) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, Calendar.JANUARY);
calendar.set(Calendar.DATE, 0);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}

/**
* @param trialTime 日期
* @return 得到一年结束的时间日期
*/
public static Date getEndYearDate(Date trialTime) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(trialTime); // 放入你的日期
calendar.set(Calendar.MONTH, Calendar.DECEMBER);
calendar.set(Calendar.DATE, calendar.getMaximum(Calendar.DATE));
calendar.add(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
return calendar.getTime();
}

/**
* @param year 年份
* @return 得到一年结束的时间日期
*/
public static Date getEndYearDate(int year) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, Calendar.DECEMBER);
calendar.set(Calendar.DATE, calendar.getMaximum(Calendar.DATE));
calendar.add(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
return calendar.getTime();
}

/**
* 得到变化年的日期
*
* @param itmp 偏移量
* @return Date 需要偏移的日期
*/
public static Date getYear(int itmp) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.YEAR, itmp);
return calendar.getTime();
}

/**
* @param itmp 偏移量
* @param date 需要偏移的日期
* @return 得到变化年的月
*/
public static Date getMonth(int itmp, Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, itmp);
return calendar.getTime();
}

/**
* @param itmp 偏移量
* @return Date 得到变化号数的日期
*/
public static Date getDate(int itmp) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DATE, itmp);
return calendar.getTime();
}

/**
* @param itmp 偏移量
* @param date 初始日期
* @return Date 得到变化号数的日期
*/
public static Date getDate(int itmp, Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, itmp);
return calendar.getTime();
}

/**
* @param yeas 年
* @param month 月
* @param date 日
* @return 日期时间,得到偏移时间
*/
public static Date getMoveDate(int yeas, int month, int date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.YEAR, yeas);
calendar.add(Calendar.MONTH, month);
calendar.add(Calendar.DATE, date);
return calendar.getTime();
}

/**
* @param startDate 起始时间
* @param yeas 年
* @param month 月
* @param date 日
* @param hour 小时
* @param min 分钟
* @param sec 秒
* @return
*/
public static Date getMoveDate(Date startDate, int yeas, int month,
int date, int hour, int min, int sec) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
calendar.add(Calendar.YEAR, yeas);
calendar.add(Calendar.MONTH, month);
calendar.add(Calendar.DATE, date);
calendar.add(Calendar.HOUR, hour);
calendar.add(Calendar.MINUTE, min);
calendar.add(Calendar.SECOND, sec);
return calendar.getTime();
}

/**
* @param date 日期
* @return 得到这天开始的时间
*/
public static Date getBeginDateTime(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.AM_PM, 0);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}

/**
* @param date 日期
* @return 得到当前时间的开始小时时间,如2012-09-09 13:33:33 返回2012-09-09 13:00:00
*/
public static Date getBeginHourDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}

/**
* @param date 日期
* @return 得到这天最后结束的时间
*/
public static Date getEndDateTime(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(getBeginDateTime(date));
calendar.set(Calendar.AM_PM, 0);
calendar.add(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
return calendar.getTime();
}

/**
* 判断指定的时间是否是今天
*
* @param date 指定的时间
* @return true:是今天,false:非今天
*/
public static boolean isInToday(Date date) {
boolean flag = false;
Date now = new Date();
String fullFormat = getFormatDateTime(now, TecnoDateUtils.DAY_FORMAT);
String beginString = fullFormat + " 00:00:00";
String endString = fullFormat + " 23:59:59";
DateFormat df = new SimpleDateFormat(TecnoDateUtils.FULL_ST_FORMAT);
try {
Date beginTime = df.parse(beginString);
Date endTime = df.parse(endString);
flag = date.before(endTime) && date.after(beginTime);
} catch (ParseException e) {
e.printStackTrace();
}
return flag;
}

/**
* 得到今天的最后结束时间
*
* @return 今天的最后时间
*/
public static Date getTodayEndTime() {
String endString = Format(TecnoDateUtils.DAY_FORMAT, new Date())
+ " 23:59:59";
DateFormat df = new SimpleDateFormat(TecnoDateUtils.FULL_ST_FORMAT);
Date endTime = new Date();
try {
endTime = df.parse(endString);
} catch (ParseException e) {
e.printStackTrace();
}
return endTime;
}

/**
* 比较较两个日期,返回天数差
*
* @param beginDate 开始日期时间
* @param endDate 结束日期时间
* @return int
*/
public static long compareDay(Date beginDate, Date endDate) {
Calendar endDateYears = new GregorianCalendar();
endDateYears.setTime(endDate);
Calendar beginYears = new GregorianCalendar();
beginYears.setTime(beginDate);
long diffMillis = endDateYears.getTimeInMillis()
- beginYears.getTimeInMillis();
return diffMillis / DAY;
}

/**
* 判断是否属于这个日期范围
*
* @param date
* @param beginDate
* @param endDate
* @return 0:在两个时间中; -1:在开始时间之前; 1:在结束时间之后
*/
public static Integer inDate(Date date, Date beginDate, Date endDate) {
if (date.after(beginDate) && date.before(endDate)) {
return 0;
} else if (date.after(endDate)) {
return 1;
} else {
return -1;
}
}

/**
* 判断当前时间是否属于这个日期范围
*
* @param beginDate
* @param endDate
* @return 0:在两个时间中; -1:在开始时间之前; 1:在结束时间之后
*/
public static Integer inDate(Date beginDate, Date endDate) {
Date currentDate = getDate(0);
if (currentDate.after(beginDate) && currentDate.before(endDate)) {
return 0;
} else if (currentDate.after(endDate)) {
return 1;
} else {
return -1;
}
}

/**
* 比较指定日期与当前日期
*
* @param beginDate 开始日期时间
* @return 当前日期-指定日期的天数差
*/
public static long compareDay(Date beginDate) {
return compareDay(beginDate, new Date());
}

public static long compareHour(Date beginDate) {
return compareHour(beginDate, new Date());
}

public static long compareHour(Date beginDate, Date endDate) {
Calendar beginYears = new GregorianCalendar();
beginYears.setTime(beginDate);
long diffMillis = endDate.getTime() - beginYears.getTimeInMillis();
return diffMillis / (60 * 60 * 1000);
}

/**
* 比较当前系统时间与参数时间的差,结果为系统时间-参数时间
*
* @param beginDate
* @return
*/
public static long compareMillis(Date beginDate) {
Calendar beginYears = new GregorianCalendar();
beginYears.setTime(beginDate);
return System.currentTimeMillis() - beginYears.getTimeInMillis();
}

/**
* 计算两个时间的差,结果为endDate-beginDate
*
* @param beginDate
* @param endDate
* @return
*/
public static long compareMillis(Date beginDate, Date endDate) {
Calendar beginYears = new GregorianCalendar();
beginYears.setTime(beginDate);
Calendar endYears = new GregorianCalendar();
endYears.setTime(endDate);
return endYears.getTimeInMillis() - beginYears.getTimeInMillis();
}

/**
* 判断是否属于这个日期范围,txweb 标题中判断是否 是新的
*
* @param date 创建日期
* @param iday 天数
* @return boolean
*/
static public boolean inDate(Date date, int iday) {
return compareDay(date) < iday;
}

/**
* 判断是否属于这个日期范围,txweb 标题中判断是否 是新的
*
* @param dateTime 创建日期
* @param iday 天数
* @return boolean
*/
static public boolean inDate(long dateTime, int iday) {
return compareDay(new Date(dateTime)) < iday;
}

/**
* 返回一个String类型的他们之间的时间差 只到小时 例如:38:15:00(三十八小时15分) 如果获取的当前日期在后面 则返回0
*
* @param date 比较的实际
* @return 字符串
*/
public static String minus(Date date) {
Date now = new Date();
if (now.after(date)) {
return "0";
} else {
long time = date.getTime() - now.getTime();
int hour = (int) (time / (60 * 60 * 1000));
int minute = (int) ((time % (60 * 60 * 1000)) / (60 * 1000));
int second = (int) (((time % (60 * 60 * 1000)) % (60 * 1000)) / 1000 + 1);
if (second == 60) {
second = 0;
minute += 1;
}
if (minute == 60) {
minute = 0;
hour += 1;
}
return "" + (hour < 10 ? ("0" + hour) : hour) + ":"
+ (minute < 10 ? ("0" + minute) : minute) + ":"
+ (second < 10 ? ("0" + second) : second);
}
}

/**
* 返回时长
*/
public static String getTimeLength(long time) {
StringBuffer timeLengthBuffer = new StringBuffer();
long year = time / (365 * 24 * 3600);
time = time % (365 * 24 * 3600);
long month = time / (30 * 24 * 3600);
time = time % (30 * 24 * 3600);
long day = time / (24 * 3600);
time = time % (24 * 3600);
long hour = time / 3600;
time = time % 3600;
long min = time / 60;
time = time % 60;
long sec = time;
timeLengthBuffer.append(year == 0 ? "" : year + "年");
timeLengthBuffer.append(month == 0 ? "" : year + "月");
timeLengthBuffer.append(day == 0 ? "" : year + "天");
timeLengthBuffer.append(hour == 0 ? "" : hour + "小时");
timeLengthBuffer.append(min == 0 ? "0分" : min + "分钟");
timeLengthBuffer.append(sec == 0 ? "0秒" : sec + "秒");
return timeLengthBuffer.toString();
}

/**
* 得到年
*
* @return int
*/
public static int getYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.YEAR);
}

/**
* 得到月
*
* @return int
*/
public static int getMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MONTH) + 1;
}

public static String getDisTimeFormat(long time) {
StringBuilder sb = new StringBuilder();
long between = time / 1000;
long hour1 = between % (24 * 3600) / 3600;
if (hour1 < 10) {
sb.append(0);
}
sb.append(hour1);
sb.append(":");
long minute1 = between % 3600 / 60;
if (minute1 < 10) {
sb.append(0);
}
sb.append(minute1);
sb.append(":");
long second1 = between % 60;
if (second1 < 10) {
sb.append(0);
}
sb.append(second1);
return sb.toString();

}

/**
* 判断是否是昨天
*
* @param time
* @return
*/
public static boolean isYesterday(long time) {
if (time >= getYesterdayMinTimeMillis() &&
time <= getYesterdayMaxTimeMillis()) {
return true;
} else {
return false;
}
}

/**
* D
* 通过long值得到Date值
*
* @param time
* @return
*/
public static Date getDateFromLong(long time) {
Date date = new Date(time);
return date;
}


/**
* 通过long值得到时间格式为
*
* @param time
* @return
*/
public static String getTimeFromLong(long time) {
Date date = new Date(time);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(date);
return dateString;
}


public static String getFormatDate(long time) {
long currentMseconds = System.currentTimeMillis();
long timeGap = currentMseconds - time;
if (timeGap / 1000 == 0) {
return "刚刚";
}
// 秒判断
if (timeGap > 0 && timeGap <= 60 * 1000) {
long second = timeGap / 1000;
return second + "秒前";
}
// 分判断
if (timeGap >= 60 * 1000 && timeGap <= 60 * 60 * 1000) {
long minutes = timeGap / (1000 * 60);
return minutes + "分前";
}
// 时判断
if (timeGap >= 60 * 60 * 1000 && timeGap <= 24 * 60 * 60 * 1000) {
long hours = timeGap / (1000 * 60 * 60);
return hours + "小时前";
}
// 6月5日
return getMonth(new Date(time)) + "月" + getDAY_OF_MONTH(new Date(time)) + "日";
}
/**
* 返回昨天的最小日期
* @return
*/
public static long getYesterdayMinTimeMillis() {
Calendar mCalendar = Calendar.getInstance();
long currTime = System.currentTimeMillis();
mCalendar.setTime(new Date(currTime));

int year = mCalendar.get(Calendar.YEAR);
int month = mCalendar.get(Calendar.MONTH);
int day = mCalendar.get(Calendar.DAY_OF_MONTH);

mCalendar.set(year, month, day, 0, 0, 0);
long minToday = mCalendar.getTimeInMillis() - DAY;

return minToday;
}
/**
* 返回昨天的最大日期
* @return
*/
public static long getYesterdayMaxTimeMillis() {
Calendar mCalendar = Calendar.getInstance();
long currTime = System.currentTimeMillis();
mCalendar.setTime(new Date(currTime));

int year = mCalendar.get(Calendar.YEAR);
int month = mCalendar.get(Calendar.MONTH);
int day = mCalendar.get(Calendar.DAY_OF_MONTH);

mCalendar.set(year, month, day, 23, 59, 59);
long minToday = mCalendar.getTimeInMillis() - DAY;

return minToday;
}
public static boolean isCurrentWeek(long time){
long currentTime = System.currentTimeMillis();
long minWeekTime = getFirstDayOfWeek(new Date(currentTime)).getTime();
long maxWeekTime = getLastDayOfWeek(new Date(currentTime)).getTime();
if(time < maxWeekTime && time > minWeekTime){
return true;
}
return false;
}
public static CharSequence getCallDate(Context context,long time) {
StringBuilder mBuilder = new StringBuilder();
if(isCurrentWeek(time)){
if(DateUtils.isToday(time)){
mBuilder.append(context.getResources().getString(R.string.call_log_header_today));
mBuilder.append(", ");
mBuilder.append( DateUtils.formatDateRange(context, time, time, DateUtils.FORMAT_SHOW_TIME |
DateUtils.FORMAT_SHOW_DATE |
DateUtils.FORMAT_ABBREV_ALL));
return mBuilder.toString();
}else if(TecnoDateUtils.isYesterday(time)){
mBuilder.append(context.getResources().getString(R.string.call_log_header_yesterday));
mBuilder.append(", ");
mBuilder.append( DateUtils.formatDateRange(context, time, time, DateUtils.FORMAT_SHOW_TIME |
DateUtils.FORMAT_SHOW_DATE |
DateUtils.FORMAT_ABBREV_ALL));
return mBuilder.toString();
}else{
return DateUtils.formatDateRange(context, time, time, DateUtils.FORMAT_SHOW_TIME |
DateUtils.FORMAT_SHOW_DATE |
DateUtils.FORMAT_SHOW_WEEKDAY |
DateUtils.FORMAT_ABBREV_ALL);
}

}else{
if(getYear() == getYear(new Date(time))){
return DateUtils.formatDateRange(context, time, time, DateUtils.FORMAT_SHOW_TIME |
DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
}else{
return DateUtils.formatDateRange(context, time, time, DateUtils.FORMAT_SHOW_TIME |
DateUtils.FORMAT_SHOW_DATE |
DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_ABBREV_ALL);
}
}
}

}