44

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?

pdem
  • 3,880
  • 1
  • 24
  • 38
Chad Lowe
  • 721
  • 1
  • 6
  • 12
  • Possible duplicate of [How to format a duration in java? (e.g format H:MM:SS)](https://stackoverflow.com/questions/266825/how-to-format-a-duration-in-java-e-g-format-hmmss) – Martin Schröder Jan 08 '19 at 14:55
  • @MartinSchröder That question is about formatting a duration (in whatever form), and given it predates Java 8, it is not specifically about formatting `java.time.Duration`. In this question the OP is asking whether he missed the existence of a formatter in `java.time` for a `java.time.Duration`. So the questions are related, but not duplicates. – Mark Rotteveel Jan 08 '19 at 16:54
  • for me, `duration.get().toMillis()` worked. I was trying just `duration.toMillis` – FBI Surveillance Van Mar 29 '21 at 03:20

5 Answers5

43

Java 9 and later: Duration::to…Part methods

In Java 9 the Duration class gained new to…Part 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

Remember that “days” here means chunks of 24-hours, ignoring dates on a calendar. If you care about dates, use Period class instead.

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.

Java 8

Oddly enough, no convenient getter methods for these values were included in the first edition release of java.time in Java 8. One of very few oversights in the otherwise excellent design of the java.time framework.

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

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
25

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).

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
  • Thanks, I guess I'll just have to roll my own. This is a hobby project for me, so its very much the journey not the destination.:) Even if it is lacking a couple of features, I figure the JSR-310 will eventualy have better out of the box support in other libraries like jaxb. – Chad Lowe Dec 29 '13 at 17:12
  • Personally I see the integration of jsr 310 with other libraries rather not so well made. For example JAXB-integration will not happen in Java 8. But there are work-arounds available like for JodaTime, so JAXB is not a hard obstacle. – Meno Hochschild Dec 29 '13 at 17:15
  • @MenoHochschild You can also get the number of days represented by a Duration (on top of hours, minutes, seconds and nanos). – assylias Dec 29 '13 at 17:50
  • @assylias That is right. I have left out this minor detail. But fact is that Duration in JSR-310 does not accept all units. And worse, because of internal conversion to only seconds and nanos many of the units which are allowed in duration construction are not really supported in querying (or only in a messy way - see your example). The basic problem is what you put inside in Duration is not what you get out. – Meno Hochschild Dec 29 '13 at 18:37
20

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"));
assylias
  • 321,522
  • 82
  • 660
  • 783
  • How messy the querying of duration is, but this is not your fault. It is possible to use hours and minutes in construction of duration, but impossible to use these units directly in the standard method get(TemporalUnit) :-( I know why I develop my own time library. – Meno Hochschild Dec 29 '13 at 18:02
  • @MenoHochschild The api is well suited for using Duration as an offset of DateTimes but not so much as a standalone object, indeed. You can contribute to 310 if you have ideas (although it is now too late for Java 8)! – assylias Dec 29 '13 at 19:13
  • @assylias How did you import the TemporalUnit `HOURS`? – IgorGanapolsky Jul 20 '15 at 15:29
  • 4
    @IgorGanapolsky `import static java.time.temporal.ChronoUnit.HOURS;` – assylias Jul 20 '15 at 16:40
13

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.

Gregor
  • 1,297
  • 1
  • 19
  • 31
  • 6
    [DurationFormatUtils.formatDurationWords](https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/time/DurationFormatUtils.html#formatDurationWords-long-boolean-boolean-) can be used to exclude empty fields. `DurationFormatUtils.formatDurationWords(interval.toMillis(), true, true)` – wittyameta Sep 27 '18 at 10:16
  • the best solution to use on java 8, thanks a lot!!! – Brendon Iwata Jul 23 '20 at 12:35
1

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());
}
CodeBlind
  • 4,519
  • 1
  • 24
  • 36