首页 > 解决方案 > jodaTime PeriodFormatter 格式不正确

问题描述

我有这个 PeriodFormatter:

PeriodFormatterBuilder()
            .appendDays()
            .appendSuffix(
                " day",
                " days")
            )
            .appendSeparator(", ")
            .printZeroRarelyLast()
            .appendHours()
            .appendSuffix(
                " hours",
                " hours"
            )
            .appendSeparator(" ")
            .appendMinutes()
            .appendSuffix(" minute")
            .toFormatter()
            .let {
                Period(Seconds.seconds(seconds.toInt())).toString(it)
            }

我想给出秒作为输入并返回 x DAYS、x HOURS、x MINUTES.... 执行此操作时返回一个空字符串。如果我将“appendSeconds()”添加到格式化程序创建中,我会得到与我发送的返回值相同的秒数。

我需要转换,不仅仅是数量,而且我对秒数不感兴趣,而是它占多少分钟,多少小时和多少天..任何可以提供帮助的人?

标签: javaandroidkotlinjodatimeperiodformatter

解决方案


您需要使用Period.normalizedStandard()来说服您的周期将您的秒数转换为分钟、小时和天。

            Period(Seconds.seconds(seconds.toInt())).normalizedStandard().toString(it)

(不确定()Kotlin 中是否需要空圆括号。它们在 Java 中,我在其中进行了测试。)


推荐阅读