6

This is my code:

long currentTime = System.currentTimeMillis();
Date date = new Date(currentTime); // if you really have long
String result = new SimpleDateFormat("HH:mm:ss").format(date.getTime());

Is it possible to add milliseconds and nanoseconds to the date format ?

David Basarab
  • 72,212
  • 42
  • 129
  • 156
user1735757
  • 185
  • 1
  • 2
  • 8

3 Answers3

14

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.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
6
String result = new SimpleDateFormat("HH:mm:ss:SSS").format(date);

"HH:mm:ss:SSS" //SSS stands for milliseconds

Check SimpleDateFormat API for more info

PermGenError
  • 45,977
  • 8
  • 87
  • 106
0
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