Let’s now get the current local time (i.e., your system time zone) with millisecond precision using the Java 8 Date Time API.
✅ Best Way: Use LocalTime.now()
+ Format with .SSS
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class CurrentTimeMillisPrecision {
public static void main(String[] args) {
LocalTime time = LocalTime.now();
String formatted = time.format(DateTimeFormatter.ofPattern("HH:mm:ss.SSS"));
System.out.println("Local time with milliseconds: " + formatted);
}
}
🔹 Output:
Local time with milliseconds: 14:52:31.128
🔧 Why It Works:
Part | Description |
---|---|
LocalTime.now() | Gets current local system time |
DateTimeFormatter | "HH:mm:ss.SSS" includes milliseconds |
SSS | Milliseconds (3 digits, padded) |
✅ If You Also Want the Date:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
LocalDateTime now = LocalDateTime.now();
String formatted = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
System.out.println("Local date & time with milliseconds: " + formatted);