加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – PeriodFormatter – 如果小时或分钟恰好是一位数字,如何

发布时间:2020-12-15 05:08:11 所属栏目:Java 来源:网络整理
导读:当我使用PeriodFormatter如下 PeriodFormatter formatter = new PeriodFormatterBuilder().appendHours() .appendLiteral(":").appendMinutes().appendLiteral(":") .appendSeconds().toFormatter(); 我得到输出为4:39:9这意味着4小时39分钟和9秒. 如何修
当我使用PeriodFormatter如下

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

我得到输出为4:39:9这意味着4小时39分钟和9秒.

如何修改格式化程序以生成04:39:09?谢谢

解决方法

添加.printZeroAlways()和.minimumPrintedDigits(2):

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读