2
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!

GWTNewbie
  • 395
  • 4
  • 16

2 Answers2

6

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()));
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

Try adding toStandardDuration():

System.out.println( PeriodFormat.getDefault().print(Period.hours(1).plusMinutes(30).plusSeconds(60).toStandardDuration()));
Thomas
  • 87,414
  • 12
  • 119
  • 157
  • javadoc for toStandardDuration() says "If the period contains years or months, an exception will be thrown.", so I'm accepting Jon Skeet's answer. Thanks! – GWTNewbie Sep 21 '11 at 12:49