【问题标题】:Joda Period FormatterJoda 期间格式化程序
【发布时间】:2011-09-21 12:32:08
【问题描述】:
System.out.println( PeriodFormat.getDefault().print(Period.hours(1).plusMinutes(30).plusSeconds(60)));

上述 Joda PeriodFormatter 的输出是“1 小时 30 分 60 秒”。

我知道这是一个边缘案例,但有没有办法将其输出为“1 小时 31 分钟”?

谢谢!

【问题讨论】:

    标签: java jodatime periodformatter


    【解决方案1】:

    你可以先用normalizedStandard对其进行标准化:

    Period period = Period.hours(1).plusMinutes(30).plusSeconds(60);
    PeriodFormat.getDefault().print(period.normalizedStandard());
    

    或者可能:

    Period period = Period.hours(1).plusMinutes(30).plusSeconds(60);
    PeriodFormat.getDefault()
                .print(period.normalizedStandard(period.getPeriodType()));
    

    【讨论】:

      【解决方案2】:

      尝试添加toStandardDuration()

      System.out.println( PeriodFormat.getDefault().print(Period.hours(1).plusMinutes(30).plusSeconds(60).toStandardDuration()));
      

      【讨论】:

      • javadoc for toStandardDuration() 说“如果期间包含年或月,将引发异常。”,所以我接受 Jon Skeet 的回答。谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-23
      • 2015-06-17
      • 1970-01-01
      • 2017-01-11
      • 1970-01-01
      • 2014-07-07
      相关资源
      最近更新 更多