【问题标题】:Formatted string from JodaTime duration来自 JodaTime 持续时间的格式化字符串
【发布时间】:2022-05-25 04:17:22
【问题描述】:

我正在尝试从 JodaTime 的持续时间类中获取格式化字符串。

Duration duration = new Duration(durationInSecond * 1000);
PeriodFormatter formatter = new PeriodFormatterBuilder()
                .appendDays()
                .appendSuffix(" days, ")
                .appendHours()
                .appendSuffix(" hours, ")
                .appendMinutes()
                .appendSuffix(" minutes and ")
                .appendSeconds()
                .appendSuffix(" seconds")
                .toFormatter();
String formattedString = formatter.print(duration.toPeriod());

formattedString 的值应该是

65 天 3 小时 5 分 20 秒

但它是

1563 小时 5 分 20 秒

1563 小时是 65 天零 3 小时,但格式化程序不会以这种方式打印。

我在这里缺少什么?

【问题讨论】:

    标签: java jodatime


    【解决方案1】:

    您可以使用PeriodTypePeriod.normalizedStandard(org.joda.time.PeriodType) 来指定您感兴趣的字段。

    在你的情况下PeriodType.dayTime()似乎合适。

    Duration duration = new Duration(durationInSecond * 1000);
    PeriodFormatter formatter = new PeriodFormatterBuilder()
            .appendDays()
            .appendSuffix(" days, ")
            .appendHours()
            .appendSuffix(" hours, ")
            .appendMinutes()
            .appendSuffix(" minutes, ")
            .appendSeconds()
            .appendSuffix(" seconds")
            .toFormatter();
    
    Period period = duration.toPeriod();
    Period dayTimePeriod = period.normalizedStandard(PeriodType.dayTime());
    String formattedString = formatter.print(dayTimePeriod);
    
    System.out.println(formattedString);
    

    【讨论】:

      【解决方案2】:

      我发现使用 PeriodFormat.getDefault() 有助于创建 PeriodFormatter,而无需使用 PeriodFormatterBuilder 进行所有额外的工作并创建自己的。它给出了相同的结果。

      【讨论】:

        【解决方案3】:

        PeriodFormat.wordBasedPeriodFormat.getDefault() 相同,但接受 Locale 参数。

        import java.util.Locale
        import org.joda.time.Duration
        import org.joda.time.PeriodType
        import org.joda.time.format.PeriodFormat
        
        
        PeriodFormat.wordBased(
            Locale("en"))
            .print(
                Duration(1653419081151L)
                    .toPeriod()
                    .normalizedStandard(
        //                PeriodType.standard() // 2733 weeks, 5 days, 19 hours, 4 minutes, 41 seconds and 151 milliseconds
        //                PeriodType.dayTime() // 19136 days, 19 hours, 4 minutes, 41 seconds and 151 milliseconds
        //                PeriodType.weeks() // 2733 weeks
                        PeriodType.days() // 19136 days
                    )
            )
        

        将以首选语言为您提供适当的输出。

        或者使用自己的PeriodType:

        normalizedStandard(
                    PeriodType.forFields(
                        arrayOf(
                            DurationFieldType.years(),
                            DurationFieldType.months(),
                            DurationFieldType.days(),
                            DurationFieldType.hours(),
                            DurationFieldType.minutes()
                        )
                    )
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-23
          • 2018-05-22
          • 1970-01-01
          • 2011-01-28
          • 1970-01-01
          相关资源
          最近更新 更多