Home >Java >javaTutorial >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!