forked from chaychan/BlogFileResource
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeUtils.java
More file actions
348 lines (308 loc) · 11.1 KB
/
TimeUtils.java
File metadata and controls
348 lines (308 loc) · 11.1 KB
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
package com.youjiang.hualuo.utils;
import android.text.format.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
* @author ChayChan
* @description: 关于时间的工具类
*/
public class TimeUtils {
public static final long ONE_MINUTE_MILLIONS = 60 * 1000;
public static final long ONE_HOUR_MILLIONS = 60 * ONE_MINUTE_MILLIONS;
public static SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
/**
* 得到剩余天数
*
* @param endTime 结束时间
* @return
*/
public static int getDayLast(String endTime) {
try {
long nowtime = new Date().getTime();
long lastTime = new SimpleDateFormat("yyyy-MM-dd").parse(endTime).getTime();
long distance = lastTime - nowtime;
if (distance <= 0) {
//如果是小于或等于0,则为0
return 0;
}
double rate = distance / (1.0f * 24 * 3600 * 1000);
int day = (int) (rate + 0.5f);
return day;
} catch (ParseException e) {
e.printStackTrace();
return -1;
}
}
/**
* 获取短时间格式
*
* @return
*/
public static String getShortTime(long millis) {
Date date = new Date(millis);
Date curDate = new Date();
String str = "";
long durTime = curDate.getTime() - date.getTime();
int dayStatus = calculateDayStatus(date, new Date());
if (durTime <= 10 * ONE_MINUTE_MILLIONS) {
str = "刚刚";
} else if (durTime < ONE_HOUR_MILLIONS) {
str = durTime / ONE_MINUTE_MILLIONS + "分钟前";
} else if (dayStatus == 0) {
str = durTime / ONE_HOUR_MILLIONS + "小时前";
} else if (dayStatus == -1) {
str = "昨天" + DateFormat.format("HH:mm", date);
} else if (isSameYear(date, curDate) && dayStatus < -1) {
str = DateFormat.format("MM-dd", date).toString();
} else {
str = DateFormat.format("yyyy-MM", date).toString();
}
return str;
}
/**
* 判断是否是同一年
* @param targetTime
* @param compareTime
* @return
*/
public static boolean isSameYear(Date targetTime, Date compareTime) {
Calendar tarCalendar = Calendar.getInstance();
tarCalendar.setTime(targetTime);
int tarYear = tarCalendar.get(Calendar.YEAR);
Calendar compareCalendar = Calendar.getInstance();
compareCalendar.setTime(compareTime);
int comYear = compareCalendar.get(Calendar.YEAR);
return tarYear == comYear;
}
/**
* 判断是否处于今天还是昨天,0表示今天,-1表示昨天,小于-1则是昨天以前
* @param targetTime
* @param compareTime
* @return
*/
public static int calculateDayStatus(Date targetTime, Date compareTime) {
Calendar tarCalendar = Calendar.getInstance();
tarCalendar.setTime(targetTime);
int tarDayOfYear = tarCalendar.get(Calendar.DAY_OF_YEAR);
Calendar compareCalendar = Calendar.getInstance();
compareCalendar.setTime(compareTime);
int comDayOfYear = compareCalendar.get(Calendar.DAY_OF_YEAR);
return tarDayOfYear - comDayOfYear;
}
/**
* 将秒数转换成00:00的字符串,如 118秒 -> 01:58
* @param time
* @return
*/
public static String secToTime(int time) {
String timeStr = null;
int hour = 0;
int minute = 0;
int second = 0;
if (time <= 0)
return "00:00";
else {
minute = time / 60;
if (minute < 60) {
second = time % 60;
timeStr = unitFormat(minute) + ":" + unitFormat(second);
} else {
hour = minute / 60;
if (hour > 99)
return "99:59:59";
minute = minute % 60;
second = time - hour * 3600 - minute * 60;
timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":"
+ unitFormat(second);
}
}
return timeStr;
}
public static String unitFormat(int i) {
String retStr = null;
if (i >= 0 && i < 10)
retStr = "0" + Integer.toString(i);
else
retStr = "" + i;
return retStr;
}
/**
* 获取当前月1号 返回格式yyyy-MM-dd (eg: 2007-06-01)
*
* @return
*/
public static String getMonthBegin() {
String yearMonth = new SimpleDateFormat(
"yyyy-MM").format(new Date());
return yearMonth + "-01";
}
/**
* 与当前时间对比
* @param time
* @return
*/
public static long compareTime(String time) {
long timeLong = 0;
long curTimeLong = 0;
try {
timeLong = sdf.parse(time).getTime();
curTimeLong = sdf.parse(getCurrentTimeString())
.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return curTimeLong - timeLong;// 当前时间减去传入的时间
}
/**
* 返回yyyy-MM-dd HH:mm:ss类型的时间字符串
*/
public static String getCurrentTimeString() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")//
.format(new Date());
}
/**
* @param milliseconds 时间值
* @param isDetail 是否需要显示具体时间段 + 时分和星期 + 时分
* @return
*/
public static String getTimeShowString(long milliseconds,boolean isDetail) {
String dataString = "";
String timeStringBy24 = "";
Date currentTime = new Date(milliseconds);
Date today = new Date();
Calendar todayStart = Calendar.getInstance();
todayStart.set(Calendar.HOUR_OF_DAY, 0);
todayStart.set(Calendar.MINUTE, 0);
todayStart.set(Calendar.SECOND, 0);
todayStart.set(Calendar.MILLISECOND, 0);
Date todaybegin = todayStart.getTime();
Date yesterdaybegin = new Date(todaybegin.getTime() - 3600 * 24 * 1000);
Date preyesterday = new Date(
yesterdaybegin.getTime() - 3600 * 24 * 1000);
if (!currentTime.before(todaybegin)) {
dataString = "今天";
} else if (!currentTime.before(yesterdaybegin)) {
dataString = "昨天";
} else if (!currentTime.before(preyesterday)) {
dataString = "前天";
} else if (isSameWeekDates(currentTime, today)) {
dataString = getWeekOfDate(currentTime);
} else {
SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy-MM-dd",
Locale.getDefault());
dataString = dateformatter.format(currentTime);
}
SimpleDateFormat timeformatter24 = new SimpleDateFormat("HH:mm",
Locale.getDefault());
timeStringBy24 = timeformatter24.format(currentTime);
if (isDetail) {//显示具体的时间
//在聊天界面显示时间,如果是今天则显示当前时间段加上时和分 如上午 9:58
if (!currentTime.before(todaybegin)) {//如果是今天
return getTodayTimeBucket(currentTime);//根据时间段分为凌晨 上午 下午等
} else {
return dataString + " " + timeStringBy24;//如果是昨天 则是 昨天 9:58 如果是同在一个星期,前天之前的时间则显示 星期一 9:58
}
}else{
//在会话记录界面不需要展示很具体的时间
if (!currentTime.before(todaybegin)) {//如果是今天
return timeStringBy24;//直接返回时和分 如 9:58
}else{
return dataString;//如果不是今天,不需要展示时和分 如 昨天 前天 星期一
}
}
}
/**
* 判断两个日期是否在同一周
*
* @param date1
* @param date2
* @return
*/
public static boolean isSameWeekDates(Date date1, Date date2) {
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);
int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
if (0 == subYear) {
if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
.get(Calendar.WEEK_OF_YEAR))
return true;
} else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
// 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
.get(Calendar.WEEK_OF_YEAR))
return true;
} else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
.get(Calendar.WEEK_OF_YEAR))
return true;
}
return false;
}
/**
* 根据日期获得星期
*
* @param date
* @return
*/
public static String getWeekOfDate(Date date) {
String[] weekDaysName = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
"星期六" };
// String[] weekDaysCode = { "0", "1", "2", "3", "4", "5", "6" };
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
return weekDaysName[intWeek];
}
/**
* 根据不同时间段,显示不同时间
*
* @param date
* @return
*/
public static String getTodayTimeBucket(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
SimpleDateFormat timeformatter0to11 = new SimpleDateFormat("KK:mm",
Locale.getDefault());
SimpleDateFormat timeformatter1to12 = new SimpleDateFormat("hh:mm",
Locale.getDefault());
int hour = calendar.get(Calendar.HOUR_OF_DAY);
if (hour >= 0 && hour < 5) {
return "凌晨 " + timeformatter0to11.format(date);
} else if (hour >= 5 && hour < 12) {
return "上午 " + timeformatter0to11.format(date);
} else if (hour >= 12 && hour < 18) {
return "下午 " + timeformatter1to12.format(date);
} else if (hour >= 18 && hour < 24) {
return "晚上 " + timeformatter1to12.format(date);
}
return "";
}
/**
* 获取当前时间 yyyy-MM-dd格式
*
* @return
*/
public static String getCurrentTimeYMD() {
return new SimpleDateFormat("yyyy-MM-dd")//
.format(new Date());
}
/**
* 将yyyy-MM-dd的字符串转换成Date对象
*/
public static Date getDateByYMD(String time) {
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd").parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}