How to format an elapsed time interval in hh:mm:ss.SSS format in Java?

Viewed 44287

I'm making a stop watch where I'm using Java's SimpleDateFormat to convert the number of milliseconds into a nice "hh:mm:ss:SSS" format. The problem is the hours field always has some random number in it. Here's the code I'm using:

public static String formatTime(long millis) {
    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss.SSS");

    String strDate = sdf.format(millis);
    return strDate;
}

If I take off the hh part then it works fine. Otherwise in the hh part it'll display something random like "07" even if the argument passed in (number of milliseconds) is zero.

I don't know much about the SimpleDateFormat class though. Thanks for any help.

17 Answers

tl;dr

LocalTime                     // Represents a time-of-day, without date and without time zone.
.ofNanoOfDay(                 // Convert a count of nanoseconds into a time-of-day in a generic 24-hour day, ignoring real-world anomalies such as Daylight Saving Time (DST). 
    Duration                  // Class that represents a span-of-time not attached to the timeline.
    .of( milliseconds )       // Parse your count of milliseconds as a span-of-time.
    .toNanos()                // Extract total number of nanoseconds in this span-of-time.
)                             // Returns a `LocalDate` object.
.toString()                   // Generate text in standard ISO 8601 format for a time-of-day (*not* recommended by me). 

java.time.Duration

The proper class to represent a span-of-time unattached to the timeline with a scale of hours-minutes-seconds is Duration. For a scale of years-months-days, use Period.

Duration d = Duration.of( milliseconds ) ;

ISO 8601 format

I suggest you avoid reporting a span-of-time using the time-of-day format of HH:MM:SS. I have seen the inherent ambiguity lead to misinterpretation and confusion in real-world business apps.

There is a standard for reporting such values, defined in ISO 8601: PnYnMnDTnHnMnS. The P marks the beginning while the T separates any year-month-day portion from any hour-minute-second portion.

The java.time classes use the ISO 8601 standard formats by default when parsing/generating strings. This includes the Duration class.

Duration d = Duration.ofMillis( 5_025_678L ) ;
String output = d.toString() ;

See this code run live at IdeOne.com.

PT1H23M45.678S

These ISO 8601 strings can be parsed as well as generated.

Duration d = Duration.parse( "PT1H23M45.678S" ) ;

Time-of-day format

But if you insist on use time-of-day format for your duration, you can put together such a sting by calling the to…Part methods on Duration object.

Duration d = Duration.ofMillis( 5_025_678L ) ;
String output = d.toHoursPart() + ":" + d.toMinutesPart() + ":" + d.toSecondsPart() + "." + TimeUnit.NANOSECONDS.toMillis( d.toNanosPart() ) ;

1:23:45.678

Or we could abuse the LocalTime class to create your string.

Duration d = Duration.ofMillis( 5_025_678L ) ;
long nanoOfDay = d.toNanos() ;
LocalTime localTimeBogus = LocalTime.ofNanoOfDay( nanoOfDay ) ;
String output = localTimeBogus.toString() ;

Again, you can see this code run live at IdeOne.com.

01:23:45.678

org.apache.commons.lang.time.DurationFormatUtils.formatDuration(stopWatch.getTime(), "HH:mm:ss")

What you are trying to format is a duration, not a Date-Time

You have a duration (no. of milliseconds elapsed) which is different from a Date-Time i.e. an instant on timeline. You use SimpleDateFormat to format a java.util.Date which represents an instant on the timeline. The difference can be better understood by the following examples:

  1. This machine has been running since 2021-09-30'T'10:20:30.
  2. This machine has been running for 3 days 4 hours 5 minutes.

The former represents an instant on timeline while the later represents a duration.

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Also, quoted below is a notice from the home page of Joda-Time:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Solution using java.time, the modern Date-Time API: I recommend you use java.time.Duration which is modelled on ISO-8601 standards and was introduced with Java-8 as part of JSR-310 implementation. With Java-9 some more convenience methods were introduced.

Demo:

import java.time.Duration;

public class Main {
    public static void main(String[] args) {
        // A sample input
        long millis = 123456789L;
        Duration duration = Duration.ofMillis(millis);
        // Default format
        System.out.println(duration);

        // Custom format
        // ####################################Java-8####################################
        String formattedElapsedTime = String.format("%2d:%02d:%02d.%d", duration.toHours() % 24,
                duration.toMinutes() % 60, duration.toSeconds() % 60, duration.toMillis() % 1000);
        System.out.println(formattedElapsedTime);
        // ##############################################################################

        // ####################################Java-9####################################
        formattedElapsedTime = String.format("%2d:%02d:%02d.%d", duration.toHoursPart(), duration.toMinutesPart(),
                duration.toSecondsPart(), duration.toMillisPart());
        System.out.println(formattedElapsedTime);
        // ##############################################################################
    }
}

Output:

PT34H17M36.789S
10:17:36.789
10:17:36.789

ONLINE DEMO

Learn more about the modern Date-Time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project. Android 8.0 Oreo already provides support for java.time.

This one actually works, but it seems like I'm tweaking the intent of the method :-).

public static String formatTime(long millis) {
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");

    String strDate = sdf.format(millis - 3600000);
    return strDate;
}

For those of you who really knows how this works you'll probably find some caveats.

This method must work in all not too old versions of Java )

public static String formatNanoSeconds(long duration) {
    StringBuilder sb = new StringBuilder(20);
    long quotient = duration / 1_000_000;
    long remainder = quotient % 1_000;
    sb.insert(0, String.format(".%03d", remainder));
    quotient = quotient / 1_000;
    remainder = quotient % 60;
    sb.insert(0, String.format(":%02d", remainder));
    quotient = quotient / 60;
    remainder = quotient % 60;
    sb.insert(0, String.format(":%02d", remainder));
    quotient = quotient / 60;
    remainder = quotient % 24;
    sb.insert(0, String.format(" %02d", remainder));
    quotient = quotient / 24;
    sb.insert(0, String.format("%d", quotient));
    return sb.toString();
}

StringBuilder.insert(), though fulfills array copy, must still work better than string concatenation.

And here's the test:

@Test
void formatNanoSeconds() {
    long m = 1_000_000;
    assertEquals("0 00:00:00.000", formatNanoSeconds(0));
    assertEquals("0 00:00:00.000", formatNanoSeconds(1));
    assertEquals("0 00:00:00.000", formatNanoSeconds(999_999));
    assertEquals("0 00:00:00.001", formatNanoSeconds(1_000_000));
    assertEquals("0 00:00:00.999", formatNanoSeconds(999 * m));
    assertEquals("0 00:00:01.000", formatNanoSeconds(1000 * m));
    assertEquals("0 00:00:59.000", formatNanoSeconds(59_000 * m));
    assertEquals("0 00:01:00.000", formatNanoSeconds(60_000 * m));
    assertEquals("0 00:01:01.000", formatNanoSeconds(61_000 * m));
    assertEquals("0 00:59:00.000", formatNanoSeconds(59 * 60_000 * m));
    assertEquals("0 01:00:00.000", formatNanoSeconds(60 * 60_000 * m));
    assertEquals("0 01:01:00.000", formatNanoSeconds(61 * 60_000 * m));
    assertEquals("0 23:00:00.000", formatNanoSeconds(23 * 60 * 60_000 * m));
    assertEquals("1 00:00:00.000", formatNanoSeconds(24 * 60 * 60_000 * m));
    assertEquals("1 01:00:00.000", formatNanoSeconds(25 * 60 * 60_000 * m));
    assertEquals("125 17:28:58.819", formatNanoSeconds(
            ((((125L * 24 * 3600) + (17 * 3600) + (28 * 60) + 58) * 1000) + 819) * m + 1));
    assertEquals("90 08:05:04.009", formatNanoSeconds(
            ((((90L * 24 * 3600) + (8 * 3600) + (5 * 60) + 4) * 1000) + 9) * m + 358));
}
Related