DateUtil.java
12 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package com.drp.mobliemall.utils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
/**
* Created by xiniu_wutao on 15/6/30.
* 时间操作 工具类
*/
public class DateUtil {
/**
* 新版时间展示 聊天页面
*
* @param mTimeStamp
* @return 【备注】注意时间单位是毫秒
*/
public static String getSessionTime(int mTimeStamp) {
if (mTimeStamp <= 0) {
return null;
}
String[] weekDays = {
"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"
};
String strDesc = null;
SimpleDateFormat formatYear = new SimpleDateFormat("yy/MM/dd");
SimpleDateFormat formatToday = new SimpleDateFormat("HH:mm");
/**消息时间戳*/
long changeTime = (long) mTimeStamp;
long messageTimeStamp = changeTime * 1000;
/**当前的时间戳*/
long currentTimeStamp = System.currentTimeMillis();
/**获取今天的 0 点时间戳*/
long todayTimeStamp = getTimesmorning();
/**获取 上一周 0点时间戳*/
long rangeWeekStamp = todayTimeStamp - 86400000 * 6;
/**今天的显示 hh:mm (今天星期三)
* 昨天
* 星期一
* 星期日 、 星期六、 星期五、星期四
* yy-hh-mm
* */
do {
long diff = currentTimeStamp - messageTimeStamp;
long diffToday = currentTimeStamp - todayTimeStamp;
/**今天之内的*/
if (diff < diffToday) {
strDesc = formatToday.format(messageTimeStamp);
break;
}
long diffWeek = currentTimeStamp - rangeWeekStamp;
/**最近一周的判断*/
if (diff < diffWeek) {
/**昨天零点的时间*/
long yesterday = todayTimeStamp - 86400000;
long diffYesterday = currentTimeStamp - yesterday;
if (diff < diffYesterday) {
strDesc = "昨天";
} else {
Calendar weekCal = Calendar.getInstance();
weekCal.setTimeInMillis(messageTimeStamp);
int w = weekCal.get(Calendar.DAY_OF_WEEK) - 1;
w = w < 0 ? 0 : w;
strDesc = weekDays[w];
}
break;
}
/**年月日显示*/
strDesc = formatYear.format(messageTimeStamp);
} while (false);
return strDesc;
}
/**
* 获取当天 零点的时间戳【linux】
*
* @return
*/
public static long getTimesmorning() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTimeInMillis();
}
public static boolean needDisplayTime(int predateTime, int curdateTime) {
long timediff = (curdateTime - predateTime);
return (timediff >= 5 * 60);
}
public static String getTimeDiffDesc(Date date) {
if (date == null) {
return null;
}
String strDesc = null;
Calendar curCalendar = Calendar.getInstance();
Date curDate = new Date();
curCalendar.setTime(curDate);
Calendar thenCalendar = Calendar.getInstance();
thenCalendar.setTime(date);
String[] weekDays = {
"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"
};
int w = thenCalendar.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
// SimpleDateFormat format = new
// SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar current = Calendar.getInstance();
Calendar today = Calendar.getInstance(); // 今天
today.set(Calendar.YEAR, current.get(Calendar.YEAR));
today.set(Calendar.MONTH, current.get(Calendar.MONTH));
today.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH));
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
// Date datetoday = today.getTime();
// System.out.println(format.format(datetoday));
Calendar yesterday = Calendar.getInstance(); // 昨天
yesterday.setTime(curDate);
yesterday.add(Calendar.DATE, -1);
yesterday.set(Calendar.HOUR_OF_DAY, 0);
yesterday.set(Calendar.MINUTE, 0);
yesterday.set(Calendar.SECOND, 0);
// Date dateyestoday = yesterday.getTime();
// System.out.println(format.format(dateyestoday));
Calendar sevendaysago = Calendar.getInstance(); // 7天
sevendaysago.setTime(curDate);
sevendaysago.add(Calendar.DATE, -7);
sevendaysago.set(Calendar.HOUR_OF_DAY, 0);
sevendaysago.set(Calendar.MINUTE, 0);
sevendaysago.set(Calendar.SECOND, 0);
// Date datesevenago = sevendaysago.getTime();
// System.out.println(format.format(datesevenago));
/*
* Date tasktime = yesterday.getTime(); SimpleDateFormat df=new
* SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
* System.out.println(df.format(tasktime));
*/
int thenMonth = thenCalendar.get(Calendar.MONTH);
int thenDay = thenCalendar.get(Calendar.DAY_OF_MONTH);
int h = thenCalendar.get(Calendar.HOUR_OF_DAY);
int m = thenCalendar.get(Calendar.MINUTE);
String sh = "", sm = "";
if (h < 10)
sh = "0";
if (m < 10)
sm = "0";
if (thenCalendar.after(today))// today
{
if (h < 6) {
strDesc = "凌晨 " + sh + h + ":" + sm + m;
} else if (h < 12) {
strDesc = "上午 " + sh + h + ":" + sm + m;
} else if (h < 13) {
strDesc = "下午 " + h + ":" + sm + m;
} else if (h < 19) {
strDesc = "下午 " + (h - 12) + ":" + sm + m;
} else {
strDesc = "晚上 " + (h - 12) + ":" + sm + m;
}
} else if (thenCalendar.before(today) && thenCalendar.after(yesterday)) {// yestoday
// System.out.println("yestoday");
if (h < 6) {
strDesc = "昨天凌晨 " + sh + h + ":" + sm + m;
} else if (h < 12) {
strDesc = "昨天上午 " + sh + h + ":" + sm + m;
} else if (h < 13) {
strDesc = "昨天下午 " + h + ":" + sm + m;
} else if (h < 19) {
strDesc = "昨天下午 " + (h - 12) + ":" + sm + m;
} else {
strDesc = "昨天晚上 " + (h - 12) + ":" + sm + m;
}
} else if (thenCalendar.before(yesterday)
&& thenCalendar.after(sevendaysago)) {// 2 ~ 7days ago
// System.out.println("2~7");
if (h < 6) {
strDesc = weekDays[w] + "凌晨 " + sh + h + ":" + sm + m;
} else if (h < 12) {
strDesc = weekDays[w] + "上午 " + sh + h + ":" + sm + m;
} else if (h < 13) {
strDesc = weekDays[w] + "下午 " + h + ":" + sm + m;
} else if (h < 19) {
strDesc = weekDays[w] + "下午 " + (h - 12) + ":" + sm + m;
} else {
strDesc = weekDays[w] + "晚上 " + (h - 12) + ":" + sm + m;
}
} else {
// System.out.println("7~");
if (h < 6) {
strDesc = (thenMonth + 1) + "月" + thenDay + "日" + "凌晨 " + sh
+ h + ":" + sm + m;
} else if (h < 12) {
strDesc = (thenMonth + 1) + "月" + thenDay + "日" + "上午 " + sh
+ h + ":" + sm + m;
} else if (h < 13) {
strDesc = (thenMonth + 1) + "月" + thenDay + "日" + "下午 " + h
+ ":" + sm + m;
} else if (h < 19) {
strDesc = (thenMonth + 1) + "月" + thenDay + "日" + "下午 "
+ (h - 12) + ":" + sm + m;
} else {
strDesc = (thenMonth + 1) + "月" + thenDay + "日" + "晚上 "
+ (h - 12) + ":" + sm + m;
}
}
// System.out.println(strDesc);
return strDesc;
}
/**
* 年月日时分秒(无下划线) yyyyMMddHHmmss
*/
public static final String dtLong = "HHmmss";
private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};
/**
* 获取的时间格式为 yyyy-MM-dd HH:mm
*
* @return
*/
public static String getCurrentTimeForDateId() {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
return format.format(cal.getTime());
}
public static String getCurrentTime() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
return format.format(cal.getTime());
}
public static String getTime(long time) {
SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm");
return format.format(new Date(time));
}
public static String getHourAndMin(long time) {
SimpleDateFormat format = new SimpleDateFormat("HH:mm");
return format.format(new Date(time));
}
public static String getChatTime(long timesamp) {
String result = "";
SimpleDateFormat sdf = new SimpleDateFormat("dd");
Date today = new Date(System.currentTimeMillis());
Date otherDay = new Date(timesamp);
int temp = Integer.parseInt(sdf.format(today))
- Integer.parseInt(sdf.format(otherDay));
switch (temp) {
case 0:
result = "今天 " + getHourAndMin(timesamp);
break;
case 1:
result = "昨天 " + getHourAndMin(timesamp);
break;
case 2:
result = "前天 " + getHourAndMin(timesamp);
break;
default:
// result = temp + "天前 ";
result = getTime(timesamp);
break;
}
return result;
}
/**
* 将字符串转位日期类型
*
* @param sdate
* @return
*/
public static Date toDate(String sdate) {
try {
return dateFormater2.get().parse(sdate);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 将date 转为string
* @param date
* @return
*/
public static String dateToString(Date date){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.format(date);
}
/**
* 将date 转为string
* @param date
* @return
*/
public static String dateTimeToString(Date date){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
return df.format(date);
}
/**
* 返回系统当前时间(精确到毫秒),作为一个唯一的订单编号
*
* @return 以yyyyMMddHHmmss为格式的当前系统时间
*/
public static String getOrderNumTime() {
Date date = new Date();
DateFormat df = new SimpleDateFormat(dtLong);
return df.format(date);
}
/**
* 产生随机的三位数
*
* @return
*/
public static String getThreeRandoms() {
Random rad = new Random();
return rad.nextInt(1000) + "";
}
/**
* 产生订单编号
*
* @return
*/
public static String getOrderNo() {
return getOrderNumTime() + getThreeRandoms();
}
}