6

When I use PeriodFormatter as below

PeriodFormatter formatter = new PeriodFormatterBuilder().appendHours()
                .appendLiteral(":").appendMinutes().appendLiteral(":")
                .appendSeconds().toFormatter();

I get the output as 4:39:9 Which means 4 hrs 39 mins and 9 seconds.

How to modify the formatter to produce 04:39:09? Thanks

Ahamed
  • 39,245
  • 13
  • 40
  • 68

2 Answers2

17

Add .printZeroAlways() and .minimumPrintedDigits(2):

    PeriodFormatter formatter = new PeriodFormatterBuilder()
        .printZeroAlways()
        .minimumPrintedDigits(2)
        .appendHours()
        .appendLiteral(":")
        .appendMinutes()
        .appendLiteral(":")
        .appendSeconds()
        .toFormatter();
Juan Mellado
  • 14,973
  • 5
  • 47
  • 54
0

Take a look at minimumPrintedDigits. It should do what you want.

http://joda-time.sourceforge.net/api-release/org/joda/time/format/PeriodFormatterBuilder.html#minimumPrintedDigits(int)

Ajay
  • 763
  • 5
  • 17