Search code examples
javajodatimeperiodformatter

Joda Period Formatter


System.out.println( PeriodFormat.getDefault().print(Period.hours(1).plusMinutes(30).plusSeconds(60)));

The output from the above Joda PeriodFormatter is "1 hour, 30 minutes and 60 seconds".

I know that this is a fringe case, but is there a way to output this as "1 hour and 31 minutes"?

Thanks!


Solution

  • You could normalize it first with normalizedStandard:

    Period period = Period.hours(1).plusMinutes(30).plusSeconds(60);
    PeriodFormat.getDefault().print(period.normalizedStandard());
    

    Or possibly:

    Period period = Period.hours(1).plusMinutes(30).plusSeconds(60);
    PeriodFormat.getDefault()
                .print(period.normalizedStandard(period.getPeriodType()));