wxr
2022-11-23 1e7b3abd15d37f6c6bc97ac14922457b9604c275
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
package com.mm.android.deviceaddmodule.mobilecommon.utils;
 
import android.content.Context;
import android.text.TextUtils;
 
import com.company.NetSDK.NET_TIME;
import com.mm.android.deviceaddmodule.R;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
 
public class TimeUtils {
    /**
     * 一天的总时间
     */
    public static final int TOTAL_SECONDS = 24 * 3600;
 
    public static final String LONG_FORMAT = "yyyy-MM-dd HH:mm:ss";
 
 
    public static final String BLE_MESSAGE_FORMAT = "yyyy年MM月dd天HH:mm:ss";
 
    public static final String SMB_REQUEST_FORMAT = "yyyyMMdd'T'HHmmss";
    public static final String REQUEST_FORMAT = "yyyyMMddHHmmss";
    public static final String REQUEST_DATE_FORMAT = "yyyyMMdd";
 
    public static final String COMMON_TIME_FORMAT = "%04d%02d%02d%02d%02d%02d";
    public static final String DH_TIME_FORMAT = "%04d%02d%02dT%02d%02d%02d";
 
    public static final String SHORT_FORMAT = "HH:mm:ss";
    public static final String SHORT_FORMAT2 = "HHmmss";
 
    public static final String SIMPLE_FORMAT = "HH:mm";
 
    public static String displayTime(Context context,long inputTime, String todayFormatStr, String yesterdayFormatStr,
                                     String otherFormatStr) {
        // 日期格式化
        SimpleDateFormat todayFormat = todayFormatStr != null ? TimeUtils.getDateFormatWithUS(todayFormatStr) : null;
        SimpleDateFormat yesterdayFormat = yesterdayFormatStr != null ? TimeUtils.getDateFormatWithUS(yesterdayFormatStr) : null;
        SimpleDateFormat otherFormat = otherFormatStr != null ? TimeUtils.getDateFormatWithUS(otherFormatStr) : null;
        String timeStr = null;
 
        // 获取当前凌晨时间
        Calendar c = Calendar.getInstance();
        String getYear = String.valueOf(c.get(Calendar.YEAR));
        String getMonth = String.valueOf(c.get(Calendar.MONTH) + 1);
        String getDayOfMonth = String.valueOf(c.get(Calendar.DAY_OF_MONTH));
        if (getMonth.length() == 1) {
            getMonth = "0" + getMonth;
        }
        if (getDayOfMonth.length() == 1) {
            getDayOfMonth = "0" + getDayOfMonth;
        }
        String currentStartTimeStr = getYear + "-" + getMonth + "-" + getDayOfMonth + " 00:00:00";
 
        // 当前凌晨时间格式转换
        java.sql.Timestamp currentStartTime = java.sql.Timestamp.valueOf(currentStartTimeStr);
        long currentStart = currentStartTime.getTime();
 
        // 与当前凌晨时间相差秒数
        long timeGap = (currentStart - inputTime) / 1000;
 
        // 输入时间:年
        if (timeGap <= 0) {
            timeStr = todayFormat != null ? todayFormat.format(inputTime) :context.getResources().getString(R.string.common_today); // 今天格式:10:00
        } else if (timeGap > 0 && timeGap <= 24 * 60 * 60) {
            timeStr = yesterdayFormat != null ? yesterdayFormat.format(inputTime) :context.getResources().getString(R.string.common_yesterday); // 昨天格式:昨天
        } else {
            timeStr = otherFormat != null ? otherFormat.format(inputTime) : String.valueOf(inputTime); // 其他格式:15/09/03
        }
 
        return timeStr;
    }
 
 
    public static NET_TIME Date2NetTime(Date date)
    {
        NET_TIME time     = new NET_TIME();
        time.dwYear     = date.getYear() + 1900;
        time.dwMonth    = date.getMonth() + 1;
        time.dwDay        = date.getDate();
        time.dwHour        = date.getHours();
        time.dwMinute    = date.getMinutes();
        time.dwSecond    = date.getSeconds();
        return time;
    }
 
    public static Date NetTimeToData(NET_TIME netTime)
    {
        Date date = new Date((int)netTime.dwYear - 1900,
                (int)netTime.dwMonth - 1,
                (int)netTime.dwDay,
                (int)netTime.dwHour,
                (int)netTime.dwMinute,
                (int)netTime.dwSecond);
        return date;
    }
 
    /**
     * 比较两个NET_TIME的大小
     * @param time1
     * @param time2
     * @return    an int < 0 if time1 is less than time2, 0 if they are equal, and an int > 0 if time1 is greater.
     */
    public static int compareNetTime(NET_TIME time1, NET_TIME time2)
    {
        Date date1 = new Date((int)time1.dwYear - 1900,
                (int)time1.dwMonth - 1,
                (int)time1.dwDay,
                (int)time1.dwHour,
                (int)time1.dwMinute,
                (int)time1.dwSecond);
 
        Date date2 = new Date((int)time2.dwYear - 1900,
                (int)time2.dwMonth - 1,
                (int)time2.dwDay,
                (int)time2.dwHour,
                (int)time2.dwMinute,
                (int)time2.dwSecond);
        return date1.compareTo(date2);
 
    }
    public static boolean isToday(Calendar calendar) {
        if(calendar == null){
            return false;
        }
        Calendar now = Calendar.getInstance();
        boolean isToday = now.get(Calendar.YEAR) == calendar
                .get(Calendar.YEAR)
                && (now.get(Calendar.MONTH) + 1) == (calendar
                .get(Calendar.MONTH) + 1)
                && now.get(Calendar.DATE) == calendar.get(Calendar.DATE)
                ;
        return isToday;
    }
 
    public static boolean isTodayOrBefore(Calendar calendar) {
        if(calendar == null){
            return false;
        }
        Calendar now = Calendar.getInstance();
 
        boolean isTodayOrBefore = now.get(Calendar.YEAR) == calendar
                .get(Calendar.YEAR)
                && (now.get(Calendar.MONTH) + 1) == (calendar
                .get(Calendar.MONTH) + 1)
                && now.get(Calendar.DATE) == calendar.get(Calendar.DATE)
                || calendar.before(now);
        return isTodayOrBefore;
    }
 
    public static boolean isBeforeToday(Calendar calendar) {
        if(calendar == null){
            return false;
        }
        Calendar now = Calendar.getInstance();
 
        boolean isTodayOrAfter = now.get(Calendar.YEAR) == calendar.get(Calendar.YEAR)
                && (now.get(Calendar.MONTH) + 1) == (calendar.get(Calendar.MONTH) + 1)
                && now.get(Calendar.DATE) == calendar.get(Calendar.DATE)
                || !calendar.before(now);
        return !isTodayOrAfter;
    }
    public static boolean isCurrentMonthOrBefore(Calendar calendar) {
        if(calendar == null){
            return false;
        }
        Calendar now = Calendar.getInstance();
 
        boolean isCurrentMonthOrBefore = now.get(Calendar.YEAR) == calendar
                .get(Calendar.YEAR)
                && (now.get(Calendar.MONTH) + 1) == (calendar
                .get(Calendar.MONTH) + 1)
                || !calendar.before(now);
        return !isCurrentMonthOrBefore;
    }
 
    /**
     * String2Date
     * <p>
     * </p>
     *
     * @param dateStr
     * @param format
     * @return
     */
    public static Date stringToDate(String dateStr, String format) {
        SimpleDateFormat formatter = TimeUtils.getDateFormatWithUS(format);
        Date strtodate = null;
        try {
            strtodate = formatter.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }
        return strtodate;
    }
 
    /**
     * 服务器0时区字符串转换才本地时间
     * @param dateStr
     * @param format
     * @return
     */
    public static long getLocalTimeByString(String dateStr, String format){
        if(TextUtils.isEmpty(dateStr)){
            return 0;
        }
 
        long time = TimeUtils.stringToDate(dateStr, format).getTime();
        return time = time + getTimeZone() * 1000; //加偏移量
    }
 
 
    /**
     * long转String
     * <p>
     * </p>
     * @param milliseconds
     *            the number of milliseconds since Jan. 1, 1970 GMT.
     * @param formatStr
     *            要转化成的时间格式
     * @return
     */
    public static String long2String(long milliseconds, String formatStr) {
        Date date = new Date(milliseconds);
        SimpleDateFormat format = TimeUtils.getDateFormatWithUS(formatStr);
        return format.format(date);
    }
 
    /**
     * string2String
     * <p>
     * </p>
     * @param dateStr
     *            被转换的时间字符串
     * @param formatFrom
     *            转化前的时间格式
     * @param formatTo
     *            转化后的时间格式
     * @return
     */
    public static String string2String(String dateStr, String formatFrom, String formatTo) {
        SimpleDateFormat formatF = TimeUtils.getDateFormatWithUS(formatFrom);
        try {
            Date date = formatF.parse(dateStr);
            SimpleDateFormat formatT = TimeUtils.getDateFormatWithUS(formatTo);
            return formatT.format(date);
        } catch (ParseException e1) {
            e1.printStackTrace();
            return dateStr;
        }
    }
 
    public static String string2StringForReport(String dateStr, String formatFrom, String formatTo) {
        SimpleDateFormat formatF = TimeUtils.getDateFormatWithUS(formatFrom);
        try {
            Date date = formatF.parse(dateStr);
            SimpleDateFormat formatT = TimeUtils.getDateFormatWithUS(formatTo);
            return formatT.format(date);
        } catch (ParseException e1) {
            e1.printStackTrace();
            return "--";
        }
    }
 
    /**
     * date2Str
     * <p>
     * </p>
     * @param d
     *            被转化的Date
     * @param format
     *            转化格式
     * @return
     */
    public static String date2String(Date d, String format) {
        if(d == null) {
            return "";
        }
        SimpleDateFormat formatter = TimeUtils.getDateFormatWithUS(format);
        return formatter.format(d);
    }
 
    public static Date string2hhmm(String strDate) {
        Date mDate = stringToDate2(strDate, LONG_FORMAT);
        if (mDate == null) {
            mDate = stringToDate2(strDate, SHORT_FORMAT);
        }
        if (mDate == null) {
            mDate = stringToDate2(strDate, SIMPLE_FORMAT);
        }
        return mDate;
    }
 
    private static Date stringToDate2(String dateStr, String format) {
        SimpleDateFormat formatter = TimeUtils.getDateFormatWithUS(format);
        Date strtodate = null;
        try {
            strtodate = formatter.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return strtodate;
    }
 
 
 
    public static String getNewRequestTime(long time){
        Date d = new Date(time);
        return date2String(d,REQUEST_DATE_FORMAT)+"T"+date2String(d,SHORT_FORMAT2);
    }
 
    public static long getResponseTime(String dateString){
        if(dateString==null)
            return 0;
        if(dateString.contains("T")){
            dateString = dateString.replace("T" ,"");
        }
        Date date = stringToDate(dateString,REQUEST_FORMAT);
        if (date == null)
            return 0;
        return date.getTime();
    }
 
    /**
     * 获取时间戳
     * @param dateString   0时区的时间
     * @return
     */
    public static long getResponseStamp(String dateString){
        if(dateString==null)
            return 0;
        if(dateString.contains("T")){
            dateString = dateString.replace("T" ,"");
        }
 
        SimpleDateFormat formatter = TimeUtils.getDateFormatWithUS(REQUEST_FORMAT);
        formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date strtodate = null;
        try {
            strtodate = formatter.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
 
        if (strtodate == null)
            return 0;
        return strtodate.getTime();
    }
    public static String setOnceTime(String time) {
        Date d = string2hhmm(time);
        Date now = new Date();
        if (d!=null && d.before(now)) {
            // 加一天
            long tomorro = d.getTime() + 24 * 60 * 60 * 1000;
            Date tomorr = new Date(tomorro);
            return date2String(tomorr, LONG_FORMAT);
        }
        return date2String(d, LONG_FORMAT);
    }
 
    /**
     * 格式化显示时间
     *
     * @param inputTime
     *            毫秒
     * @param inputTime 输入时间(UNIX时间戳毫秒)
     * @return
     */
    public static String displayTime(long inputTime, String todayFormatStr, String yesterdayFormatStr,
                                     String otherFormatStr) {
        // 日期格式化
        SimpleDateFormat todayFormat = todayFormatStr != null ? TimeUtils.getDateFormatWithUS(todayFormatStr) : null;
        SimpleDateFormat yesterdayFormat = yesterdayFormatStr != null ? TimeUtils.getDateFormatWithUS(yesterdayFormatStr) : null;
        SimpleDateFormat otherFormat = otherFormatStr != null ? TimeUtils.getDateFormatWithUS(otherFormatStr) : null;
        String timeStr = null;
 
        // 获取当前凌晨时间
        Calendar c = Calendar.getInstance();
        String getYear = String.valueOf(c.get(Calendar.YEAR));
        String getMonth = String.valueOf(c.get(Calendar.MONTH) + 1);
        String getDayOfMonth = String.valueOf(c.get(Calendar.DAY_OF_MONTH));
        if (getMonth.length() == 1) {
            getMonth = "0" + getMonth;
        }
        if (getDayOfMonth.length() == 1) {
            getDayOfMonth = "0" + getDayOfMonth;
        }
        String currentStartTimeStr = getYear + "-" + getMonth + "-" + getDayOfMonth + " 00:00:00";
 
        // 当前凌晨时间格式转换
        java.sql.Timestamp currentStartTime = java.sql.Timestamp.valueOf(currentStartTimeStr);
        long currentStart = currentStartTime.getTime();
 
        // 与当前凌晨时间相差秒数
        long timeGap = (currentStart - inputTime) / 1000;
 
        // 输入时间:年
        if (timeGap <= 0) {
            timeStr = todayFormat != null ? todayFormat.format(inputTime) : "今天  "; // 今天格式:10:00
        } else if (timeGap > 0 && timeGap <= 24 * 60 * 60) {
            timeStr = yesterdayFormat != null ? yesterdayFormat.format(inputTime) : "昨天  "; // 昨天格式:昨天
        } else {
            timeStr = otherFormat != null ? otherFormat.format(inputTime) : String.valueOf(inputTime); // 其他格式:15/09/03
        }
 
        return timeStr;
    }
 
    /**
     * 我的文件、报警消息列表悬停头的文案 今天 、昨天、 05/07、15/12/19
     *
     * @param strTimestamp
     *            输入时间(格式:"yyyy-MM-dd HH:mm:ss")
     * @return
     */
    public static String getStickxinzaieader(String strTimestamp) {
        // 日期格式化
        SimpleDateFormat xinzai = TimeUtils.getDateFormatWithUS("yy/MM/dd");
        SimpleDateFormat mh = TimeUtils.getDateFormatWithUS("MM/dd");
        SimpleDateFormat y = TimeUtils.getDateFormatWithUS("yyyy");
        String timeStr = null;
 
        // 获取当前凌晨时间
        Calendar c = Calendar.getInstance();
        String getYear = String.valueOf(c.get(Calendar.YEAR));
        String getMonth = String.valueOf(c.get(Calendar.MONTH) + 1);
        String getDayOfMonth = String.valueOf(c.get(Calendar.DAY_OF_MONTH));
        if (getMonth.length() == 1) {
            getMonth = "0" + getMonth;
        }
        if (getDayOfMonth.length() == 1) {
            getDayOfMonth = "0" + getDayOfMonth;
        }
        String currentStartTimeStr = getYear + "-" + getMonth + "-" + getDayOfMonth + " 00:00:00";
 
        // 当前凌晨时间格式转换
        java.sql.Timestamp currentStartTime = java.sql.Timestamp.valueOf(currentStartTimeStr);
        long currentStart = currentStartTime.getTime();
 
        // 输入时间格式转换
        java.sql.Timestamp time = java.sql.Timestamp.valueOf(strTimestamp);
        long timestamp = time.getTime();
 
        // 与当前凌晨时间相差秒数
        long timeGap = (currentStart - timestamp) / 1000;
 
        // 输入时间:年
        String year = y.format(timestamp);
 
        if (timeGap <= 0) {
            timeStr = "今天  "; // 格式:今天
        } else if (timeGap > 0 && timeGap <= 24 * 60 * 60) {
            timeStr = "昨天  "; // 格式:昨天
        } else if (year.equals(String.valueOf(getYear))) {
            timeStr = mh.format(timestamp); // (年内)MM月dd日
        } else {
            timeStr = xinzai.format(timestamp); // (年前)格式:yyyy年MM月dd日
        }
 
        return timeStr;
    }
 
    /**
     * 我的文件、报警消息列表悬停头的文案 今天 、昨天、 05/07、15/12/19
     *
     * @param timestamp 输入时间UNIX时间戳毫秒
     * @return
     */
    public static String getStickxinzaieader(long timestamp) {
        // 日期格式化
        SimpleDateFormat xinzai = TimeUtils.getDateFormatWithUS("yy/MM/dd");
        SimpleDateFormat mh = TimeUtils.getDateFormatWithUS("MM/dd");
        SimpleDateFormat y = TimeUtils.getDateFormatWithUS("yyyy");
        String timeStr = null;
 
        // 获取当前凌晨时间
        Calendar c = Calendar.getInstance();
        String getYear = String.valueOf(c.get(Calendar.YEAR));
        String getMonth = String.valueOf(c.get(Calendar.MONTH) + 1);
        String getDayOfMonth = String.valueOf(c.get(Calendar.DAY_OF_MONTH));
        if (getMonth.length() == 1) {
            getMonth = "0" + getMonth;
        }
        if (getDayOfMonth.length() == 1) {
            getDayOfMonth = "0" + getDayOfMonth;
        }
        String currentStartTimeStr = getYear + "-" + getMonth + "-" + getDayOfMonth + " 00:00:00";
 
        // 当前凌晨时间格式转换
        java.sql.Timestamp currentStartTime = java.sql.Timestamp.valueOf(currentStartTimeStr);
        long currentStart = currentStartTime.getTime();
 
        // 与当前凌晨时间相差秒数
        long timeGap = (currentStart - timestamp) / 1000;
 
        // 输入时间:年
        String year = y.format(timestamp);
 
        if (timeGap <= 0) {
            timeStr = "今天  "; // 格式:今天
        } else if (timeGap > 0 && timeGap <= 24 * 60 * 60) {
            timeStr = "昨天  "; // 格式:昨天
        } else if (year.equals(String.valueOf(getYear))) {
            timeStr = mh.format(timestamp); // (年内)MM月dd日
        } else {
            timeStr = xinzai.format(timestamp); // (年前)格式:yyyy年MM月dd日
        }
 
        return timeStr;
    }
 
    /**
     * 获取当前年的第一天
     * <p>
     * </p>
     *
     * @return
     */
    public static Date getCurrentYearFirst() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        return getYearFirst(year);
    }
    /**
     * 获取某一年的第一天
     * <p>
     * </p>
     * @param year
     * @return
     */
    public static Date getYearFirst(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date firstDay = calendar.getTime();
        return firstDay;
    }
 
    /**
     * 获取当前年的最后一天
     * <p>
     * </p>
     * @return
     */
    public static Date getCurrentYearLast() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        return getYearLast(year);
    }
    /**
     * 获取某一年的最后一天
     * <p>
     * </p>
     * @param year
     * @return
     */
    public static Date getYearLast(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date lastDay = calendar.getTime();
        return lastDay;
    }
 
    /**
     * 在输入Date的基础上加/减某段时间,返回一个新的Date
     * <p>
     * </p>
     * @param date
     * @param field
     * @param value
     * @return
     */
    public static Date add(Date date, int field, int value) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        calendar.add(field, value);
        return calendar.getTime();
    }
 
 
    /**
     * 直播分享时间格式
     */
    public static  String getTime(long time) {
        time = time +System.currentTimeMillis();
        Date date = new Date(time);
        SimpleDateFormat format = TimeUtils.getDateFormatWithUS("MM月dd日 HH:mm");
        return  format.format(date);
 
    }
 
    public static String getTimeSec() {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat format = TimeUtils.getDateFormatWithUS("yyyy-MM-dd HH:mm:ss");
        return  format.format(date);
 
    }
 
    /*用户融合添加*/
 
    /*获取手机时区----UTC格式*/
    public static int getTimeZone(){
        Calendar cal = Calendar.getInstance(Locale.getDefault());
        int zoneOffset = cal.get(Calendar.ZONE_OFFSET);
        int zone = zoneOffset/1000;
 
        return zone;
    }
 
    /*获取时间字符串:从yyyyMMddTHHmmssZ时间格式到yyyy-MM-dd HH:mm*/
    public static String getTimeFromUTC(String timeStr) {
        if(TextUtils.isEmpty(timeStr)) {
            return "";
        }
        Date timeDate = TimeUtils.stringToDate(timeStr, "yyyyMMdd'T'HHmmss'Z'");
        if(timeDate == null) {
            return "";
        }
        long time = timeDate.getTime();
//        time = time + getTimeZone() * 1000; //加偏移量
        Date date = new Date(time);
        SimpleDateFormat sdf = TimeUtils.getDateFormatWithUS("yyyy-MM-dd HH:mm");
        return sdf.format(date);
    }
 
    /**
     * 我的文件、报警消息列表悬停头的文案 今天 、昨天、 05/07、15/12/19
     *
     * @param context 上下文
     * @param timestamp 输入时间UNIX时间戳毫秒
     * @return
     */
    public static String getStickxinzaieader(Context context,long timestamp) {
        // 日期格式化
        SimpleDateFormat xinzai = TimeUtils.getDateFormatWithUS("yy/MM/dd");
        SimpleDateFormat mh = TimeUtils.getDateFormatWithUS("MM/dd");
        SimpleDateFormat y = TimeUtils.getDateFormatWithUS("yyyy");
        String timeStr = null;
 
        // 获取当前凌晨时间
        Calendar c = Calendar.getInstance();
        String getYear = String.valueOf(c.get(Calendar.YEAR));
        String getMonth = String.valueOf(c.get(Calendar.MONTH) + 1);
        String getDayOfMonth = String.valueOf(c.get(Calendar.DAY_OF_MONTH));
        if (getMonth.length() == 1) {
            getMonth = "0" + getMonth;
        }
        if (getDayOfMonth.length() == 1) {
            getDayOfMonth = "0" + getDayOfMonth;
        }
        String currentStartTimeStr = getYear + "-" + getMonth + "-" + getDayOfMonth + " 00:00:00";
 
        // 当前凌晨时间格式转换
        java.sql.Timestamp currentStartTime = java.sql.Timestamp.valueOf(currentStartTimeStr);
        long currentStart = currentStartTime.getTime();
 
        // 与当前凌晨时间相差秒数
        long timeGap = (currentStart - timestamp) / 1000;
 
        // 输入时间:年
        String year = y.format(timestamp);
 
        if (timeGap <= 0) {
            timeStr = context.getResources().getString(R.string.common_today); // 格式:今天
        } else if (timeGap > 0 && timeGap <= 24 * 60 * 60) {
            timeStr = context.getResources().getString(R.string.common_yesterday); // 格式:昨天
        } else if (year.equals(String.valueOf(getYear))) {
            timeStr = mh.format(timestamp); // (年内)MM月dd日
        } else {
            timeStr = xinzai.format(timestamp); // (年前)格式:yyyy年MM月dd日
        }
 
        return timeStr;
    }
 
    //获取当前时区与0时区差值
    public static int getTimeOffset(){
        Calendar calendar=Calendar.getInstance(TimeZone.getDefault());
        int offset=calendar.get(Calendar.ZONE_OFFSET);
        return offset/1000;
    }
 
    /**
     *
     * @param time 带时区的时间戳(时间单位为mS)
     * @return
     */
    public static long change2UTC(long time) {
        SimpleDateFormat format = TimeUtils.getDateFormatWithUS("yyyy-MM-dd HH:mm:ss");
        format.setTimeZone(TimeZone.getTimeZone("UTC"));
        String timeStr=format.format(new Date(time));
        Date date = string2hhmm(timeStr);
        return date != null ? date.getTime() : 0;
    }
 
    /**
     *
     * @param time UTC时间戳(时间单位为mS)
     * @return
     */
    public static long change2Local(long time){
        SimpleDateFormat format = TimeUtils.getDateFormatWithUS("yyyy-MM-dd HH:mm:ss");
        format.setTimeZone(TimeZone.getDefault());
        String timeStr=format.format(new Date(time + getTimeOffset() * 1000));
        Date date = string2hhmm(timeStr);
        return date != null ? date.getTime() : 0;
    }
 
    /**
     *将服务返回的时间字串格式转换为yyyy-MM-dd HH:mm:ss格式
     * @param time 服务返回设备时间
     * @return
     */
    public static String changeTimeFormat2Standard(String time){
        SimpleDateFormat format = TimeUtils.getDateFormatWithUS("yyyyMMdd'T'HHmmss");
        Date date=null;
        try {
            date=format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat standardFormat = TimeUtils.getDateFormatWithUS("yyyy-MM-dd HH:mm:ss");
        String standardTimeStr=date==null?"":standardFormat.format(date);
        return standardTimeStr;
    }
 
    /**
     *将服务返回的时间字串格式转换为MM-dd HH:mm格式
     * @param time 服务返回设备时间
     * @return
     */
    public static String changeTimeFormat2StandardNoYear(String time){
        SimpleDateFormat format = TimeUtils.getDateFormatWithUS("yyyyMMdd'T'HHmmss");
        Date date=null;
        try {
            date=format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat standardFormat = TimeUtils.getDateFormatWithUS("MM-dd HH:mm:ss");
        String standardTimeStr=date==null?"":standardFormat.format(date);
        return standardTimeStr;
    }
 
    /**
     *将服务返回的时间字串格式转换为yyyy-MM-dd HH:mm格式
     * @param time 服务返回设备时间
     * @return
     */
    public static String changeTimeFormat2StandardMin(String time){
        SimpleDateFormat format = TimeUtils.getDateFormatWithUS("yyyyMMdd'T'HHmmss");
        Date date=null;
        try {
            date=format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat standardFormat = TimeUtils.getDateFormatWithUS("yyyy-MM-dd HH:mm");
        String standardTimeStr=date==null?"":standardFormat.format(date);
        return standardTimeStr;
    }
 
    /**
     *将服务返回的时间字串格式转换为长整型
     * @param time 服务返回设备时间
     * @return
     */
    public static long changeTimeStrToStamp(String time){
        SimpleDateFormat format = TimeUtils.getDateFormatWithUS("yyyyMMdd'T'HHmmss");
        Date date=null;
        try {
            date=format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long timeStamp=date==null?0:date.getTime();
        return timeStamp;
    }
 
    /**
     *将服务返回的时间字串格式转换为outTimeformat格式
     * @param time 服务返回设备时间
     *             @param outTimeformat 自定义时间格式 , 0时区时间转换
     * @return
     */
    public static String changeTimeFormat2StandardMinByDateFormat(String time,String outTimeformat){
        SimpleDateFormat format = TimeUtils.getDateFormatWithUS("yyyyMMdd'T'HHmmss'Z'");
        Date date=null;
        try {
            date=format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }catch (NullPointerException e){
            e.printStackTrace();
        }
        SimpleDateFormat standardFormat = TimeUtils.getDateFormatWithUS(outTimeformat);
        if(date == null)return "";
        date.setTime(date.getTime()+ TimeUtils.getTimeZone() * 1000);//加偏移量
        String standardTimeStr= standardFormat.format(date);
        return standardTimeStr;
    }
 
    //返回 0时区 毫秒值
    public static long changeTime2UTCStamp(String time, String dateFormat) {
        SimpleDateFormat format = TimeUtils.getDateFormatWithUS(dateFormat);
        format.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date date = null;
        try {
            date = format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date == null ? 0 : date.getTime();
    }
 
    /**
     * 不带年份的时间转时间戳
     * @param time
     * @return
     */
    public static long changeDateToUnix(String time) {
        SimpleDateFormat sdf = TimeUtils.getDateFormatWithUS("MM-dd HH:mm");
        Date date = new Date();
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }
 
 
    /**
     * 转成大华标准时间 yyyyMMddTHHmmss
     * @return
     */
    public static String changeTimeFormat(String timeFormat) {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            return String.format(timeFormat,
                    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));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
 
    /**
     * 按周设置夏令时,保存时将
     * Mar 2nd Sun 00:00
     * 格式转换成
     * 3-2-0 00:00:00 格式
     * 3--1-1 代表三月最后一个周一
     * 月份 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
     第几个 "1st","2nd","3rd","4th","5th"
     周几 "Mon","Tue","Wed","Thu","Fri","Sat","Sun"
 
     月,是从1开始,1~12
     周是从1开始,1~4,以及-1,-1表示最后一周,或第四周是最后一周,也可以用4表示
     从0~6,0表示周日
 
     * @param time
     * @return
     */
    private static final String[] months = new String[]{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
    private static final String[] numweeks = new String[]{"1st","2nd","3rd","4th","last"};
    private static final String[] weekDays = new String[]{"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
 
    public static String changeEngStrToNumStr(String time)
    {
        StringBuffer buffer = new StringBuffer();
        if(!TextUtils.isEmpty(time))
        {
            String[] array = time.split(" ");
            if(array!=null && array.length==4)
            {
                String month = array[0];
                String numWeek = array[1];
                String weekDay = array[2];
                String date = array[3];
 
                for(int i=0; i<months.length; i++)
                {
                    if(months[i].equalsIgnoreCase(month))
                    {
                        buffer.append(i+1).append("-");
                        break;
                    }
                }
 
                for(int i=0; i<numweeks.length; i++)
                {
                    if(numweeks[i].equalsIgnoreCase(numWeek) && !"last".equalsIgnoreCase(numWeek))
                    {
                        buffer.append(i+1).append("-");
                        break;
                    }else if("last".equalsIgnoreCase(numWeek)) //最后一周用-1表示
                    {
                        buffer.append("-1").append("-");
                        break;
                    }
                }
 
                for(int i=0; i<weekDays.length; i++)
                {
                    if(weekDays[i].equalsIgnoreCase(weekDay) && !"Sun".equalsIgnoreCase(weekDay))
                    {
                        buffer.append(i+1).append(" ");
                        break;
                    }else if("Sun".equalsIgnoreCase(weekDay))
                    {
                        //周日对应到0
                        buffer.append("0").append(" ");
                        break;
                    }
                }
 
                buffer.append(date).append(":00"); //最后增加 “秒”的两位
            }else
            {
                return "";
            }
        }
        return buffer.toString();
    }
 
    /**
     * 将数字格式字符转换为英文格式
     * 03-02-00 00:00:00
     * 03--1-00 00:00:00
     * 格式转换成
     * Mar 2nd Sun 00:00
     * @param time
     * @return
     */
    public static String changeNumStrToEngStr(String time)
    {
        StringBuffer buffer = new StringBuffer();
        boolean hasLastFlag = false; //是否包含最后一个的标识 -1
 
        if(time!=null)
        {
            if(time.contains("-1"))
            {
                time = time.replace("-1","last");
                hasLastFlag = true;
            }
            String[] array = time.split(" ");
            String[] dateArray = null;
            String[] timeArray = null;
 
            if(array!=null && array.length==2)
            {
                dateArray = array[0].split("-");
                timeArray = array[1].split(":");
            }else
            {
                return "";
            }
 
            String month = null;
            String numWeek = null;
            String weekDay = null;
            String hour = null;
            String minute = null;
 
            if(dateArray!=null && dateArray.length==3 && timeArray!=null && timeArray.length==3)
            {
                month = dateArray[0];
                numWeek = dateArray[1];
                weekDay = dateArray[2];
                hour = timeArray[0];
                minute = timeArray[1];
            }else
            {
                return "";
            }
 
            if(month!=null && month.length()==2)
            {
                if("0".equals(month.substring(0,1)))
                {
                    int index = Integer.parseInt(month.substring(1))-1;
                    if(index<0 || index>11)
                    {
                        return "";
                    }else
                    {
                        buffer.append(months[index]).append(" ");
                    }
                }else
                {
                    int index = Integer.parseInt(month)-1;
                    if(index<0 || index>11)
                    {
                        return "";
                    }else
                    {
                        buffer.append(months[index]).append(" ");
                    }
                }
            }
 
            if(hasLastFlag)
            {
                buffer.append("last").append(" ");
 
            }else if(numWeek!=null && !numWeek.equalsIgnoreCase("last"))
            {
                int index = Integer.parseInt(numWeek.substring(1))-1;
                if(index<0 || index >4)
                {
                    return "";
                }else
                {
                    buffer.append(numweeks[index]).append(" ");
                }
            }
 
            if(weekDay!=null  && weekDay.equals("00"))        //00 是周日,不能按常规判断
            {
                buffer.append(weekDays[6]).append(" ");
            }
            else if(weekDay!=null  && weekDay.length()==2 && !weekDay.equals("00"))
            {
                int index = Integer.parseInt(weekDay.substring(1))-1;
                if(index<0 || index >6)
                {
                    return "";
                }else
                {
                    buffer.append(weekDays[index]).append(" ");
                }
            }
            buffer.append(hour).append(":").append(minute);
        }
        return buffer.toString();
    }
 
    /**
     * 使用默认英语格式化时间,避免多语言环境下(如阿拉伯语)格式化的时间服务器无法识别
     * @param formate
     * @return
     */
    public static SimpleDateFormat getDateFormatWithUS(String formate){
        return  new SimpleDateFormat(formate, Locale.US);
    }
 
    /**
     * 计算两时间戳相差天数
     * @param startTime
     * @param endTime
     * @return
     */
    public static long dateDiff(long startTime, long endTime) {
        long nd = 24 * 60 * 60;// 一天的秒数
        long nh = 60 * 60;// 一小时的秒数
        long nm = 60;// 一分钟的秒数
        long ns = 1;// 一秒钟的秒数
        long diff;
        long day = 0;
        // 获得两个时间的毫秒时间差异
        diff = endTime - startTime;
        day = diff / nd;// 计算差多少天
        long hour = diff % nd / nh;// 计算差多少小时
        long min = diff % nd % nh / nm;// 计算差多少分钟
        long sec = diff % nd % nh % nm / ns;// 计算差多少秒
        if (day >= 1) {
            return day;
        } else {
            if (day == 0) {
                return 0;
            } else {
                return 0;
            }
 
        }
    }
 
    public static long getDateAddDays(int days){
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, days);
        return c.getTimeInMillis();
 
    }
 
    /**
     * 时间戳转换成日期格式字符串
     * @param seconds 精确到秒的字符串
     * @param format
     * @return
     */
    public static String timeStamp2Date(String seconds,String format) {
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
            return "";
        }
        if(format == null || format.isEmpty()){
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds+"000")));
    }
}