提交 | 用户 | 时间
|
0d7d6a
|
1 |
package cn.stylefeng.guns.base.enums; |
YY |
2 |
|
|
3 |
import java.text.SimpleDateFormat; |
|
4 |
|
|
5 |
/** |
|
6 |
* Class to generate timestamps with microsecond precision |
|
7 |
* For example: MicroTimestamp.INSTANCE.get() = "2012-10-21 19:13:45.267128" |
|
8 |
*/ |
|
9 |
public enum MicroTimestamp |
|
10 |
{ |
|
11 |
INSTANCE ; |
|
12 |
private long startDate ; |
|
13 |
private long startNanoseconds ; |
|
14 |
private SimpleDateFormat dateFormat ; |
|
15 |
|
|
16 |
private MicroTimestamp() |
|
17 |
{ this.startDate = System.currentTimeMillis() ; |
|
18 |
this.startNanoseconds = System.nanoTime() ; |
|
19 |
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") ; |
|
20 |
} |
|
21 |
|
|
22 |
public String get() |
|
23 |
{ long microSeconds = (System.nanoTime() - this.startNanoseconds) / 1000 ; |
|
24 |
long date = this.startDate + (microSeconds/1000) ; |
|
25 |
return this.dateFormat.format(date) + String.format("%03d", microSeconds % 1000) ; |
|
26 |
} |
|
27 |
} |