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

How to Get the Current Time with Milliseconds in Java?

Susan Sarandon
Susan SarandonOriginal
2024-11-13 07:15:03817browse

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;
}

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");

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!

Statement:
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