-
Notifications
You must be signed in to change notification settings - Fork 802
/
AppDateMgr.java
1577 lines (1415 loc) · 39.6 KB
/
AppDateMgr.java
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
package com.jingewenku.abrahamcaijin.commonutil;
import com.jingewenku.abrahamcaijin.commonutil.klog.KLog;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
/**
* @Description:主要功能:时间日期管理
*
* @Prject: CommonUtilLibrary
* @Package: com.jingewenku.abrahamcaijin.commonutil
* @author: june
* @date: 2017年05月05日 14:18
* @Copyright: 个人版权所有
* @Company:
* @version: 1.0.0
*/
public class AppDateMgr {
public AppDateMgr() {
throw new UnsupportedOperationException("cannot be instantiated");
}
public static final SimpleDateFormat YYYYMMDD_FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
public static final SimpleDateFormat HHMMSS_FORMAT = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
public static final SimpleDateFormat YYYYMMDDHHMMSS_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
private static final String[] CHINESE_ZODIAC = new String[]{"猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊"};
private static final String[] ZODIAC = new String[]{"水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座"};
private static final int[] ZODIAC_FLAGS = new int[]{20, 19, 21, 21, 21, 22, 23, 23, 23, 24, 23, 22};
/**
* 获取北京时区
* @return
*/
public static TimeZone getBeijingTimeZone(){
return TimeZone.getTimeZone("GMT+8:00");
}
/**
* 获取当前手机对应的系统时区
*
*/
public static TimeZone getPhoneTimeZone()
{
return TimeZone.getDefault();
}
/**
* 以“GMT+8:00”形式返回当前系统对应的时区
* @return
*/
public static String getCurrentTimeZoneStr()
{
TimeZone tz = TimeZone.getDefault();
return createGmtOffsetString(true,true,tz.getRawOffset());
}
public static String createGmtOffsetString(boolean includeGmt,
boolean includeMinuteSeparator, int offsetMillis) {
int offsetMinutes = offsetMillis / 60000;
char sign = '+';
if (offsetMinutes < 0) {
sign = '-';
offsetMinutes = -offsetMinutes;
}
StringBuilder builder = new StringBuilder(9);
if (includeGmt) {
builder.append("GMT");
}
builder.append(sign);
appendNumber(builder, 2, offsetMinutes / 60);
if (includeMinuteSeparator) {
builder.append(':');
}
appendNumber(builder, 2, offsetMinutes % 60);
return builder.toString();
}
private static void appendNumber(StringBuilder builder, int count, int value) {
String string = Integer.toString(value);
for (int i = 0; i < count - string.length(); i++) {
builder.append('0');
}
builder.append(string);
}
/**
* 获取更改时区后的时间
* @param date 时间
* @param oldZone 旧时区
* @param newZone 新时区
* @return 时间
*/
public static Date changeTimeZone(Date date, TimeZone oldZone, TimeZone newZone)
{
Date dateTmp = null;
if (date != null)
{
int timeOffset = oldZone.getRawOffset() - newZone.getRawOffset();
dateTmp = new Date(date.getTime() - timeOffset);
}
return dateTmp;
}
/**
* 将北京时区的时间转化为当前系统对应时区的时间
* @param beijingTime
* @param format
* @return
*/
public static String beijingTime2PhoneTime(String beijingTime,String format){
Date beijingDate = parseToDate(beijingTime, format);
Date phoneDate = changeTimeZone(beijingDate, getBeijingTimeZone(), getPhoneTimeZone());
String phoneTime= formatDateToStr(phoneDate,format);
return phoneTime;
}
/**
* 将日期字符串转换为Date对象
* @param date 日期字符串,必须为"yyyy-MM-dd HH:mm:ss"
* @param format 格式化字符串
* @return 日期字符串的Date对象表达形式
* */
public static Date parseToDate(String date, String format)
{
Date dt = null;
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
try
{
dt = dateFormat.parse(date);
}
catch(ParseException e)
{
e.printStackTrace();
}
return dt;
}
/**
* 将date----->String
* 将Date对象转换为指定格式的字符串
* @param date Date对象
* @param //String format 格式化字符串
* @return Date对象的字符串表达形式
* */
public static String formatDateToStr(Date date, String format)
{
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
return dateFormat.format(date);
}
/** 格式化日期的标准字符串 */
public final static String Detail_Format = "yyyy-MM-dd HH:mm:ss";
/**
* 将本地系统对应时区的时间转换为上北京时区对应的时间
* @param phoneTime
* @return
*/
public static String phoneTime2BeijingTime(String phoneTime){
Date phoneDate = parseToDate(phoneTime, Detail_Format);
Date beijingDate = changeTimeZone(phoneDate,getPhoneTimeZone(), getBeijingTimeZone());
String beijingTime= formatDateToStr(beijingDate,Detail_Format);
return beijingTime;
}
/**
* 当天的年月日
* @return
*/
public static String todayYyyyMmDd() {
return YYYYMMDD_FORMAT.format(new Date());
}
/**
* 当天的时分秒
* @return
*/
public static String todayHhMmSs() {
return HHMMSS_FORMAT.format(new Date());
}
/**
* 当天的年月日时分秒
* @return
*/
public static String todayYyyyMmDdHhMmSs() {
return YYYYMMDDHHMMSS_FORMAT.format(new Date());
}
/**
* 获取年
* @param dateTime
* @return
*/
public static int parseYyyy(String dateTime) {
try {
Calendar e = Calendar.getInstance();
Date date = YYYYMMDDHHMMSS_FORMAT.parse(dateTime);
e.setTime(date);
return e.get(1);
} catch (ParseException var3) {
var3.printStackTrace();
return 0;
}
}
/**
* 获取年
* @param dateTime
* @param simpleDateFormat
* @return
*/
public static int parseYyyy(String dateTime, SimpleDateFormat simpleDateFormat) {
try {
Calendar e = Calendar.getInstance();
Date date = simpleDateFormat.parse(dateTime);
e.setTime(date);
return e.get(1);
} catch (ParseException var4) {
var4.printStackTrace();
return 0;
}
}
/**
* 获取年
* @param date
* @return
*/
public static int parseYyyy(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(1);
}
/**
* 获取月
* @param dateTime
* @return
*/
public static int parseMm(String dateTime) {
try {
Calendar e = Calendar.getInstance();
Date date = YYYYMMDDHHMMSS_FORMAT.parse(dateTime);
e.setTime(date);
return e.get(2);
} catch (ParseException var3) {
var3.printStackTrace();
return 0;
}
}
/**
* 获取月
* @param dateTime
* @param simpleDateFormat
* @return
*/
public static int parseMm(String dateTime, SimpleDateFormat simpleDateFormat) {
try {
Calendar e = Calendar.getInstance();
Date date = simpleDateFormat.parse(dateTime);
e.setTime(date);
return e.get(2);
} catch (ParseException var4) {
var4.printStackTrace();
return 0;
}
}
/**
* 获取月
* @param date
* @return
*/
public static int parseMm(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(2);
}
/**
* 获取日
* @param dateTime
* @return
*/
public static int parseDd(String dateTime) {
try {
Calendar e = Calendar.getInstance();
Date date = YYYYMMDDHHMMSS_FORMAT.parse(dateTime);
e.setTime(date);
return e.get(5);
} catch (ParseException var3) {
var3.printStackTrace();
return 0;
}
}
/**
* 获取日
* @param dateTime
* @param simpleDateFormat
* @return
*/
public static int parseDd(String dateTime, SimpleDateFormat simpleDateFormat) {
try {
Calendar e = Calendar.getInstance();
Date date = simpleDateFormat.parse(dateTime);
e.setTime(date);
return e.get(5);
} catch (ParseException var4) {
var4.printStackTrace();
return 0;
}
}
/**
* 获取日
* @param date
* @return
*/
public static int parseDd(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(5);
}
/**
* 获取年月日
* @param dateTime
* @return
*/
public static String parseYyyyMmDd(String dateTime) {
String result = "";
try {
Date e = YYYYMMDDHHMMSS_FORMAT.parse(dateTime);
result = YYYYMMDD_FORMAT.format(e);
} catch (ParseException var3) {
var3.printStackTrace();
}
return result;
}
/**
* 获取年月日
* @param dateTime
* @param simpleDateFormat
* @return
*/
public static String parseYyyyMmDd(String dateTime, SimpleDateFormat simpleDateFormat) {
String result = "";
try {
Date e = simpleDateFormat.parse(dateTime);
result = YYYYMMDD_FORMAT.format(e);
} catch (ParseException var4) {
var4.printStackTrace();
}
return result;
}
/**
* 获取年月日
* @param date
* @return
*/
public static String parseYyyyMmDd(Date date) {
return YYYYMMDD_FORMAT.format(date);
}
/**
* 时分秒
* @param dateTime
* @return
*/
public static String parseHhMmSs(String dateTime) {
try {
Date e = YYYYMMDDHHMMSS_FORMAT.parse(dateTime);
return HHMMSS_FORMAT.format(e);
} catch (ParseException var2) {
var2.printStackTrace();
return "";
}
}
/**
* 时分秒
* @param dateTime
* @param simpleDateFormat
* @return
*/
public static String parseHhMmSs(String dateTime, SimpleDateFormat simpleDateFormat) {
try {
Date e = simpleDateFormat.parse(dateTime);
return HHMMSS_FORMAT.format(e);
} catch (ParseException var3) {
var3.printStackTrace();
return "";
}
}
/**
* 时分秒
* @param date
* @return
*/
public static String parseHhMmSs(Date date) {
return HHMMSS_FORMAT.format(date);
}
/**
* 获取星期几
* @param dateTime
* @return
*/
public static int getWeekNumber(String dateTime) {
return getWeekNumber(string2Date(dateTime, YYYYMMDDHHMMSS_FORMAT));
}
/**
* 获取星期几
* @param dateTime
* @param simpleDateFormat
* @return
*/
public static int getWeekNumber(String dateTime, SimpleDateFormat simpleDateFormat) {
return getWeekNumber(string2Date(dateTime, simpleDateFormat));
}
/**
* 获取星期几
* @param date
* @return
*/
public static int getWeekNumber(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(7);
}
/**
* 日期中某个月份的第几周
* @param dateTime
* @return
*/
public static int getWeekOfMonth(String dateTime) {
return getWeekOfMonth(string2Date(dateTime, YYYYMMDDHHMMSS_FORMAT));
}
/**
* 日期中某个月份的第几周
* @param dateTime
* @param simpleDateFormat
* @return
*/
public static int getWeekOfMonth(String dateTime, SimpleDateFormat simpleDateFormat) {
return getWeekOfMonth(string2Date(dateTime, simpleDateFormat));
}
/**
* 日期中某个月份的第几周
* @param date
* @return
*/
public static int getWeekOfMonth(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(4);
}
/**
* 日期中某个年份的第几周
* @param time
* @return
*/
public static int getWeekOfYear(String time) {
return getWeekOfYear(string2Date(time, YYYYMMDDHHMMSS_FORMAT));
}
/**
* 日期中某个年份的第几周
* @param time
* @param simpleDateFormat
* @return
*/
public static int getWeekOfYear(String time, SimpleDateFormat simpleDateFormat) {
return getWeekOfYear(string2Date(time, simpleDateFormat));
}
/**
* 日期中某个年份的第几周
* @param date
* @return
*/
public static int getWeekOfYear(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(3);
}
/**
* 将年月日时分秒转成Long类型
* @param dateTime
* @return
*/
public static Long dateTimeToTimeStamp(String dateTime) {
try {
Date e = YYYYMMDDHHMMSS_FORMAT.parse(dateTime);
return Long.valueOf(e.getTime() / 1000L);
} catch (ParseException var2) {
var2.printStackTrace();
return Long.valueOf(0L);
}
}
/**
* 将Long类型转成年月日时分秒
* @param timeStamp
* @return
*/
public static String timeStampToDateTime(Long timeStamp) {
return YYYYMMDDHHMMSS_FORMAT.format(new Date(timeStamp.longValue() * 1000L));
}
/**
* 将年月日时分秒转成Date类型
* @param time
* @return
*/
public static Date string2Date(String time) {
return string2Date(time, YYYYMMDDHHMMSS_FORMAT);
}
/**
* 将年月日时分秒转成Date类型
* @param time
* @param simpleDateFormat
* @return
*/
public static Date string2Date(String time, SimpleDateFormat simpleDateFormat) {
try {
return simpleDateFormat.parse(time);
} catch (ParseException var3) {
var3.printStackTrace();
return null;
}
}
/**
* 将Date类型转成年月日时分秒
* @param date
* @return
*/
public static String date2String(Date date) {
return date2String(date, YYYYMMDDHHMMSS_FORMAT);
}
/**
* 将Date类型转成年月日时分秒
* @param date
* @param simpleDateFormat
* @return
*/
public static String date2String(Date date, SimpleDateFormat simpleDateFormat) {
return simpleDateFormat.format(date);
}
/**
* 比较日期
* @param standDate
* @param desDate
* @return
*/
public static boolean dateIsBefore(String standDate, String desDate) {
try {
return YYYYMMDDHHMMSS_FORMAT.parse(desDate).before(YYYYMMDDHHMMSS_FORMAT.parse(standDate));
} catch (ParseException var3) {
var3.printStackTrace();
return false;
}
}
/**
* 相差多少分钟
* @param beginDate
* @param endDate
* @return
*/
public static long minutesBetweenTwoDate(String beginDate, String endDate) {
long millisBegin = dateTimeToTimeStamp(beginDate).longValue();
long millisEnd = dateTimeToTimeStamp(endDate).longValue();
return (millisEnd - millisBegin) / 60L;
}
/**
* 获取日期中的生肖
* @param dateTime
* @return
*/
public static String getChineseZodiac(String dateTime) {
int yyyy = parseYyyy(dateTime);
return getChineseZodiac(yyyy);
}
/**
* 获取日期中的生肖
* @param dateTime
* @param simpleDateFormat
* @return
*/
public static String getChineseZodiac(String dateTime, SimpleDateFormat simpleDateFormat) {
int yyyy = parseYyyy(dateTime, simpleDateFormat);
return getChineseZodiac(yyyy);
}
/**
* 获取日期中的生肖
* @param date
* @return
*/
public static String getChineseZodiac(Date date) {
int yyyy = parseYyyy(date);
return getChineseZodiac(yyyy);
}
/**
* 获取日期中的生肖
* @param year
* @return
*/
public static String getChineseZodiac(int year) {
return CHINESE_ZODIAC[year % 12];
}
/**
* 获取日期中的星座
* @param dateTime
* @return
*/
public static String getZodiac(String dateTime) {
int dd = parseDd(dateTime);
int month = parseMm(dateTime);
return getZodiac(month, dd);
}
/**
* 获取日期中的星座
* @param dateTime
* @param simpleDateFormat
* @return
*/
public static String getZodiac(String dateTime, SimpleDateFormat simpleDateFormat) {
int dd = parseDd(dateTime, simpleDateFormat);
int month = parseMm(dateTime, simpleDateFormat);
return getZodiac(month, dd);
}
/**
* 获取日期中的星座
* @param date
* @return
*/
public static String getZodiac(Date date) {
int dd = parseDd(date);
int month = parseMm(date);
return getZodiac(month, dd);
}
/**
* 获取日期中的星座
* @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];
}
/**
* 获取日期
*
* @param offset 表示偏移天数
* @return
*/
public String getNowDayOffset(int offset) {
Calendar m_Calendar = Calendar.getInstance();
long time = (long) m_Calendar.getTimeInMillis();
time = time + offset * 24 * 3600 * 1000;
Date myDate = new Date(time);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.format(myDate);
}
/**
* 获取日期
*
* @param
* @return
*/
public String getTime(long time) {
Date myDate = new Date(time);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return df.format(myDate);
}
/**
* 使指定日期向前走一天,变成“明天”的日期
*
* @param cal 等处理日期
*/
public void forward(Calendar cal) {
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);//0到11
int day = cal.get(Calendar.DAY_OF_MONTH);
int days = getDaysOfMonth(year, month + 1);
if (day == days) {//如果是本月最后一天,还要判断年份是不是要向前滚
if (month == 11) {//如果是12月份,年份要向前滚
cal.roll(Calendar.YEAR, true);
cal.set(Calendar.MONTH, 0);//月份,第一月是0
cal.set(Calendar.DAY_OF_MONTH, 1);
} else {//如果不是12月份
cal.roll(Calendar.MONTH, true);
cal.set(Calendar.DAY_OF_MONTH, 1);
}
} else {
cal.roll(Calendar.DAY_OF_MONTH, 1);//如果是月内,直接天数加1
}
}
/**
* 使日期倒一天
*
* @param cal
*/
public void backward(Calendar cal) {
//计算上一月有多少天
int month = cal.get(Calendar.MONTH);//0到11
int year = cal.get(Calendar.YEAR);
int days = getDaysOfMonth(year, month);//上个月的天数
int day = cal.get(Calendar.DAY_OF_MONTH);
if (day == 1) {//如果是本月第一天,倒回上一月
if (month == 0) {//如果是本年第一个月,年份倒一天
cal.roll(Calendar.YEAR, false);
cal.set(Calendar.MONTH, 11);//去年最后一个月是12月
cal.set(Calendar.DAY_OF_MONTH, 31);//12月最后一天总是31号
} else {//月份向前
cal.roll(Calendar.MONTH, false);
cal.set(Calendar.DAY_OF_MONTH, days);//上个月最后一天
}
} else {
cal.roll(Calendar.DAY_OF_MONTH, false);//如果是月内,日期倒一天
}
}
/**
* 判断平年闰年
*
* @param year
* @return true表示闰年,false表示平年
*/
public boolean isLeapYear(int year) {
if (year % 400 == 0) {
return true;
} else if (year % 100 != 0 && year % 4 == 0) {
return true;
}
return false;
}
/**
* 计算某月的天数
*
* @param year
* @param month 现实生活中的月份,不是系统存储的月份,从1到12
* @return
*/
public int getDaysOfMonth(int year, int month) {
if (month < 1 || month > 12) {
return 0;
}
boolean isLeapYear = isLeapYear(year);
int daysOfMonth = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
daysOfMonth = 31;
break;
case 4:
case 6:
case 9:
case 11:
daysOfMonth = 30;
break;
case 2:
if (isLeapYear) {
daysOfMonth = 29;
} else {
daysOfMonth = 28;
}
}
return daysOfMonth;
}
/**
* 获取当天凌晨的秒数
*
* @return
*/
public long secondsMorning(Calendar c) {
Calendar tempCalendar = Calendar.getInstance();
tempCalendar.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
return tempCalendar.getTimeInMillis();
}
/**
* 获取第二天凌晨的秒数
*
* @return
*/
public long secondsNight(Calendar c) {
Calendar tempCalendar = Calendar.getInstance();
tempCalendar.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
forward(tempCalendar);
return tempCalendar.getTimeInMillis();
}
/**
* 判断某两天是不是同一天
*
* @param c1
* @param c2
* @return
*/
public boolean isSameDay(Calendar c1, Calendar c2) {
if (c1.get(Calendar.YEAR) != c2.get(Calendar.YEAR))
return false;
if (c1.get(Calendar.MONTH) != c2.get(Calendar.MONTH))
return false;
if (c1.get(Calendar.DAY_OF_MONTH) != c2.get(Calendar.DAY_OF_MONTH))
return false;
return true;
}
/** 日期格式: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 **/
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;// 年
/** Log输出标识 **/
private static final String TAG = AppDateMgr.class.getSimpleName();
/**
* 将日期格式化成友好的字符串:几分钟前、几小时前、几天前、几月前、几年前、刚刚
*
* @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 "刚刚";
}
/**
* 将日期以yyyy-MM-dd HH:mm:ss格式化
*
* @param dateL
* 日期
* @return
*/
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
*/
public static String formatDateTime(long dateL, String formater) {
SimpleDateFormat sdf = new SimpleDateFormat(formater);
return sdf.format(new Date(dateL));
}
/**
* 将日期以yyyy-MM-dd HH:mm:ss格式化
*
* @param formater
* 日期
* @return
*/
public static String formatDateTime(Date date, String formater) {
SimpleDateFormat sdf = new SimpleDateFormat(formater);
return sdf.format(date);
}
/**
* 将日期字符串转成日期
*
* @param strDate
* 字符串日期
* @return java.util.date日期类型
*/
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) {
KLog.v(TAG, "parseDate failed !");
}
return returnDate;
}
/**
* 获取系统当前日期
*
* @return
*/
public static Date gainCurrentDate() {
return new Date();
}
/**
* 验证日期是否比当前日期早
*
* @param target1