Android Calendar的工具类

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
/**
 * 时间工具类
 * 1、String类型的日期时间转化为Date类型 getDateByFormat("2016-04-15 12:20:11","yyyy-MM-dd HH:mm:ss");
 * 2、获取Date偏移之后的Date getDateByOffset(date,Calendar.DATE,1);
 * 3、获取字符串日期时间的字符串(可偏移) getStringByOffset("2016-04-15 12:20:11","yyyy-MM-dd HH:mm:ss",Calendar.DATE,1);
 * 4、Date类型转化为String类型(可偏移)  getStringByOffset(date,"yyyy-MM-dd HH:mm:ss",Calendar.DATE,1);
 * 5、Date类型转化为String类型 getStringByFormat((date,"yyyy-MM-dd HH:mm:ss");
 * 6、获取指定日期时间的字符串,用于导出想要的格式 getStringByFormat("2016-04-15 12:20:11","yyyy-MM-dd HH:mm:ss");
 * 7、获取milliseconds表示的日期时间的字符串 getStringByFormat(1470816365,"yyyy-MM-dd HH:mm:ss");
 * 8、获取表示当前日期时间的字符串 getCurrentDate("yyyy-MM-dd HH:mm:ss");
 * 9、计算两个日期所差的天数 getOffectDay(1470816365,1470816415); or getOffectDay(startDate,endDate)
 * 10、计算两个日期所差的小时数 getOffectHour(1470816365,1470816415);
 * 11、计算两个日期所差的分钟数 getOffectMinutes(1470816365,1470816415);
 * 12、获取本周一 getFirstDayOfWeek("2016-04-15 12:20:11");
 * 13、获取本周日 getLastDayOfWeek("2016-04-15 12:20:11");
 * 14、获取本周的某一天 getDayOfWeek("2016-04-15 12:20:11",Calendar.DATE);
 * 15、获取本月第一天 getFirstDayOfMonth("2016-04-15 12:20:11");
 * 16、获取本月最后一天 getLastDayOfMonth("2016-04-15 12:20:11");
 * 17、获取表示当前日期的0点时间毫秒数 getFirstTimeOfDay();
 * 18、获取表示当前日期24点时间毫秒数 getLastTimeOfDay();
 * 19、判断是否是闰年 isLeapYear(2014);
 * 20、根据时间返回格式化后的时间的描述 formatDateStr2Desc2("2016-04-15 12:20:11","yyyy-MM-dd HH:mm:ss");
 * 21、以友好的方式显示时间  formatDateStr2Desc("2016-04-15 12:20:11","yyyy-MM-dd HH:mm:ss");
 * 22、取指定日期为星期几 getWeekNumber("2016-04-15 12:20:11","yyyy-MM-dd HH:mm:ss");
 * 23、根据给定的日期判断是否为上下午 getTimeQuantum("2016-04-15 12:20:11","yyyy-MM-dd HH:mm:ss");
 * 24、根据给定的毫秒数算得时间的描述 getTimeDescription(1470816365);
 * 25、将Calendar转成字符串 calendarConvertString(calendar,"yyyy-MM-dd HH:mm:ss");
 * 26、将字符串转成Calendar stringConvertCalendar("2016-04-15 12:20:11","yyyy-MM-dd HH:mm:ss");
 * 27、比较2个时间的大小 calendarCompare(calendar1,calendar2);
 * 28、得到当前时间的随机数 getCurRandomDate();
 * 29、某年某月有多少天 getDaysInMonth(2016,5);
 * 30、某年某月的第一天是周几 getFirstWeekDayInMonth(2016,5);
 * 31、得到2个时间戳之间相差的 天 时 分 秒 getDate(1470816365,1470816415);
 * 32、阳历节日 calendarFestival(calendar);
 * 33、阴历节日 lunarHoliday(calendar);
 * Created by slantech on 2018/04/28 13:40
 */
public class CalendarTool {
 
    /**
     * 时间日期格式化到年月日时分秒.
     */
    public static final String dateFormatYMDHMS = "yyyy-MM-dd HH:mm:ss";
 
    public static final String dateFormatYMDHMSSSS = "yyyy-MM-dd HH:mm:ss:SSS";
    /**
     * 时间日期格式化到年月日.
     */
    public static final String dateFormatYMD = "yyyy-MM-dd";
    /**
     * 时间日期格式化到年月.
     */
    public static final String dateFormatYM = "yyyy-MM";
    /**
     * 时间日期格式化到年.
     */
    public static final String dateFormatY = "yyyy";
    /**
     * 时间日期格式化到年月日时分.
     */
    public static final String dateFormatYMDHM = "yyyy-MM-dd HH:mm";
    /**
     * 时间日期格式化到月日.
     */
    public static final String dateFormatMD = "MM/dd";
    /**
     * 时分秒.
     */
    public static final String dateFormatHMS = "HH:mm:ss";
    /**
     * 时分.
     */
    public static final String dateFormatHM = "HH:mm";
    /**
     * 上午.
     */
    public static final String AM = "AM";
    /**
     * 下午.
     */
    public static final String PM = "PM";
 
    static final String[] solarTerms = {"立春", "雨水", "惊蛰", "春分", "清明", "谷雨",
            "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露",
            "霜降", "立冬", "小雪", "大雪", "冬至", "小寒", "大寒"};
 
    static final int[][] solarTermsDays = {{2, 4}, {2, 19}, {3, 6},
            {3, 21}, {4, 5}, {4, 20}, {5, 6}, {5, 21}, {6, 6},
            {6, 21}, {7, 7}, {7, 23}, {8, 8}, {8, 23}, {9, 8},
            {9, 23}, {10, 8}, {10, 23}, {11, 7}, {11, 22}, {12, 7},
            {12, 22}, {1, 6}, {1, 20}};
 
    static final String[] festivals = {"元旦", "情人", "女生", "妇女", "植树", "愚人",
            "劳动", "青年", "儿童", "建军", "教师", "国庆", "万圣", "光棍", "平安", "圣诞"};
 
    static final int[][] festivalsDays = {{1, 1}, {2, 14}, {3, 7},
            {3, 8}, {3, 12}, {4, 1}, {5, 1}, {5, 4}, {6, 1},
            {8, 1}, {9, 10}, {10, 1}, {10, 31}, {11, 11}, {12, 24},
            {12, 25}};
 
    static final String[] chineseFestivals = {"春节", "元宵", "花朝", "上巳节", "端午",
            "七夕", "中元", "中秋", "重阳", "祭祖", "下元", "腊八"};
 
    static final int[][] chineseFestivalsDays = {{1, 1}, {1, 15},
            {2, 12}, {3, 3}, {5, 5}, {7, 7}, {7, 15}, {8, 15},
            {9, 9}, {10, 1}, {10, 15}, {12, 8}};
 
 
    /**
     * 是否是大于当前日期 以天判断
     *
     * @param day
     * @return
     */
    public static boolean isFutureOfDay(String day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getDateByFormat(day, "yyyy-MM-dd"));
        Calendar current = Calendar.getInstance();
        current.setTime(getDateByFormat(getCurrentDate("yyyy-MM-dd"), "yyyy-MM-dd"));
        return calendar.getTimeInMillis() - current.getTimeInMillis() >= 0;
    }
 
    /**
     * 是否是大于当前日期的天数 以天判断
     *
     * @param day
     * @return
     */
    public static boolean isFutureOfDay(String day, int fday) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getDateByFormat(day, "yyyy-MM-dd"));
        Calendar current = Calendar.getInstance();
        current.setTime(getDateByFormat(getCurrentDate("yyyy-MM-dd"), "yyyy-MM-dd"));
        long size = calendar.getTimeInMillis() - current.getTimeInMillis();
        return size >= fday * 24 * 60 * 60*1000;
    }
 
 
    /**
     * 判断时间是否超过当前时间
     *
     * @return
     */
    public static boolean DateIsCurrent(String time) {
        boolean is = false;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            ULogger.e(e);
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        is = calendar.getTimeInMillis() > System.currentTimeMillis();
        return is;
    }
 
 
    /**
     * String 类型转化为long类型
     *
     * @param date
     * @return
     */
    public static long getStringToLongDate(String date) {
        Calendar c = Calendar.getInstance();
        c.setTime(getDateByFormat(date, "yyyy-MM-dd"));
        return c.getTimeInMillis();
    }
 
    /**
     * String 类型转化为long类型
     *
     * @param date
     * @param format
     * @return
     */
    public static long getStringToLongDate(String date, String format) {
        Calendar c = Calendar.getInstance();
        c.setTime(getDateByFormat(date, format));
        return c.getTimeInMillis();
    }
 
    /**
     * String类型的日期时间转化为Date类型.
     *
     * @param strDate String形式的日期时间
     * @param format  格式化字符串,如:"yyyy-MM-dd HH:mm:ss"
     * @return Date Date类型日期时间
     */
    public static Date getDateByFormat(String strDate, String format) {
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = mSimpleDateFormat.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
 
    /**
     * 获取Date偏移之后的Date.
     *
     * @param date          日期时间
     * @param calendarField Calendar属性,对应offset的值, 如(Calendar.DATE,表示+offset天,Calendar.HOUR_OF_DAY,表示+offset小时)
     * @param offset        偏移(值大于0,表示+,值小于0,表示-)
     * @return Date 偏移之后的日期时间
     */
    public Date getDateByOffset(Date date, int calendarField, int offset) {
        Calendar c = new GregorianCalendar();
        try {
            c.setTime(date);
            c.add(calendarField, offset);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }
 
    /**
     * 获取字符串日期时间的字符串(可偏移).
     *
     * @param strDate       String形式的日期时间
     * @param format        格式化字符串,如:"yyyy-MM-dd HH:mm:ss"
     * @param calendarField Calendar属性,对应offset的值,
     *                      如(Calendar.DATE,表示+offset天,Calendar.HOUR_OF_DAY,表示+offset小时)
     * @param offset        偏移(值大于0,表示+,值小于0,表示-)
     * @return String String类型的日期时间
     */
    public static String getStringByOffset(String strDate, String format, int calendarField, int offset) {
        String mDateTime = null;
        try {
            Calendar c = new GregorianCalendar();
            SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
            c.setTime(mSimpleDateFormat.parse(strDate));
            c.add(calendarField, offset);
            mDateTime = mSimpleDateFormat.format(c.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return mDateTime;
    }
 
    /**
     * Date类型转化为String类型(可偏移).
     *
     * @param date          the date
     * @param format        the format
     * @param calendarField the calendar field
     * @param offset        the offset
     * @return String String类型日期时间
     */
    public static String getStringByOffset(Date date, String format, int calendarField, int offset) {
        String strDate = null;
        try {
            Calendar c = new GregorianCalendar();
            SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
            c.setTime(date);
            c.add(calendarField, offset);
            strDate = mSimpleDateFormat.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strDate;
    }
 
 
    /**
     * @param str
     * @param format
     * @return
     */
    public static Date getStringToDate(String str, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
 
        return new Date();
    }
 
 
    /**
     * Date类型转化为String类型.
     *
     * @param date   the date
     * @param format the format
     * @return String String类型日期时间
     */
    public static String getStringByFormat(Date date, String format) {
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
        String strDate = "";
        try {
            strDate = mSimpleDateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strDate;
    }
 
    /**
     * 获取指定日期时间的字符串,用于导出想要的格式.
     *
     * @param strDate String形式的日期时间,必须为yyyy-MM-dd HH:mm:ss格式
     * @param format  输出格式化字符串,如:"yyyy-MM-dd HH:mm:ss"
     * @return String 转换后的String类型的日期时间
     */
    public static String getStringByFormat(String strDate, String format) {
        if (TextUtils.isEmpty(strDate))
            return "";
        String mDateTime = "";
        try {
            Calendar c = new GregorianCalendar();
            SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
            c.setTime(mSimpleDateFormat.parse(strDate));
            SimpleDateFormat mSimpleDateFormat2 = new SimpleDateFormat(format);
            mDateTime = mSimpleDateFormat2.format(c.getTime());
        } catch (Exception e) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy K:m:s a", Locale.ENGLISH);
                Date d2 = sdf.parse(strDate);
                return getStringByFormat(d2, format);
            } catch (Exception e1) {
                Long l = Long.parseLong(strDate);
                return getStringByFormat(l, format);
            }
        }
        return mDateTime;
    }
 
    /**
     * 获取milliseconds表示的日期时间的字符串.
     *
     * @param milliseconds the milliseconds
     * @param format       格式化字符串,如:"yyyy-MM-dd HH:mm:ss"
     * @return String 日期时间字符串
     */
    public static String getStringByFormat(long milliseconds, String format) {
        String thisDateTime = "";
        try {
            SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
            thisDateTime = mSimpleDateFormat.format(milliseconds);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return thisDateTime;
    }
 
    /**
     * 获取表示当前日期时间的字符串.
     *
     * @param format 格式化字符串,如:"yyyy-MM-dd HH:mm:ss"
     * @return String String类型的当前日期时间
     */
    public static String getCurrentDate(String format) {
        String curDateTime = null;
        try {
            SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
            Calendar c = new GregorianCalendar();
            curDateTime = mSimpleDateFormat.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return curDateTime;
 
    }
 
    /**
     * 获取表示当前日期时间的字符串(可偏移).
     *
     * @param format        格式化字符串,如:"yyyy-MM-dd HH:mm:ss"
     * @param calendarField Calendar属性,对应offset的值, 如(Calendar.DATE,表示+offset天,Calendar.HOUR_OF_DAY,表示+offset小时)
     * @param offset        偏移(值大于0,表示+,值小于0,表示-)
     * @return String String类型的日期时间
     */
    public static String getCurrentDateByOffset(String format, int calendarField, int offset) {
        String mDateTime = null;
        try {
            SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
            Calendar c = new GregorianCalendar();
            c.add(calendarField, offset);
            mDateTime = mSimpleDateFormat.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mDateTime;
 
    }
 
    /**
     * 计算两个日期所差的天数.
     *
     * @param milliseconds1 the milliseconds1 big
     * @param milliseconds2 the milliseconds2 s
     * @return int 所差的天数
     */
    public static int getOffectDay(long milliseconds1, long milliseconds2) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTimeInMillis(milliseconds1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTimeInMillis(milliseconds2);
        //先判断是否同年
        /*int y1 = calendar1.get(Calendar.YEAR);
        int y2 = calendar2.get(Calendar.YEAR);
        int d1 = calendar1.get(Calendar.DAY_OF_YEAR);
        int d2 = calendar2.get(Calendar.DAY_OF_YEAR);
        int maxDays = 0;
        int day = 0;
        if (y1 - y2 > 0) {
            maxDays = calendar2.getActualMaximum(Calendar.DAY_OF_YEAR);
            day = d1 - d2 + maxDays;
        } else if (y1 - y2 < 0) {
            maxDays = calendar1.getActualMaximum(Calendar.DAY_OF_YEAR);
            day = d1 - d2 - maxDays;
        } else {
            day = d1 - d2;
        }*/
        return getOffectDay(calendar2.getTime(), calendar1.getTime());
    }
 
    /**
     * 计算两个日期所差的天数.
     *
     * @param startDate startDate s
     * @param endDate   endDate big
     * @return int 无正负情况
     */
    public static int getOffectDay(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);
        long timec = toCalendar.getTime().getTime() - fromCalendar.getTime().getTime();
        return (int) (timec / (1000 * 60 * 60 * 24));
    }
 
    /**
     * 计算两个日期所差的小时数.
     *
     * @param date1 第一个时间的毫秒表示
     * @param date2 第二个时间的毫秒表示
     * @return int 所差的小时数
     */
    public static int getOffectHour(long date1, long date2) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTimeInMillis(date1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTimeInMillis(date2);
        int h1 = calendar1.get(Calendar.HOUR_OF_DAY);
        int h2 = calendar2.get(Calendar.HOUR_OF_DAY);
        int h = 0;
        int day = getOffectDay(date1, date2);
        h = h1 - h2 + day * 24;
        return h;
    }
 
    /**
     * 计算两个日期所差的分钟数.
     *
     * @param date1 第一个时间的毫秒表示
     * @param date2 第二个时间的毫秒表示
     * @return int 所差的分钟数
     */
    public static int getOffectMinutes(long date1, long date2) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTimeInMillis(date1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTimeInMillis(date2);
        int m1 = calendar1.get(Calendar.MINUTE);
        int m2 = calendar2.get(Calendar.MINUTE);
        int h = getOffectHour(date1, date2);
        int m = 0;
        m = m1 - m2 + h * 60;
        return m;
    }
 
    /**
     * 获取本周一.
     *
     * @param format format
     * @return String String类型日期时间
     */
    public static String getFirstDayOfWeek(String format) {
        return getDayOfWeek(format, Calendar.MONDAY);
    }
 
    /**
     * 获取本周日.
     *
     * @param format the format
     * @return String String类型日期时间
     */
    public static String getLastDayOfWeek(String format) {
        return getDayOfWeek(format, Calendar.SUNDAY);
    }
 
    /**
     * 获取本周的某一天.
     *
     * @param format        format
     * @param calendarField calendarField
     * @return String String类型日期时间
     */
    private static String getDayOfWeek(String format, int calendarField) {
        String strDate = null;
        try {
            Calendar c = new GregorianCalendar();
            SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
            int week = c.get(Calendar.DAY_OF_WEEK);
            if (week == calendarField) {
                strDate = mSimpleDateFormat.format(c.getTime());
            } else {
                int offectDay = calendarField - week;
                if (calendarField == Calendar.SUNDAY) {
                    offectDay = 7 - Math.abs(offectDay);
                }
                c.add(Calendar.DATE, offectDay);
                strDate = mSimpleDateFormat.format(c.getTime());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strDate;
    }
 
    /**
     * 获取当前时间和当前时间前30天时间
     *
     * @return
     */
    public static String[] getDateOfTodayTofirst() {
        String[] dates = new String[2];
        Calendar calendar = Calendar.getInstance();
        dates[1] = UtilsString.longToStringDate(calendar.getTimeInMillis(), dateFormatYMD);//今天
        calendar.add(Calendar.MONTH, -1);
        dates[0] = UtilsString.longToStringDate(calendar.getTimeInMillis(), dateFormatYMD);
        return dates;
    }
 
 
    /**
     * 获取本月第一天.
     *
     * @param format format
     * @return String String类型日期时间
     */
    public static String getFirstDayOfMonth(String format) {
        String strDate = null;
        try {
            Calendar c = new GregorianCalendar();
            SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
            //当前月的第一天
            c.set(GregorianCalendar.DAY_OF_MONTH, 1);
            strDate = mSimpleDateFormat.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strDate;
 
    }
 
    /**
     * 获取本月最后一天.
     *
     * @param format format
     * @return String String类型日期时间
     */
    public static String getLastDayOfMonth(String format) {
        String strDate = null;
        try {
            Calendar c = new GregorianCalendar();
            SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
            // 当前月的最后一天
            c.set(Calendar.DATE, 1);
            c.roll(Calendar.DATE, -1);
            strDate = mSimpleDateFormat.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strDate;
    }
 
    /**
     * 获取表示当前日期的0点时间毫秒数.
     *
     * @return long the first time of day
     */
    public static long getFirstTimeOfDay() {
        Date date = null;
        try {
            String currentDate = getCurrentDate(dateFormatYMD);
            date = getDateByFormat(currentDate + " 00:00:00", dateFormatYMDHMS);
            return date.getTime();
        } catch (Exception e) {
        }
        return -1;
    }
 
 
    /**
     * 获取传入日期的0点的时间毫秒
     *
     * @param str
     * @return
     */
    public static long getFirstTimeOfDay(String str) {
        Date date = null;
        try {
            String currentDate = getStringByFormat(str, dateFormatYMD);
            date = getDateByFormat(currentDate + " 00:00:00", dateFormatYMDHMS);
            return date.getTime();
        } catch (Exception e) {
        }
        return -1;
    }
 
 
    /**
     * 获取表示当前日期24点时间毫秒数.
     *
     * @return long the last time of day
     */
    public static long getLastTimeOfDay() {
        Date date = null;
        try {
            String currentDate = getCurrentDate(dateFormatYMD);
            date = getDateByFormat(currentDate + " 24:00:00", dateFormatYMDHMS);
            return date.getTime();
        } catch (Exception e) {
        }
        return -1;
    }
 
    /**
     * 判断是否是闰年()(year能被4整除 并且 不能被100整除) 或者 year能被400整除,则该年为闰年.
     *
     * @param year 年代(如2012)
     * @return boolean 是否为闰年
     */
    public static boolean isLeapYear(int year) {
        if ((year % 4 == 0 && year % 400 != 0) || year % 400 == 0) {
            return true;
        } else {
            return false;
        }
    }
 
    /**
     * 以友好的方式显示时间
     *
     * @param sdate     sdate
     * @param outFormat outFormat
     * @return String
     */
    public static String formatDateStr2Desc(String sdate, String outFormat) {
        try {
 
            if (!TextUtils.isEmpty(sdate) && (sdate.length() == 10 || sdate.length() == 9 || sdate.length() == 8)) {
                sdate = sdate + " 00:00";
            }
 
            Date time = getDateByFormat(sdate, dateFormatYMDHM);
 
            String ftime = "";
            Calendar cal = Calendar.getInstance();
            // 判断是否是同一天
            String curDate = getCurrentDate(dateFormatYMDHM);
            String paramDate = getStringByFormat(time, dateFormatYMDHM);
 
            if (curDate.equals(paramDate)) {
                int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
                if (hour == 0)
                    ftime = Math.max(
                            (cal.getTimeInMillis() - time.getTime()) / 60000, 1)
                            + "分钟前";
                else
                    ftime = hour + "小时前";
                return ftime;
            }
 
            long lt = time.getTime() / 86400000;
            long ct = cal.getTimeInMillis() / 86400000;
            int days = (int) (ct - lt);
            if (days == 0) {
                int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
                if (hour == 0)
                    ftime = Math.max(
                            (cal.getTimeInMillis() - time.getTime()) / 60000, 1)
                            + "分钟前";
                else
                    ftime = hour + "小时前";
            } else if (days == 1) {
                ftime = "昨天";
            } else if (days == 2) {
                ftime = "前天 ";
            } else if (days > 2 && days < 31) {
                ftime = days + "天前";
            } else if (days >= 31 && days <= 2 * 31) {
                ftime = "一个月前";
            } else if (days > 2 * 31 && days <= 3 * 31) {
                ftime = "2个月前";
            } else if (days > 3 * 31 && days <= 4 * 31) {
                ftime = "3个月前";
            } else {
                ftime = getStringByFormat(time, outFormat);
            }
            return ftime;
        } catch (Exception e) {
            return "";
        }
    }
 
    /**
     * 取指定日期为星期几.
     *
     * @param strDate  指定日期
     * @param inFormat 指定日期格式
     * @return String   星期几
     */
    public static String getWeekNumber(String strDate, String inFormat) {
        String week = "星期日";
        Calendar calendar = new GregorianCalendar();
        DateFormat df = new SimpleDateFormat(inFormat);
        try {
            calendar.setTime(df.parse(strDate));
        } catch (Exception e) {
            return "错误";
        }
        int intTemp = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        switch (intTemp) {
            case 0:
                week = "星期日";
                break;
            case 1:
                week = "星期一";
                break;
            case 2:
                week = "星期二";
                break;
            case 3:
                week = "星期三";
                break;
            case 4:
                week = "星期四";
                break;
            case 5:
                week = "星期五";
                break;
            case 6:
                week = "星期六";
                break;
            default:
                break;
        }
        return week;
    }
 
    /**
     * 根据给定的日期判断是否为上下午.
     *
     * @param strDate strDate
     * @param format  format
     * @return the time quantum
     */
    public static String getTimeQuantum(String strDate, String format) {
        Date mDate = getDateByFormat(strDate, format);
        int hour = mDate.getHours();
        if (hour >= 12)
            return "PM";
        else
            return "AM";
    }
 
    /**
     * 根据给定的毫秒数算得时间的描述.
     *
     * @param milliseconds the milliseconds
     * @return the time description
     */
    public static String getTimeDescription(long milliseconds) {
        if (milliseconds > 1000) {
            //大于一分
            if (milliseconds / 1000 / 60 > 1) {
                long minute = milliseconds / 1000 / 60;
                long second = milliseconds / 1000 % 60;
                return minute + "分" + second + "秒";
            } else {
                //显示秒
                return milliseconds / 1000 + "秒";
            }
        } else {
            return milliseconds + "毫秒";
        }
    }
 
 
    /**
     * 将Calendar转成字符串
     *
     * @param calendar calendar
     * @param format   指定格式
     * @return String
     */
    public static String calendarConvertString(Calendar calendar, String format) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            return simpleDateFormat.format(calendar.getTime());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
 
    /**
     * 将字符串转成Calendar
     *
     * @param times  times
     * @param format 指定格式
     * @return Calendar
     */
    public static Calendar stringConvertCalendar(String times, String format) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            Date date = simpleDateFormat.parse(times);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
 
 
    /***
     * 比较2个时间的大小
     *
     * @param one one
     * @param two two
     * @return int ==0相等 >0one大于two <0one小于two
     */
    public static int calendarCompare(Calendar one, Calendar two) {
        int num = -1;
        try {
            num = one.compareTo(two);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return num;
    }
 
    /**
     * 得到当前时间的随机数
     *
     * @return String
     */
    public static String getCurRandomDate() {
        Calendar calendar = Calendar.getInstance();
        String curTimes = calendar.get(Calendar.YEAR) + ""
                + (calendar.get(Calendar.MONTH) + 1) + ""
                + calendar.get(Calendar.DAY_OF_MONTH) + ""
                + calendar.get(Calendar.HOUR_OF_DAY) + ""
                + calendar.get(Calendar.MINUTE) + ""
                + calendar.get(Calendar.SECOND) + ""
                + calendar.get(Calendar.MILLISECOND);
 
        return curTimes;
    }
 
    /**
     * 某年某月有多少天
     *
     * @param year  year
     * @param month month
     * @return int
     */
    public static int getDaysInMonth(int year, int month) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        return c.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
 
    /**
     * 某年某月的第一天是周几
     *
     * @param year  year
     * @param month month
     * @return int
     */
    public static int getFirstWeekDayInMonth(int year, int month) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.DAY_OF_MONTH, 1);
        return c.get(Calendar.DAY_OF_WEEK) - 1;
    }
 
    /**
     * 得到2个时间戳之间相差的 天 时 分 秒
     *
     * @param date1 date1
     * @param date2 date2
     * @return long[]
     */
    public static long[] getDate(long date1, long date2) {
        long[] times = new long[]{0, 0, 0, 0};
        try {
            SimpleDateFormat dfs = new SimpleDateFormat(dateFormatYMDHMS);
            Date begin = dfs.parse(getStringByFormat(date1, dateFormatYMDHMS));
 
            Date end = dfs.parse(getStringByFormat(date2, dateFormatYMDHMS));
 
 
            long between = (end.getTime() - begin.getTime()) / 1000;//除以1000是为了转换成秒
 
            long day1 = between / (24 * 3600);
            long hour1 = between % (24 * 3600) / 3600;
            long minute1 = between % 3600 / 60;
            long second1 = between % 60 / 60;
            times[0] = day1;
            times[1] = hour1;
            times[2] = minute1;
            times[3] = second1;
        } catch (Exception ex) {
        }
        return times;
    }
 
 
}

  

posted @   bg_不够  阅读(358)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 我与微信审核的“相爱相杀”看个人小程序副业
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~
历史上的今天:
2021-05-27 MMKV使用笔记
点击右上角即可分享
微信分享提示