-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeUtils.java
More file actions
562 lines (470 loc) · 13.1 KB
/
TimeUtils.java
File metadata and controls
562 lines (470 loc) · 13.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
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
package fragment;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;
/**
* @Author: Finance Group
* @Date: 2018/12/12 20:07
*/
public class TimeUtils {
/**
* UnixTime of Day
*/
public static final long UNIXTIME_STD_DAY = TimeUnit.SECONDS.convert(1, TimeUnit.DAYS) ;
/**
* 取得yyyy-MM-dd HH:mm:ss格式的当前时间
*
* @return
*/
public static String yyyyMMddHHmmssNow() {
return yyyyMMddHHmmss(new Date());
}
/**
* 取得yyyy-MM-dd HH:mm:ss格式的当前时间
*
* @return
*/
public static String yyyyMMddHHmmss() {
return yyyyMMddHHmmss(new Date());
}
/**
* 取得yyyy-MM-dd HH:mm:ss格式的当前时间
*
* @return
*/
public static String yyyyMMddHHmmss(long timestamp) {
return yyyyMMddHHmmss(new Date(timestamp));
}
/**
* 取得yyyy-MM-dd HH:mm:ss格式的时间
*
* @return
*/
public static String yyyyMMddHHmmss(Date date) {
return dateFormat4yyyyMMddHHmmss().format(date);
}
/**
* 将yyyy-MM-dd HH:mm:ss转换为Date
*
* @param formatdate
* @return
*/
public static Date yyyyMMddHHmmss(String formatdate) {
try
{
return dateFormat4yyyyMMddHHmmss().parse(formatdate);
} catch (Exception e){throw new IllegalStateException("parse["+formatdate+"] with ex.", e);}
}
public static Date yyyyMMdd(String formatdate) {
try
{
return dateFormat4yyyyMMdd().parse(formatdate);
} catch (Exception e){throw new IllegalStateException("parse["+formatdate+"] with ex.", e);}
}
public static long getMilliseconds(String unixtime) {
return Long.valueOf(unixtime)*1000 ;
}
public static Date setUnixTime(Date date, String unixtime) {
if(date==null) date = new Date() ;
date.setTime(getMilliseconds(unixtime)) ;
return date ;
}
/**
*
* @param format
* @param date
* @return
*/
public static String format(DateFormat format, Date date) {
try{
return format.format(date) ;
} catch (Exception e){throw new IllegalArgumentException("format["+date+"] with ex.", e);}
}
/**
*
* @param format
* @param source
* @return
*/
public static Date parse(DateFormat format, String source) {
try{
return format.parse(source) ;
} catch (Exception e){throw new IllegalArgumentException("parse["+source+"] with ex.", e);}
}
/**
*
* @return
*/
public static SimpleDateFormat dateFormatyyyyMMddHHmmss() {
return new SimpleDateFormat("yyyyMMddHHmmss") ;
}
/**
*
* @return
*/
public static SimpleDateFormat dateFormat4yyyyMMddHHmmss() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
}
/**
*
* @return
*/
public static SimpleDateFormat dateFormat2yyyyMMddHHmmss() {
return new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒") ;
}
/**
*
* @return yyyy-MM-dd格式
*/
public static SimpleDateFormat dateFormat4yyyyMMdd() {
return new SimpleDateFormat("yyyy-MM-dd") ;
}
/**
*
* @return yyyyMMdd格式
*/
public static SimpleDateFormat yyyyMMddFormatter() {
return new SimpleDateFormat("yyyyMMdd") ;
}
/**
*
* @return yyyy年MM月dd日格式
*/
public static DateFormat yyyyMMddChianiseFormatter() {
return new SimpleDateFormat("yyyy年MM月dd日") ;
}
/**
*
* @param date
* @return
*/
public static String yyyyMMdd(Date date) {
return dateFormat4yyyyMMdd().format(date) ;
}
public static String yyyyMMddHHMMSS(Date date){
return dateFormat2yyyyMMddHHmmss().format(date);
}
public static Date newDateByUnixTimestamp(long unixTimestamp) {
return unixTimestamp==0 ? null : new Date(unixTimestamp*1000) ;
}
public static long getUnixTimestamp(Date date) {
return date == null ? 0 : date.getTime()/1000 ;
}
public static long unixtimestampOfNow() {
return System.currentTimeMillis()/1000 ;
}
public static String stringOfUnixtimestampNow() {
return String.valueOf(System.currentTimeMillis()/1000) ;
}
public static String stringOfUnixtimestamp(Date date) {
return String.valueOf(date.getTime()/1000) ;
}
public static String yyyyMMddChianise(Date date) {
return yyyyMMddChianiseFormatter().format(date) ;
}
public static void main(String[] args) {
Date now = newDateByUnixTimestamp(1385027819) ;
System.out.println(TimeUtils.yyyyMMdd(now));
}
/**
*
* @param date
* @return
*/
public static Date getDateZero(Date date) {
Calendar c = Calendar.getInstance() ;
if(date!=null) c.setTime(date) ;
setCalendarToZero(c) ;
return c.getTime() ;
}
/**
* 获取今天的截至时间
* @return
*/
public static Date getTodayLimit() {
return getDateEndTime(new Date()) ;
}
/**
* 获取某一天的开始时间
* @param date
* @return
*/
public static Date getDateStartTime(Date date) {
if(date==null) return null ;
Calendar c = Calendar.getInstance() ;
c.setTime(date) ;
setCalendarToZero(c) ;
return c.getTime() ;
}
/**
*
* @param c
* @return
*/
public static void setCalendarToZero(Calendar c) {
c.set(Calendar.HOUR_OF_DAY, 0) ;
c.set(Calendar.MINUTE, 0) ;
c.set(Calendar.SECOND, 0) ;
c.set(Calendar.MILLISECOND, 0) ;
}
/**
* 获取某一天的截至时间
* @param date
* @return
*/
public static Date getDateEndTime(Date date) {
if(date==null) return null ;
Calendar c = Calendar.getInstance() ;
c.setTime(date) ;
c.add(Calendar.DAY_OF_MONTH, 1) ;
setCalendarToZero(c) ;
return c.getTime() ;
}
/**
*
* @return
*/
public static Date getTodayZero() {
return getDateZero(null) ;
}
public static Date parse(java.sql.Timestamp timestamp) {
if(timestamp==null) return null ;
return new Date(timestamp.getTime()) ;
}
/**
*
* @param birthday
* @return
*/
public static int getAge(Date birthday) {
long day = TimeUnit.DAYS.convert(System.currentTimeMillis()-birthday.getTime(), TimeUnit.MILLISECONDS) ;
return (int)(day/365) ;
}
/**
* 得到当月第一天开始时间
* @param now
* @return
*/
public static Date getMonthStartTime(Date date){
if(date==null) return null ;
Calendar c = Calendar.getInstance() ;
c.setTime(date) ;
setCalendarToZero(c);
c.set(Calendar.DAY_OF_MONTH, 1);
return c.getTime() ;
}
public static Date getMonthEndTime(Date date){
if(date==null) return null ;
Date newDate = getMonthStartTime(date);
Calendar c = Calendar.getInstance() ;
c.setTime(newDate) ;
setCalendarToZero(c);
c.set(Calendar.MONTH,c.get(Calendar.MONTH)+1);
return c.getTime() ;
}
/**
* 得到本周第一天开始时间
* @param now
* @return
*/
public static Date getWeekStartTime(Date date){
if(date==null) return null ;
Calendar c = Calendar.getInstance() ;
c.setTime(date) ;
setCalendarToZero(c);
c.set(Calendar.DAY_OF_WEEK, 1);
return c.getTime() ;
}
/**
* 得到昨天开始时间
* @param now
* @return
*/
public static Date getYesterdayStartTime(Date date){
if(date==null) return null ;
Calendar c = Calendar.getInstance() ;
c.setTime(date) ;
setCalendarToZero(c);
c.add(Calendar.DAY_OF_MONTH, -1);
return c.getTime() ;
}
/**
* 得到今天之前多少天的开始时间 — +value
* 或今天之后多少天的开始时间 -value
* @param date
* @return
*/
public static Date getDayOffStartTime(Date date,int value){
if(date==null) return null ;
Calendar c = Calendar.getInstance() ;
c.setTime(date) ;
setCalendarToZero(c);
c.add(Calendar.DAY_OF_MONTH,value);
return c.getTime() ;
}
/**
* 得到几天前或几天后的今天时间
* @param date
* @param value
* @return
*/
public static Date getDayOffTime(Date date,int value){
if(date==null) return null ;
Calendar c = Calendar.getInstance() ;
c.setTime(date) ;
c.add(Calendar.DAY_OF_MONTH,value);
setCalendarToZero(c);
return c.getTime() ;
}
public static Date getYearStartTime(){
Calendar c = Calendar.getInstance() ;
setCalendarToZero(c);
c.set(Calendar.DAY_OF_YEAR, 1);
return c.getTime() ;
}
/**
* 获得指定日期的下一天时间
* @param date
* @return
*/
public static Date getNextDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, +1);
return calendar.getTime();
}
/**
* 获得指定日期前一天时间
* @param date
* @return
*/
public static Date getBeforeDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, -1);
return calendar.getTime();
}
/**
* 获得当前时间 value小时后的时间
* @param value
* @return
*/
public static int getDiffSecond(int value){
Calendar c = Calendar.getInstance();
Date date = new Date();
c.setTime(date);
c.add(Calendar.HOUR, value);
date=c.getTime();
int second = (int)(getUnixTimestamp(date) - getUnixTimestamp(new Date()));
return second;
}
/**
* 根据秒获得时间差
* @param second
* @return
*/
@SuppressWarnings("unused")
public static String getTimeDiff(int second) {
int h = 0, d = 0, s = 0;
int temp = second % 3600;
if (second > 3600) {
h = second / 3600;
if (temp != 0) {
if (temp > 60) {
d = temp / 60;
if (temp % 60 != 0) {
s = temp % 60;
}
} else {
s = temp;
}
}
} else {
d = second / 60;
if (second % 60 != 0) {
s = second % 60;
}
}
return h + "小时" + d + "分钟";
}
/**
* 返回两个日期的天数差(例如one=2016-01-06、other=2016-01-01), 只计算天数差, 不算时分秒
* @param one
* @param other
* @return
*/
public static int daysBetween(Date one, Date other) {
Calendar c = Calendar.getInstance() ;
c.setTime(one);
TimeUtils.setCalendarToZero(c);
long first = c.getTimeInMillis() ;
c.setTime(other);
TimeUtils.setCalendarToZero(c);
long second = c.getTimeInMillis() ;
return (int)TimeUnit.DAYS.convert(first - second, TimeUnit.MILLISECONDS) ;
}
//-----时间消耗方法开始------------------------------------------------------//
/**
* 计算耗时
*
* @param beginTimeMillis 开始时间(MILLISECONDS)
* @return 耗时(单位:秒)
*/
public static long cost(long beginTimeMillis) {
long endTimeMillis = System.currentTimeMillis() ;
return cost(beginTimeMillis, endTimeMillis) ;
}
/**
* 计算耗时
*
* @param beginTimeMillis 开始时间(MILLISECONDS)
* @param endTimeMillis 结束时间(MILLISECONDS)
*
* @return 耗时(单位:秒)
*/
public static long cost(long beginTimeMillis, long endTimeMillis) {
return TimeUnit.SECONDS.convert(endTimeMillis - beginTimeMillis, TimeUnit.MILLISECONDS) ;
}
/**
* 格式化消耗的时间(格式:X小时Y分钟Z秒)
*
* @param beginTimeMillis 开始时间(单位:MILLISECONDS)
*
* @return
*/
public static String costTime(long beginTimeMillis) {
long cost = System.currentTimeMillis() - beginTimeMillis ;
return formatCostTime(cost) ;
}
/**
* 格式化消耗的时间(格式:X小时Y分钟Z秒)
*
* @param cost 消耗的时间(MILLISECONDS)
*
* @return
*/
public static String formatCostTime(long cost) {
long cost_second = TimeUnit.SECONDS.convert(cost, TimeUnit.MILLISECONDS) ;
long second = cost_second % 60 ;
long minute = cost_second / 60 ;
long hour = minute / 60 ;
int type = 0 ;
if(hour > 0) {
type = 2 ;
minute = minute % 60 ;
} else if( hour == 0 && minute > 0 ) {
type = 1 ;
}
String desc = "" ;
if(type == 0) {
desc = second + "秒" ;
} else if(type == 1) {
desc = minute + "分钟" + second + "秒" ;
} else if( type == 2 ) {
desc = hour + "小时" + minute + "分钟" + second + "秒" ;
}
return desc ;
}
}