yantian yue
2023-10-23 0d7d6a88080dc8759ef84ed5ad7875f25642df6c
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
package cn.stylefeng.guns.base.enums;
 
import java.text.SimpleDateFormat;
 
/**
 * Class to generate timestamps with microsecond precision
 * For example: MicroTimestamp.INSTANCE.get() = "2012-10-21 19:13:45.267128"
 */
public enum MicroTimestamp
{
    INSTANCE ;
    private long startDate ;
    private long startNanoseconds ;
    private SimpleDateFormat dateFormat ;
 
    private MicroTimestamp()
    {  this.startDate = System.currentTimeMillis() ;
        this.startNanoseconds = System.nanoTime() ;
        this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") ;
    }
 
    public String get()
    {  long microSeconds = (System.nanoTime() - this.startNanoseconds) / 1000 ;
        long date = this.startDate + (microSeconds/1000) ;
        return this.dateFormat.format(date) + String.format("%03d", microSeconds % 1000) ;
    }
}