Home > Java > javaTutorial > How to Get the Current Time with Milliseconds in Java?

How to Get the Current Time with Milliseconds in Java?

Susan Sarandon
Release: 2024-11-13 07:15:03
Original
847 people have browsed it

How to Get the Current Time with Milliseconds in Java?

Retrieving Current Time with Milliseconds in Java

To retrieve the current time in the format YYYY-MM-DD HH:MM:SS.MS, including milliseconds, in Java, you can modify the following code snippet:

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

The original code uses "yyyy-MM-dd HH:mm:ss", which only supports seconds. To include milliseconds, you need to change it to "yyyy-MM-dd HH:mm:ss.SSS". The additional ".SSS" will append milliseconds to the output.

Therefore, the modified code should now function correctly:

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

The above is the detailed content of How to Get the Current Time with Milliseconds 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