How can I “pretty print” a Duration in Java?

2019-01-07 15:25发布

Does anyone know of a Java library that can pretty print a number in milliseconds in the same way that C# does?

E.g., 123456 ms as a long would be printed as 4d1h3m5s.

10条回答
We Are One
2楼-- · 2019-01-07 15:58

Here's how you can do it using pure JDK code:

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.Duration;

long diffTime = 215081000L;
Duration duration = DatatypeFactory.newInstance().newDuration(diffTime);

System.out.printf("%02d:%02d:%02d", duration.getDays() * 24 + duration.getHours(), duration.getMinutes(), duration.getSeconds()); 
查看更多
劫难
3楼-- · 2019-01-07 15:58

Java 9

Duration d1 = Duration.ofDays(0);
        d1 = d1.plusHours(47);
        d1 = d1.plusMinutes(124);
        d1 = d1.plusSeconds(124);
System.out.println(String.format("%s d %sh %sm %ss", 
                d1.toDaysPart(), 
                d1.toHoursPart(), 
                d1.toMinutesPart(), 
                d1.toSecondsPart()));

2 d 1h 6m 4s

查看更多
Summer. ? 凉城
4楼-- · 2019-01-07 16:06

An alternative to the builder-approach of Joda-Time would be a pattern-based solution. This is offered by my library Time4J. Example using the class Duration.Formatter (added some spaces for more readability - removing the spaces will yield the wished C#-style):

IsoUnit unit = ClockUnit.MILLIS;
Duration<IsoUnit> dur = Duration.of(123456, unit).with(Duration.STD_PERIOD);
String s = Duration.Formatter.ofPattern("D'd' h'h' m'm' s.fff's'").format(dur);
System.out.println(s); // output: 0d 0h 2m 3.456s

Another way is using the class net.time4j.PrettyTime (which is also good for localized output and printing relative times):

s = PrettyTime.of(Locale.ENGLISH).print(dur, TextWidth.NARROW);
System.out.println(s); // output: 2m 3s 456ms
查看更多
Animai°情兽
5楼-- · 2019-01-07 16:08

I've built a simple solution, using Java 8's Duration.toString() and a bit of regex:

public static String humanReadableFormat(Duration duration) {
    return duration.toString()
            .substring(2)
            .replaceAll("(\\d[HMS])(?!$)", "$1 ")
            .toLowerCase();
}

The result will look like:

- 5h
- 7h 15m
- 6h 50m 15s
- 2h 5s
- 0.1s

If you don't want spaces between, just remove replaceAll.

查看更多
唯我独甜
6楼-- · 2019-01-07 16:08

I realize this might not fit your use case exactly, but PrettyTime might be useful here.

PrettyTime p = new PrettyTime();
System.out.println(p.format(new Date()));
//prints: “right now”

System.out.println(p.format(new Date(1000*60*10)));
//prints: “10 minutes from now”
查看更多
兄弟一词,经得起流年.
7楼-- · 2019-01-07 16:09

JodaTime has a Period class that can represent such quantities, and can be rendered (via IsoPeriodFormat) in ISO8601 format, e.g. PT4D1H3M5S, e.g.

Period period = new Period(millis);
String formatted = ISOPeriodFormat.standard().print(period);

If that format isn't the one you want, then PeriodFormatterBuilder lets you assemble arbitrary layouts, including your C#-style 4d1h3m5s.

查看更多
登录 后发表回答