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 get date in the format 2009-09-22 16:47:08
(YYYY-MM-DD HH:MI:Sec).
But I want to retrieve the current time in the format 2009-09-22 16:47:08.128
((YYYY-MM-DD HH:MI:Sec.Ms)- where 128 tells the millisecond.
SimpleTextFormat
will work fine. Here the lowest unit of time is second, but how do I get millisecond as well?
To complement the above answers, here is a small working example of a program that prints the current time and date, including milliseconds.
You only have to add the millisecond field in your date format string:
The API doc of SimpleDateFormat describes the format string in detail.
I would use something like this:
Variable
dateTime
could be any date and/or time value, see JavaDoc forFormatter
.try this:-
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
or
The doc in Java 8 names it fraction-of-second , while in Java 6 was named millisecond. This brought me to confusion