Most idiomatic way to print a time difference in Java?

∥☆過路亽.° 提交于 2019-11-26 21:04:40

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");

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

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.

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.

Thorbjørn Ravn Andersen

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.

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));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!