How to get the current time in YYYY-MM-DD HH:MI:Sec.Millisecond format in Java?

To get the current time in the YYYY-MM-DD HH:MI:Sec.Millisecond format in Java, you can use the SimpleDateFormat class and specify the desired format string:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String currentTime = sdf.format(new Date());

This will create a SimpleDateFormat object with the desired format string, and then use the format method to format the current time (as a Date object) as a string. The resulting string will be in the YYYY-MM-DD HH:MI:Sec.Millisecond format.

Here is a breakdown of the format string:

  • yyyy: The four-digit year
  • MM: The month as a two-digit number (01-12)
  • dd: The day of the month as a two-digit number (01-31)
  • HH: The hour as a two-digit number in 24-hour format (00-23)
  • mm: The minute as a two-digit number (00-59)
  • ss: The second as a two-digit number (00-59)
  • SSS: The milliseconds as a three-digit number (000-999)

I hope this helps! Let me know if you have any other questions.