Home > Java > javaTutorial > How to Extract Current Time with Millisecond Precision in Java?

How to Extract Current Time with Millisecond Precision in Java?

Susan Sarandon
Release: 2024-11-12 16:52:02
Original
819 people have browsed it

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;
}
Copy after login

To append millisecond data, a modified date format is applied:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template