Most idiomatic way to print a time difference in Java?
Asked Answered
C

6

16

I'm familiar with printing time difference in milliseconds:

 long time = System.currentTimeMillis();
 //do something that takes some time...
 long completedIn = System.currentTimeMillis() - time;

But, is there a nice way print a complete time in a specified format (eg: HH:MM:SS) either using Apache Commons or even the dreaded platform API's Date/Time objects? In other words, what is the shortest, simplest, no nonsense way to write a time format derived from milliseconds in Java?

Clemente answered 24/4, 2010 at 12:39 Comment(1)
Should be System.currentTimeMillis() - time;.Teodorateodorico
M
23

Apache Commons has the DurationFormatUtils class for applying a specified format to a time duration. So, something like:

long time = System.currentTimeMillis();
//do something that takes some time...
long completedIn = System.currentTimeMillis() - time;

DurationFormatUtils.formatDuration(completedIn, "HH:mm:ss:SS");
Mortar answered 24/4, 2010 at 12:45 Comment(5)
You could even inline that to: DurationFormatUtils.formatDuration(System.currentTimeMillis() - time, "HH:mm:ss:SS");Solifluction
Or use the almost identical, built-in DurationFormatUtils.formatDurationHMS(end-start);Howlet
@JRL: DurationFormatUtils.formatDurationHMS(end-start); will satisfy the example given in the question, but I added the milliseconds in my solution.Mortar
yep! Although formatDurationHMS doesn't quite fit the requirements as it's H:m:s and not HH:mm:ss, but I just wanted to point it out as a possibility.Howlet
This work can now be done without 3rd-party libraries: See Duration & Period classes in the java.time packages to represent a span-of-time unattached to the timeline.Befitting
S
5

A library designed for the purpose is the better approach, but SimpleDateFormat with the right TimeZone may suffice for periods less than a day. Longer periods require treating the day specially.

import java.text.DateFormat;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

public class Elapsed {

    private static final long MS_DAY = 24 * 60 * 60 * 1000;
    private final DateFormat df = new SimpleDateFormat("HH : mm : ss : S");

    public Elapsed() {
        df.setTimeZone(TimeZone.getTimeZone("GMT"));
    }

    private String format(long elapsed) {
        long day = elapsed / MS_DAY;
        StringBuilder sb = new StringBuilder();
        sb.append(day);
        sb.append(" : ");
        sb.append(df.format(new Date(elapsed)));
        return sb.toString();
    }

    public static void main(String[] args) {
        Elapsed e = new Elapsed();
        for (long t = 0; t < 3 * MS_DAY; t += MS_DAY / 2) {
            System.out.println(e.format(t));
        }
    }
}

Console output:

0 : 00 : 00 : 00 : 0
0 : 12 : 00 : 00 : 0
1 : 00 : 00 : 00 : 0
1 : 12 : 00 : 00 : 0
2 : 00 : 00 : 00 : 0
2 : 12 : 00 : 00 : 0
Sowder answered 24/4, 2010 at 18:35 Comment(0)
B
4

tl;dr

Duration.ofMillis( … )
        .toString()

…or…

Duration.between(      // `Duration` represents a span of time unattached to the timeline, in a scale of days-hours-minutes-seconds-nanos.
    instantEarlier ,   // Capture the current moment to start.
    Instant.now()      // Capture the current moment now, in UTC.
)                      // Returns a `Duration` object.
.toString()            // Generate a string in standard ISO 8601 format.

PT1M23.407S

ISO 8601

what is the shortest, simplest, no nonsense way to write a time format derived from milliseconds

The ISO 8601 standard defines textual formats for date-time values. These formats are indeed short, simple, and no-nonsense. They are largely human-readable across various cultures, practical, and unambiguous.

For spans of time unattached to the timeline, the standard format is PnYnMnDTnHnMnS. The P marks the beginning, and the T separates the years-month-days from the hours-minutes-seconds. So an hour and a half is PT1H30M.

java.time.Duration

The java.time classes include a pair of classes to represent spans of time. The Duration class is for hours-minutes-seconds, and Period.

Instant

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Instant earlier = Instant.now() ;  // Capture the current moment in UTC.
… 
Instant later = Instant.now() ;

Duration d = Duration.between( earlier , later ) ;
String output = d.toString() ;  

PT3.52S


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Befitting answered 23/1, 2017 at 6:30 Comment(0)
I
1

If you actually want to see a millisecond difference, I don't think there's a shorter, simpler way.

If you need a more powerful (but certainly not simpler) way to collect performance statistics, there is Perf4J.

Idleman answered 24/4, 2010 at 12:43 Comment(0)
T
1

Not to my knowledge. But printf() can do this easily if you calculate the values for H, M and S, and use a %02 pattern for each.

Thacher answered 24/4, 2010 at 12:44 Comment(0)
I
0
long diff = System.currentTimeMillis() - startTime;
int hours = (int)(diff / 3600000); diff -= hours * 3600000;
int mins = (int)(diff / 60000); diff -= mins * 60000;
int secs = (int)(diff / 1000); diff -= secs * 1000;
System.out.println(String.format("%d:%d:%d.%d", hours, mins, secs, diff));
Impala answered 26/6, 2013 at 14:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.