DateUtils.java
13 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
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
package com.sw.laryngoscope.utils;
/**
* 日志相关类:默认是测试环境<br>
* <b>支持:存储Log日志文件到本地。发送Log日志信息到服务器</b>
*
* @author
* @since 2018-6-18 12:38:39
*/
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
//Android日期工具类整合大全
//https://blog.csdn.net/lliuliu18810979455/article/details/53045443
/**
* 日期工具类
*/
public class DateUtils {
/**
* 获取格林威治时间(1970年至今的秒数)
*/
public static long getGMTime1() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("Etc/Greenwich"));
String format = sdf.format(new Date());
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date gmDate = null;
try {
gmDate = sdf1.parse(format);
} catch (ParseException e) {
e.printStackTrace();
}
return gmDate.getTime() / 1000;
}
/**
* 获取格林威治时间 即1970年至今的秒数
*/
public static long getGMTime2() {
int round = (int) (System.currentTimeMillis() / 1000);
return round;
}
/**
* 获取时间HH:mm:ss
*
* @return
*/
public static String getCurrentTime() {
String time = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(new Date());
//"\\s"以空格截断
String[] split = date.split("\\s");
if (split.length > 1) {
time = split[1];
}
return time;
}
/**
* 获取当前时间的年月日时分秒
* @return
*/
public static String current() {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
return year + "年" + month + "月" + day + "日" + hour + "时" + minute + "分" + second + "秒";
}
/**
* 得到昨天的日期
*
* @return
*/
public static String getYesterdayDate() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String yestoday = sdf.format(calendar.getTime());
return yestoday;
}
/**
* 得到今天的日期
*
* @return
*/
public static String getTodayDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(new Date());
return date;
}
/**
* 得到明天的日期
*
* @return
*/
public static String getTomorrowDate() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String tomorrow = sdf.format(calendar.getTime());
return tomorrow;
}
/**
* 时间转化为时间格式
*
* @param timeStamp
* @return
*/
public static String timeStampToStr(long timeStamp) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(timeStamp * 1000);
return date;
}
/**
* 时间转化为时间格式
*
* @param timeStamp
* @return
*/
public static String timeStampToStr1(long timeStamp) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String date = sdf.format(timeStamp * 1000);
return date;
}
/**
* 时间转化为时间(几点)
*
* @param time
* @return
*/
public static String timeStampToTime(long time) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String date = sdf.format(time * 1000);
return date;
}
/**
* 将日期格式转化为时间(秒数)
*
* @param time
* @return
*/
public static long getStringToDate(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = new Date();
try {
date = sdf.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return date.getTime() / 1000;
}
/**
* 将日期格式转化为时间(秒数)
*
* @param time
* @return
*/
public static long getString2Date(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
try {
date = sdf.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return date.getTime() / 1000;
}
/**
* 判断是否大于当前时间
*
* @param time
* @return
*/
public static boolean judgeCurrTime(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = new Date();
try {
date = sdf.parse(time);
long t = date.getTime();
long round = System.currentTimeMillis();
if (t - round > 0) {
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 判断是否大于当前时间
*
* @param time
* @return
*/
public static boolean judgeCurrTime(long time) {
long round = System.currentTimeMillis();
if (time - round > 0) {
return true;
} else {
return false;
}
}
/**
* 比较后面的时间是否大于前面的时间
*
* @param
* @return
*/
public static boolean judgeTime2Time(String time1, String time2) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
//转化为时间
Date date1 = sdf.parse(time1);
Date date2 = sdf.parse(time2);
//获取秒数作比较
long l1 = date1.getTime() / 1000;
long l2 = date2.getTime() / 1000;
if (l2 - l1 > 0) {
return true;
} else {
return false;
}
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}
/**
* 得到日期 yyyy-MM-dd
*
* @param time
* @return
*/
public static String formatDate(long time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(time * 1000);
return date;
}
/**
* 得到时间 HH:mm:ss
*
* @param timeStamp
* @return
*/
public static String getTime(long timeStamp) {
String time = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(timeStamp * 1000);
String[] split = date.split("\\s");
if (split.length > 1) {
time = split[1];
}
return time;
}
/**
* 得到时间 HH:mm:ss
*
* @param timeStamp
* @return
*/
public static String getTimeOfMMM(long timeStamp) {
//DateTimeFormatter dateFormatter = DateTimeFormat.longDate().withLocale(Locale.ENGLISH);
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy HH:mm:ss", Locale.ENGLISH);
String date = sdf.format(timeStamp);
return date;
}
public static String getTimeString(long timeStamp) {
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);//Z
String date = sdf.format(timeStamp);
return date;
}
public static String getTimeMY(long timeStamp) {
SimpleDateFormat sdf = new SimpleDateFormat("MMMM yyyy", Locale.ENGLISH);
String date = sdf.format(timeStamp);
return date;
}
/**
* 将一个时间转换成提示性时间字符串,(多少分钟)
*
* @param timeStamp
* @return
*/
public static String timeStampToFormat(long timeStamp) {
long curTime = System.currentTimeMillis() / (long) 1000;
long time = curTime - timeStamp;
return time / 60 + "";
}
/**
* 获得当前时间差
*
* @param timeStamp
* @return
*/
public static int nowCurrentTime(long timeStamp) {
long curTime = System.currentTimeMillis() / (long) 1000;
long time = timeStamp - curTime;
return (int) time;
}
/**
* 获取当前的时 -->flag==true
* 获取当前的分 -->flag==false
*
* @return
*/
public static String nowCurrentPoint(boolean flag) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String date = sdf.format(System.currentTimeMillis());
String[] split = date.split(":");
String hour = null;
String minute = null;
if (flag) {
if (split.length > 1) {
hour = split[0];
return hour;
}
} else {
if (split.length > 1) {
minute = split[1];
return minute;
}
}
return null;
}
/**
* 将标准时间格式HH:mm:ss化为当前的时间差值
*
* @param str
* @return
*/
public static String StandardFormatStr(String str) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date d = sdf.parse(str);
long timeStamp = d.getTime();
long curTime = System.currentTimeMillis() / (long) 1000;
long time = curTime - timeStamp / 1000;
return time / 60 + "";
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 将一个时间转换成提示性时间字符串,如刚刚,1秒前
*
* @param timeStamp
* @return
*/
public static String convertTimeToFormat(long timeStamp) {
long curTime = System.currentTimeMillis() / (long) 1000;
long time = curTime - timeStamp;
if (time < 60 && time >= 0) {
return "刚刚";
} else if (time >= 60 && time < 3600) {
return time / 60 + "分钟前";
} else if (time >= 3600 && time < 3600 * 24) {
return time / 3600 + "小时前";
} else if (time >= 3600 * 24 && time < 3600 * 24 * 30) {
return time / 3600 / 24 + "天前";
} else if (time >= 3600 * 24 * 30 && time < 3600 * 24 * 30 * 12) {
return time / 3600 / 24 / 30 + "个月前";
} else if (time >= 3600 * 24 * 30 * 12) {
return time / 3600 / 24 / 30 / 12 + "年前";
} else {
return "刚刚";
}
}
/**
* 日期变量转成对应的星期字符串
*
* @param date
* @return
*/
public static final int WEEKDAYS = 7;
//星期字符数组
public static String[] WEEK = {"周日", "周一", "周二", "周三",
"周四", "周五", "周六"};
public static String DateToWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dayIndex = calendar.get(Calendar.DAY_OF_WEEK);
if (dayIndex < 1 || dayIndex > WEEKDAYS) {
return null;
}
return WEEK[dayIndex - 1];
}
public static long getCurrentWeekStartTime() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
//start of the week
calendar.add(Calendar.DAY_OF_WEEK, -(calendar.get(Calendar.DAY_OF_WEEK) - 1));
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
long startTime = calendar.getTimeInMillis();
return startTime;
}
public static long[] getCurrentWeekTimeFrame() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
//start of the week
calendar.add(Calendar.DAY_OF_WEEK, -(calendar.get(Calendar.DAY_OF_WEEK) - 1));
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
long startTime = calendar.getTimeInMillis();
//end of the week
calendar.add(Calendar.DAY_OF_WEEK, 6);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
long endTime = calendar.getTimeInMillis();
return new long[]{startTime, endTime};
}
}