时间格式的毫秒和纳秒

这是我的代码:

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

是否可以在日期格式中添加毫秒和纳秒?

您可以通过在末尾添加SSS来添加毫秒,例如格式为HH:mm:ss.SSS

SimpleDateFormat没有引用纳秒。 通常, System.nanoTime()用于性能调试,而不用于显示目的。

 String result = new SimpleDateFormat("HH:mm:ss:SSS").format(date); "HH:mm:ss:SSS" //SSS stands for milliseconds 

查看SimpleDateFormat API以获取更多信息

 String timeStamp = new SimpleDateFormat("dd:MM:yyyy_HH:mm:ss:SSS").format(Calendar.getInstance().getTime()); timeStamp = timeStamp + ":" + System.nanoTime(); 

这给了我输出,

 24:03:2014_18:24:09:890:2018826751601473