Formatting a Duration in Java 8 / jsr310

2019-01-07 23:19发布

问题:

I am transitioning a project from Joda-Time to java8's native time libraries, and I have run into a snag. I have been unable to find a formatter for Duration. I would like to have a custom String format of, for instance, HHH+MM, where a Duration of 75 hours and 15 minutes would format as "75+15". This was easy to do with Joda-Time by converting to period, and using a PeriodFormatter, but I have been unable to find this type of class in Java8. Am I missing something?

回答1:

There is no period/duration-formatter in jsr-310, different from JodaTime. Not every feature of JodaTime was ported to JSR-310 (for example also not PeriodType). And in reverse JSR-310 has some features which are not available in JodaTime (for example localized weekday numbers or the strategy pattern approach with adjusters).

It might happen that Java 9 will introduce some kind of built-in period formatting (read something about this from S. Colebourne).

Conclusion: JSR-310 and JodaTime are not fully compatible to each other, so a lot of work can be required. I would not be so keen on migration as soon as possible. Do you need special features of JSR-310 which are not offered by JodaTime?

Additional note: You should also be aware of the fact that joda period (which includes all units from years to seconds) is not fully compatible with jsr310-period (only years, months, days) or jsr310-duration (only hours, minutes, seconds and fraction seconds).



回答2:

Java 8

Oddly enough, no convenient getter methods for these values were included in the first edition release of java.time in Java 8.

See the related Question: Why can't I get a duration in minutes or hours in java.time?.

Java 9 and later

In Java 9 the Duration class gets new methods for returning the various parts of days, hours, minutes, seconds, milliseconds/nanoseconds. See this pre-release OpenJDK source code.

Given a duration of 49H30M20.123S…

  • toNanosPart() = 123000000
  • toMillisPart() = 123
  • toSecondsPart() = 20
  • toMinutesPart() = 30
  • toHoursPart() = 1
  • toDaysPart() = 2

I do not know if any additional formatter features are added. But at least you will be able to more conveniently generate your own strings from numbers obtained via these new getter methods.



回答3:

There is no built-in method but you can access the number of hours/minutes without having to calculate them manually. Your specific format could look like:

Duration d = Duration.of(75, HOURS).plusMinutes(15);
long hours = d.toHours(); //75
long minutes = d.minusHours(hours).toMinutes(); //15
String HH_PLUS_MM = hours + "+" + minutes; //75+15
System.out.println(HH_PLUS_MM);

If the duration is guaranteed to be less than 24 hours, you can also use this trick:

String hhPlusMm = LocalTime.MIDNIGHT.plus(d).format(DateTimeFormatter.ofPattern("HH+mm"));


回答4:

you can use the DurationFormatUtils from commons-lang3-time (for minutes you have to use "mm" as the format is the same as in SimpleDateFormat): DurationFormatUtils.formatDuration(interval.toMillis(), "HHH+mm")

Sadly I found no way to exclude empty parts, like in my case days or hours could be 0, so I still had to roll my own.

Update: I have opened an issue for this on Apache commons.lang.time.DurationFormatUtils JIRA.



回答5:

I know this is an old question but I recently ran into the same thing. There really should be a better solution than this, but this worked for me:

public static String millisToElapsedTime(long millis){
    DateFormat fmt = new SimpleDateFormat(":mm:ss.SSS");
    fmt.setTimeZone(TimeZone.getTimeZone("UTC"));
    return (millis/3600000/*hours*/)+fmt.format(new Date(millis));
}

Then, you could add this:

public static String durationToElapsedTime(Duration d){
    return millisToElapsedTime(d.toMillis());
}