Period formatting is performed by the PeriodFormatter class. You can use it by default or create your own PeriodFormatterBuilder . Another code may be required, as you may like to install this constructor correctly, but you can use it, for example, like this:
scala> import org.joda.time._ import org.joda.time._ scala> import org.joda.time.format._ import org.joda.time.format._ scala> val d1 = new DateTime(2010,1,1,10,5,1,0) d1: org.joda.time.DateTime = 2010-01-01T10:05:01.000+01:00 scala> val d2 = new DateTime(2010,1,1,13,7,2,0) d2: org.joda.time.DateTime = 2010-01-01T13:07:02.000+01:00 scala> val p = new Period(d1, d2) p: org.joda.time.Period = PT3H2M1S scala> val hms = new PeriodFormatterBuilder() minimumPrintedDigits(2) printZeroAlways() appendHours() appendSeparator(":") appendMinutes() appendSuffix(":") appendSeconds() toFormatter hms: org.joda.time.format.PeriodFormatter = org.joda.time.format.PeriodFormatter@4d2125 scala> hms print p res0: java.lang.String = 03:02:01
You should also be aware that daily crossings do not count:
scala> val p2 = new Period(new LocalDate(2010,1,1), new LocalDate(2010,1,2)) p2: org.joda.time.Period = P1D scala> hms print p2 res1: java.lang.String = 00:00:00
therefore, if you also need these hands, you also need to add the necessary fields (days, weeks, years) to the formatter.
Arjan blokzijl
source share