Java.Java8.How to get current time in local time with millisecond precision using Date Time API?

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:

PartDescription
LocalTime.now()Gets current local system time
DateTimeFormatter"HH:mm:ss.SSS" includes milliseconds
SSSMilliseconds (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);

This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.