How to remove milliseconds from LocalTime in java 8

Using the java.time framework, I want to print time in format hh:mm:ss, but LocalTime.now() gives the time in the format hh:mm:ss,nnn. I tried to use DateTimeFormatter:

DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_TIME;
LocalTime time = LocalTime.now();
String f = formatter.format(time);
System.out.println(f);

The result:

22:53:51.894

How can I remove milliseconds from the time?

Jon Skeet
people
quotationmark

Just create the DateTimeFormatter explicitly:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss", Locale.US);
LocalTime time = LocalTime.now();
String f = formatter.format(time);
System.out.println(f);

(I prefer to explicitly use the US locale, to make it clear that I don't want anything from the default format locale.)

people

See more on this question at Stackoverflow