forked from variflight/feeyo-redisproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeUtil.java
More file actions
63 lines (47 loc) · 1.44 KB
/
TimeUtil.java
File metadata and controls
63 lines (47 loc) · 1.44 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
package com.feeyo.redis.nio.util;
import java.util.Calendar;
/**
* 弱精度的计时器,考虑性能不使用同步策略。
**/
public class TimeUtil {
private static volatile long CURRENT_TIME = System.currentTimeMillis();
private static volatile int OFFSET = 0;
static {
Calendar cal = Calendar.getInstance();
int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
OFFSET = zoneOffset + dstOffset;
}
public static final long currentUtcTimeMillis() {
return CURRENT_TIME - OFFSET;
}
public static final long currentTimeMillis() {
return CURRENT_TIME;
}
public static final long currentTimeNanos() {
return System.nanoTime();
}
public static final void update() {
CURRENT_TIME = System.currentTimeMillis();
}
public static String formatTimestamp(long mills) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis( mills );
int hour = cal.get( Calendar.HOUR_OF_DAY );
int minute = cal.get( Calendar.MINUTE );
int second = cal.get( Calendar.SECOND );
int millsecond = cal.get( Calendar.MILLISECOND );
StringBuffer sb = new StringBuffer();
sb.append( hour ).append(":");
if ( minute >= 10)
sb.append( minute).append(":");
else
sb.append("0").append( minute).append(":");
if ( second >= 10 )
sb.append( second );
else
sb.append("0").append( second );
sb.append(".").append( millsecond );
return sb.toString();
}
}