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.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3