Java – periodformatter – how to preset 0 if the hour or minute is exactly one digit?

When I use periodformatter

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

I get an output of 4:39:9, which means 4 hours, 39 minutes and 9 seconds

How do I modify the formatter to generate 04:39:09? thank you

Solution

add to. Printzeroalways() and minimumPrintedDigits(2):

PeriodFormatter formatter = new PeriodFormatterBuilder()
        .printZeroAlways()
        .minimumPrintedDigits(2)
        .appendHours()
        .appendLiteral(":")
        .appendMinutes()
        .appendLiteral(":")
        .appendSeconds()
        .toFormatter();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>