admin
2021-06-08 cca287441f277a3049cc280ae0d1e622d99d22ef
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
package com.tejia.lijin.app.util;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
/**
 * -
 * 判断时间 昨天 今天 前天
 */
public class TimeJudge {
    public static String showDate(Date date, String pattern) {
        String dateStr = format(date, pattern);
        String year = dateStr.substring(0, 4);
        Long yearNum = Long.parseLong(year);
        int month = Integer.parseInt(dateStr.substring(5, 7));
        int day = Integer.parseInt(dateStr.substring(8, 10));
        String hour = dateStr.substring(11, 13);
        String minute = dateStr.substring(14, 16);
 
        long addtime = date.getTime();
        long today = System.currentTimeMillis();//当前时间的毫秒数
        Date now = new Date(today);
        String nowStr = format(now, pattern);
        int nowDay = Integer.parseInt(nowStr.substring(8, 10));
        String result = "";
        long l = today - addtime;//当前时间与给定时间差的毫秒数
        long days = l / (24 * 60 * 60 * 1000);//这个时间相差的天数整数,大于1天为前天的时间了,小于24小时则为昨天和今天的时间
        long hours = (l / (60 * 60 * 1000) - days * 24);//这个时间相差的减去天数的小时数
        long min = ((l / (60 * 1000)) - days * 24 * 60 - hours * 60);//
        long s = (l / 1000 - days * 24 * 60 * 60 - hours * 60 * 60 - min * 60);
        if (days > 0) {
            if (days > 0 && days < 2) {
//                result = "前天" + hour + "点" + minute + "分";
                result = "前天";
            } else if (days > 1 && days < 10) {
//                result = "前天" + hour + "点" + minute + "分";
                result = days + "天前 " + hour + "." + minute;
            } else {
//                result = yearNum % 100 + "年" + month + "月 " + day + "日" + hour + "点" + minute + "分";
                result = yearNum % 100 + "." + month + ". " + day + "." + hour + ":" + minute;
            }
        } else if (hours > 0) {
            if (day != nowDay) {
//                result = "昨天" + hour + "点" + minute + "分";
                result = "昨天" + hour + "." + minute;
            } else {
//                result = hours + "小时 前";
                result = "今天" + format2(date, null);
            }
        } else if (min > 0) {
            if (min > 0 && min < 10) {
//                result = "刚刚";
//                result = "0" + min + "." + s;
                result = "今天" + format2(date, null);
            } else {
//                result = min + "分 前";
//                result = min + "." + s;
                result = "今天" + format2(date, null);
            }
        } else {
//            result = s + "秒 前";
            result = "今天" + format2(date, null);
        }
        return result;
    }
 
    /**
     * 日期格式化
     *
     * @param date    需要格式化的日期
     * @param pattern 时间格式,如:yyyy-MM-dd HH:mm:ss
     * @return 返回格式化后的时间字符串
     */
    public static String format(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }
 
    /**
     * 日期格式化
     *
     * @param date    需要格式化的日期
     * @param pattern 时间格式,如:  HH:mm
     * @return 返回格式化后的时间字符串
     */
    public static String format2(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        return sdf.format(date);
    }
 
    /**
     * 获取 当前时间 对比 存入时间之间的 天数
     *
     * @param date
     * @param pattern
     * @return
     */
    public static int getday(Date date, String pattern) {
        String dateStr = format(date, pattern);
        String year = dateStr.substring(0, 4);
        Long yearNum = Long.parseLong(year);
        int month = Integer.parseInt(dateStr.substring(5, 7));
        int day = Integer.parseInt(dateStr.substring(8, 10));
        String hour = dateStr.substring(11, 13);
        String minute = dateStr.substring(14, 16);
 
        long addtime = date.getTime();
        long today = System.currentTimeMillis();//当前时间的毫秒数
        Date now = new Date(today);
        String nowStr = format(now, pattern);
        int nowDay = Integer.parseInt(nowStr.substring(8, 10));
        String result = "";
        long l = today - addtime;//当前时间与给定时间差的毫秒数
        long days = l / (24 * 60 * 60 * 1000);//这个时间相差的天数整数,大于1天为前天的时间了,小于24小时则为昨天和今天的时间
//        long hours = (l / (60 * 60 * 1000) - days * 24);//这个时间相差的减去天数的小时数
//        long min = ((l / (60 * 1000)) - days * 24 * 60 - hours * 60);//
//        long s = (l / 1000 - days * 24 * 60 * 60 - hours * 60 * 60 - min * 60);
        if (days > 0) {
            return (int) days;
        } else {
            return 0;
        }
    }
 
    /**
     * 获取 当前时间 对比 存入时间
     * 返回 天 时 分 秒
     *
     * @param date
     * @param pattern
     * @return
     */
    public static int[] getTimeGroup(Date date, String pattern) {
 
        long addtime = date.getTime();
        long today = System.currentTimeMillis();//当前时间的毫秒数
        long l = today - addtime;//当前时间与给定时间差的毫秒数
        long days = l / (24 * 60 * 60 * 1000);//这个时间相差的天数整数,大于1天为前天的时间了,小于24小时则为昨天和今天的时间
        long hours = (l / (60 * 60 * 1000) - days * 24);//这个时间相差的减去天数的小时数
        long min = ((l / (60 * 1000)) - days * 24 * 60 - hours * 60);//
        long s = (l / 1000 - days * 24 * 60 * 60 - hours * 60 * 60 - min * 60);
 
        return new int[]{(int) days, (int) hours, (int) min, (int) s};
    }
 
    /**
     * 获取 当前时间 对比 存入时间
     * 返回 天 时 分 秒
     *
     * @param date
     * @return
     */
    public static long[] getTimeSecondTransformation(long date) {
//        Log.e("eee", "getTimeSecondTransformation: " + date);
        long days = date / (24 * 60 * 60 * 1000);//这个时间相差的天数整数,大于1天为前天的时间了,小于24小时则为昨天和今天的时间
        long hours = (date / (60 * 60 * 1000) - days * 24);//这个时间相差的减去天数的小时数
        long min = ((date / (60 * 1000)) - days * 24 * 60 - hours * 60);//
        long s = (date / 1000 - days * 24 * 60 * 60 - hours * 60 * 60 - min * 60);
        return new long[]{days, hours, min, s};
    }
}