Home >Java >javaTutorial >How to Extract Current Time with Millisecond Precision in Java?
Extracting Current Time with Millisecond Precision in Java
To obtain the current time in the format YYYY-MM-DD HH:MI:Sec.Millisecond, an extension to the provided code is necessary. The crux of the modification lies in refining the SimpleDateFormat pattern.
The code provided retrieves the current time without millisecond information:
public static String getCurrentTimeStamp() { SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date now = new Date(); String strDate = sdfDate.format(now); return strDate; }
To append millisecond data, a modified date format is applied:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
The additional ".SSS" signifies the placement of milliseconds. The resulting format grants access to timestamp data with millisecond precision.
For instance, a timestamp in the format YYYY-MM-DD HH:MM:SS (2009-09-22 16:47:08) can be transformed to incorporate milliseconds (2009-09-22 16:47:08.128). This enhanced time representation caters to applications requiring granular temporal accuracy.
The above is the detailed content of How to Extract Current Time with Millisecond Precision in Java?. For more information, please follow other related articles on the PHP Chinese website!