Milliseconds and nanoseconds in time format

后端 未结 3 1268
天命终不由人
天命终不由人 2021-01-06 09:55

This is my code:

long currentTime = System.currentTimeMillis();
Date date = new Date(currentTime); // if you really have long
String result = new SimpleDateF         


        
相关标签:
3条回答
  • 2021-01-06 10:32

    You can add milliseconds by adding SSS at the end, such as the format will be HH:mm:ss.SSS.

    There is no reference in the SimpleDateFormat to nanoseconds. Usually System.nanoTime() is used for performance debugging and not for display purposes.

    0 讨论(0)
  • 2021-01-06 10:41
    String result = new SimpleDateFormat("HH:mm:ss:SSS").format(date);
    
    "HH:mm:ss:SSS" //SSS stands for milliseconds
    

    Check SimpleDateFormat API for more info

    0 讨论(0)
  • 2021-01-06 10:43
    String timeStamp = new SimpleDateFormat("dd:MM:yyyy_HH:mm:ss:SSS").format(Calendar.getInstance().getTime());
    timeStamp = timeStamp + ":" + System.nanoTime();
    

    This gave me output,

    24:03:2014_18:24:09:890:2018826751601473
    
    0 讨论(0)
提交回复
热议问题