Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current time in YYYY-MM-DD HH:MI:Sec.Millisecond format in Java?

Tags:

java

date

The code below gives me the current time. But it does not tell anything about milliseconds.

public static String getCurrentTimeStamp() {     SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//dd/MM/yyyy     Date now = new Date();     String strDate = sdfDate.format(now);     return strDate; } 

I have a date in the format YYYY-MM-DD HH:MM:SS (2009-09-22 16:47:08).

But I want to retrieve the current time in the format YYYY-MM-DD HH:MM:SS.MS (2009-09-22 16:47:08.128, where 128 are the milliseconds).

SimpleTextFormat will work fine. Here the lowest unit of time is second, but how do I get millisecond as well?

like image 377
Sunil Kumar Sahoo Avatar asked Sep 22 '09 12:09

Sunil Kumar Sahoo


People also ask

How do you obtain the current second minute and hour in Java?

System. out. printf("%d-%02d-%02d %02d:%02d:%02d. %03d", year, month, day, hour, minute, second, millis);

How do you display milliseconds in time format?

Usually we display time in in 12 hour format hh:mm:aa format (e.g. 12:30 PM) or 24 hour format HH:mm (e.g. 13:30), however sometimes we also want to show the milliseconds in the time. To show the milliseconds in the time we include “SSS” in the pattern which displays the Milliseconds.


1 Answers

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); 
like image 119
JayJay Avatar answered Oct 03 '22 07:10

JayJay