3
3
import java .text .ParseException ;
4
4
import java .text .SimpleDateFormat ;
5
5
import java .util .Date ;
6
+ import java .util .Locale ;
6
7
7
8
import static com .blankj .utilcode .utils .UnitUtils .*;
8
9
@@ -151,19 +152,31 @@ private TimeUtils() {
151
152
* </tr>
152
153
* </table>
153
154
* <pre>
154
- * yyyy-MM-dd 1969-12-31
155
- * yyyy-MM-dd 1970-01-01
156
- * yyyy-MM-dd HH:mm 1969-12-31 16:00
157
- * yyyy-MM-dd HH:mm 1970-01-01 00:00
158
- * yyyy-MM-dd HH:mmZ 1969-12-31 16:00-0800
159
- * yyyy-MM-dd HH:mmZ 1970-01-01 00:00+0000
160
- * yyyy-MM-dd HH:mm:ss.SSSZ 1969-12-31 16:00:00.000-0800
161
- * yyyy-MM-dd HH:mm:ss.SSSZ 1970-01-01 00:00:00.000+0000
162
- * yyyy-MM-dd'T'HH:mm:ss.SSSZ 1969-12-31T16:00:00.000-0800
163
- * yyyy-MM-dd'T'HH:mm:ss.SSSZ 1970-01-01T00:00:00.000+0000
155
+ * HH:mm 15:44
156
+ * h:mm a 3:44 下午
157
+ * HH:mm z 15:44 CST
158
+ * HH:mm Z 15:44 +0800
159
+ * HH:mm zzzz 15:44 中国标准时间
160
+ * HH:mm:ss 15:44:40
161
+ * yyyy-MM-dd 2016-08-12
162
+ * yyyy-MM-dd HH:mm 2016-08-12 15:44
163
+ * yyyy-MM-dd HH:mm:ss 2016-08-12 15:44:40
164
+ * yyyy-MM-dd HH:mm:ss zzzz 2016-08-12 15:44:40 中国标准时间
165
+ * EEEE yyyy-MM-dd HH:mm:ss zzzz 星期五 2016-08-12 15:44:40 中国标准时间
166
+ * yyyy-MM-dd HH:mm:ss.SSSZ 2016-08-12 15:44:40.461+0800
167
+ * yyyy-MM-dd'T'HH:mm:ss.SSSZ 2016-08-12T15:44:40.461+0800
168
+ * yyyy.MM.dd G 'at' HH:mm:ss z 2016.08.12 公元 at 15:44:40 CST
169
+ * K:mm a 3:44 下午
170
+ * EEE, MMM d, ''yy 星期五, 八月 12, '16
171
+ * hh 'o''clock' a, zzzz 03 o'clock 下午, 中国标准时间
172
+ * yyyyy.MMMMM.dd GGG hh:mm aaa 02016.八月.12 公元 03:44 下午
173
+ * EEE, d MMM yyyy HH:mm:ss Z 星期五, 12 八月 2016 15:44:40 +0800
174
+ * yyMMddHHmmssZ 160812154440+0800
175
+ * yyyy-MM-dd'T'HH:mm:ss.SSSZ 2016-08-12T15:44:40.461+0800
176
+ * EEEE 'DATE('yyyy-MM-dd')' 'TIME('HH:mm:ss')' zzzz 星期五 DATE(2016-08-12) TIME(15:44:40) 中国标准时间
164
177
* </pre>
165
178
*/
166
- public static final SimpleDateFormat DEFAULT_SDF = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss" );
179
+ public static final SimpleDateFormat DEFAULT_SDF = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss" , Locale . getDefault () );
167
180
168
181
169
182
/**
@@ -303,7 +316,7 @@ private static long milliseconds2Unit(long milliseconds, int unit) {
303
316
case MIN :
304
317
case HOUR :
305
318
case DAY :
306
- return Math . abs ( milliseconds ) / unit ;
319
+ return milliseconds / unit ;
307
320
}
308
321
return -1 ;
309
322
}
@@ -312,8 +325,8 @@ private static long milliseconds2Unit(long milliseconds, int unit) {
312
325
* 获取两个时间差(单位:unit)
313
326
* <p>time1和time2格式都为yyyy-MM-dd HH:mm:ss</p>
314
327
*
315
- * @param time1 时间字符串1
316
- * @param time2 时间字符串2
328
+ * @param time0 时间字符串1
329
+ * @param time1 时间字符串2
317
330
* @param unit <ul>
318
331
* <li>MSEC:毫秒</li>
319
332
* <li>SEC :秒</li>
@@ -323,16 +336,16 @@ private static long milliseconds2Unit(long milliseconds, int unit) {
323
336
* </ul>
324
337
* @return unit时间戳
325
338
*/
326
- public static long getIntervalTime (String time1 , String time2 , int unit ) {
327
- return getIntervalTime (time1 , time2 , unit , DEFAULT_SDF );
339
+ public static long getIntervalTime (String time0 , String time1 , int unit ) {
340
+ return getIntervalTime (time0 , time1 , unit , DEFAULT_SDF );
328
341
}
329
342
330
343
/**
331
344
* 获取两个时间差(单位:unit)
332
345
* <p>time1和time2格式都为format</p>
333
346
*
334
- * @param time1 时间字符串1
335
- * @param time2 时间字符串2
347
+ * @param time0 时间字符串1
348
+ * @param time1 时间字符串2
336
349
* @param unit <ul>
337
350
* <li>MSEC:毫秒</li>
338
351
* <li>SEC :秒</li>
@@ -343,17 +356,17 @@ public static long getIntervalTime(String time1, String time2, int unit) {
343
356
* @param format 时间格式
344
357
* @return unit时间戳
345
358
*/
346
- public static long getIntervalTime (String time1 , String time2 , int unit , SimpleDateFormat format ) {
347
- return milliseconds2Unit (string2Milliseconds (time1 , format )
348
- - string2Milliseconds (time2 , format ), unit );
359
+ public static long getIntervalTime (String time0 , String time1 , int unit , SimpleDateFormat format ) {
360
+ return Math . abs ( milliseconds2Unit (string2Milliseconds (time0 , format )
361
+ - string2Milliseconds (time1 , format ), unit ) );
349
362
}
350
363
351
364
/**
352
365
* 获取两个时间差(单位:unit)
353
366
* <p>time1和time2都为Date类型</p>
354
367
*
355
- * @param time1 Date类型时间1
356
- * @param time2 Date类型时间2
368
+ * @param time0 Date类型时间1
369
+ * @param time1 Date类型时间2
357
370
* @param unit <ul>
358
371
* <li>MSEC:毫秒</li>
359
372
* <li>SEC :秒</li>
@@ -363,8 +376,9 @@ public static long getIntervalTime(String time1, String time2, int unit, SimpleD
363
376
* </ul>
364
377
* @return unit时间戳
365
378
*/
366
- public static long getIntervalTime (Date time1 , Date time2 , int unit ) {
367
- return milliseconds2Unit (date2Milliseconds (time2 ) - date2Milliseconds (time1 ), unit );
379
+ public static long getIntervalTime (Date time0 , Date time1 , int unit ) {
380
+ return Math .abs (milliseconds2Unit (date2Milliseconds (time1 )
381
+ - date2Milliseconds (time0 ), unit ));
368
382
}
369
383
370
384
/**
@@ -383,7 +397,7 @@ public static long getCurTimeMills() {
383
397
* @return 时间字符串
384
398
*/
385
399
public static String getCurTimeString () {
386
- return milliseconds2String ( getCurTimeMills ());
400
+ return date2String ( new Date ());
387
401
}
388
402
389
403
/**
@@ -394,7 +408,7 @@ public static String getCurTimeString() {
394
408
* @return 时间字符串
395
409
*/
396
410
public static String getCurTimeString (SimpleDateFormat format ) {
397
- return milliseconds2String ( getCurTimeMills (), format );
411
+ return date2String ( new Date (), format );
398
412
}
399
413
400
414
/**
0 commit comments