37

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?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Nikolas
  • 2,322
  • 9
  • 33
  • 55
  • Use a different formatter? – markspace Aug 27 '14 at 20:42
  • 1
    there are no nanoseconds in your example, and even IF there were such a precision shown : within most operating systems time-precisions below 1ms make no sense / are literally impossible, all you get is somewhat random noise, expressed in numbers. You can COUNT PROCESSOR-TICKS, though ... – specializt Aug 27 '14 at 20:47

6 Answers6

72

Edit: I should add that these are nanoseconds not milliseconds.

I feel these answers don't really answer the question using the Java 8 SE Date and Time API as intended. I believe the truncatedTo method is the solution here.

LocalDateTime now = LocalDateTime.now();
System.out.println("Pre-Truncate:  " + now);
DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME;
System.out.println("Post-Truncate: " + now.truncatedTo(ChronoUnit.SECONDS).format(dtf));

Output:

Pre-Truncate:  2015-10-07T16:40:58.349
Post-Truncate: 2015-10-07T16:40:58

Alternatively, if using Time Zones:

LocalDateTime now = LocalDateTime.now();
ZonedDateTime zoned = now.atZone(ZoneId.of("America/Denver"));
System.out.println("Pre-Truncate:  " + zoned);
DateTimeFormatter dtf = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
System.out.println("Post-Truncate: " + zoned.truncatedTo(ChronoUnit.SECONDS).format(dtf));

Output:

Pre-Truncate:  2015-10-07T16:38:53.900-06:00[America/Denver]
Post-Truncate: 2015-10-07T16:38:53-06:00    
demos74dx
  • 836
  • 7
  • 6
  • This confused me because I'm used to calling parse on a formatter object, instead of a static parse method on the type of object I want. Thanks! – Sam Barnum Apr 29 '21 at 18:18
41

cut to minutes:

 localTime.truncatedTo(ChronoUnit.MINUTES);

cut to seconds:

localTime.truncatedTo(ChronoUnit.SECONDS);

Example:

import java.time.LocalTime;
import java.time.temporal.ChronoUnit;

LocalTime.now()
  .truncatedTo(ChronoUnit.SECONDS)
  .format(DateTimeFormatter.ISO_LOCAL_TIME);

Outputs 15:07:25

Tim Nieradzik
  • 112
  • 1
  • 6
Mike_vin
  • 510
  • 4
  • 5
  • Thanks! I added example to show that the formatter will honor the truncation, that's great, and it works just the same with `ZonedDateTime`. – Hugues M. Aug 25 '17 at 09:11
11

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.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
5

Use this in your first line

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
Justin
  • 1,356
  • 2
  • 9
  • 16
2

Try to use patterns defined here: http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

For example:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd HH. mm. ss");
String text = date.toString(formatter);
dpassy
  • 363
  • 1
  • 10
-1

You can so it simply by using regex on the string:

String f = formatter.format(time).replaceAll("\\.[^.]*", "");

This deletes (by replacing with blank) the last dot and everything after.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 3
    I think this is more like a hack – Shervin Asgari Aug 28 '14 at 08:08
  • @Shervin why is it a "hack"? It's a formatting problem, with a formatting solution. In a way it's superior to writing your own format pattern, because you don't need to know anything about the pattern or how to write one for this to work. – Bohemian Aug 28 '14 at 08:18