Runt
2022-08-17 2a320dc04d6deb22116ebfd302d6f898a15f23af
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
package com.runt.open.mvvm.util;
 
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
/**
 * @purpose Created by Runt (qingingrunt2010@qq.com) on 2020-9-17.
 */
public class HandleDate {
 
    private static SimpleDateFormat secondsdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat timesdf = new SimpleDateFormat("HH:mm:ss");
    private static SimpleDateFormat datesdf = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat hoursdf = new SimpleDateFormat("yyyy-MM-dd HH");
    private static SimpleDateFormat minutesdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
 
    /**
     * 获取当前时间的String类型
     */
    public static long getDateToLong() {
        return new Date().getTime();
    }
 
    /**
     * 获取指定时间的long类型
     */
    public static long getDateToLong(Date date) {
 
        return date.getTime();
    }
 
    /**
     * 获取指定时间的long类型
     *
     * @throws ParseException
     */
    public static long getDateToLong(String datestr) throws ParseException {
 
        return datesdf.parse(datestr).getTime();
    }
 
    /**
     * 将long类型的时间转换成只有日期的int类型
     */
    public static int getDateToInt(long datetime) {
 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date date = getLongToDate(datetime);
        String format = sdf.format(date);
        return Integer.parseInt(format);
    }
 
    /**
     * 将指定long类型的日期转换为date
     */
    public static Date getLongToDate(Long datetime) {
        return new Date(datetime);
    }
 
    /**
     * 将指定long类型的日期转换为string时间精确到秒
     */
    public static String getLongToSecond(Long datetime) {
        Date date = new Date(datetime);
        return secondsdf.format(date);
    }
 
    public static long getDateTimeToLong(String datetime) {
        try {
            return secondsdf.parse(datetime).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }
 
    /**
     * 将指定long类型的日期转换为string时间 只显示 时分秒
     */
    public static String getLongToTime(Long datetime) {
        String datestr = "";
        Date date = null;
        try {
            date = new Date(datetime);
            datestr = timesdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return datestr;
    }
 
    /**
     * 将指定long类型的日期转换为string时间只显示日期
     */
    public static String getLongToDatestr(Long datetime) {
        String datestr = "";
        Date date = null;
        try {
            date = new Date(datetime);
            datestr = datesdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return datestr;
    }
 
    /**
     * 将指定long类型的日期转换为string时间精确到小时
     */
    public static String getLongToHour(Long datetime) {
        String datestr = "";
        Date date = null;
        try {
            date = new Date(datetime);
            datestr = hoursdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return datestr;
    }
 
    /**
     * 将指定long类型的日期转换为string日期
     */
    public static String getLongToSimpleDate(Long datetime) {
        String datestr = "";
        Date date = null;
        try {
            date = new Date(datetime);
            datestr = datesdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return datestr;
    }
 
    /**
     * 根据毫秒时间戳来格式化字符串 今天显示今天、昨天显示昨天、前天显示前天. 早于前天的显示具体年-月-日,如2017-06-12;
     *
     * @param timeStamp 毫秒值
     * @return 今天 昨天 前天 或者 yyyy-MM-dd HH:mm:ss类型字符串
     */
    public static String format(long timeStamp) {
        long curTimeMillis = System.currentTimeMillis();
        Date curDate = new Date(curTimeMillis);
        int todayHoursSeconds = curDate.getHours() * 60 * 60;
        int todayMinutesSeconds = curDate.getMinutes() * 60;
        int todaySeconds = curDate.getSeconds();
        int todayMillis = (todayHoursSeconds + todayMinutesSeconds + todaySeconds) * 1000;
        long todayStartMillis = curTimeMillis - todayMillis;
        if (timeStamp >= todayStartMillis) {
            return "今天";
        }
        int oneDayMillis = 24 * 60 * 60 * 1000;
        long yesterdayStartMilis = todayStartMillis - oneDayMillis;
        if (timeStamp >= yesterdayStartMilis) {
            return "昨天";
        }
        long yesterdayBeforeStartMilis = yesterdayStartMilis - oneDayMillis;
        if (timeStamp >= yesterdayBeforeStartMilis) {
            return "前天";
        }
        //            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date(timeStamp));
    }
 
    /**
     * 根据时间戳来判断当前的时间是几天前,几分钟,刚刚
     *
     * @param long_time
     * @return
     */
 
    public static String getTimeStateNew(String long_time) {
        String long_by_13 = "1000000000000";
        String long_by_10 = "1000000000";
        if (Long.valueOf(long_time) / Long.valueOf(long_by_13) < 1) {
            if (Long.valueOf(long_time) / Long.valueOf(long_by_10) >= 1) {
                long_time = long_time + "000";
            }
        }
        return getTimeStateNew(long_time);
    }
    public static String getTimeStateNew(Date long_time) {
        return getTimeStateNew(long_time.getTime() );
    }
    public static String getTimeStateNew(Long long_time) {
        Timestamp time = new Timestamp(Long.valueOf(long_time));
        Timestamp now = new Timestamp(System.currentTimeMillis());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //         System.out.println("传递过来的时间:"+format.format(time));
        //         System.out.println("现在的时间:"+format.format(now));
        long day_conver = 1000 * 60 * 60 * 24;
        long hour_conver = 1000 * 60 * 60;
        long min_conver = 1000 * 60;
        long time_conver = now.getTime() - time.getTime();
        long temp_conver;
        //         System.out.println("天数:"+time_conver/day_conver);
        if ((time_conver / day_conver) < 3) {
            temp_conver = time_conver / day_conver;
            if (temp_conver <= 2 && temp_conver >= 1) {
                return temp_conver + "天前";
            } else {
                temp_conver = (time_conver / hour_conver);
                if (temp_conver >= 1) {
                    return temp_conver + "小时前";
                } else {
                    temp_conver = (time_conver / min_conver);
                    if (temp_conver >= 1) {
                        return temp_conver + "分钟前";
                    } else {
                        return "刚刚";
                    }
                }
            }
        } else {
            return format.format(time);
        }
    }
 
 
}