2011-05-07 61 views
4

我有jodatime API的问题。我不明白为什么这个测试不起作用。我需要在X毫秒内解析我有多少天,几小时,几分钟和几秒钟。但天价值没有解决....任何想法?jodatime天期间类型问题/错误

@Test 
public void testTime() { 
    long secs = 3866 * 24 * 35; 
    long msecs = secs * 1000; 
    //Period period = duration.toPeriod(); 


    PeriodType periodType = PeriodType.forFields(
      new DurationFieldType[]{ 
        DurationFieldType.days(), 
        DurationFieldType.hours(), 
        DurationFieldType.minutes(), 
        DurationFieldType.seconds(), 
      }); 
    Duration duration = Duration.standardSeconds(secs); 
    Period period = duration.toPeriod(periodType, GregorianChronology.getInstance()); 

    System.out.println("days:" + period.getDays()); 
    System.out.println("hours:" + period.getHours()); 
    System.out.println("mins:" + period.getMinutes()); 
    System.out.println("seconds:" + period.getSeconds()); 
    PeriodFormatter periodFormatter = new PeriodFormatterBuilder() 
      .printZeroAlways() 
      .minimumPrintedDigits(2) 
      .appendDays() 
      .appendSeparator(":") 
      .appendHours() 
      .appendSeparator(":") 
      .appendMinutes() 
      .appendSeparator(":") 
      .appendSecondsWithOptionalMillis() 
      .toFormatter(); 
    StringBuffer stringBuffer = new StringBuffer(); 
    periodFormatter.printTo(stringBuffer, period); 
    System.out.println(">>>" + stringBuffer); 
} 

输出是 天:0 小时:902 分钟:4 秒:0 00:902:04:00

+0

这是http://stackoverflow.com/questions/1440557/joda-time-period-to-string – ditkin 2011-05-07 12:50:20

回答

1

您需要使用正常化时期:

Period normalizedPeriod = period.normalizeStandard(); 

Period normalizedPeriod = period.normalizeStandardPeriodType(); 

然后你可以使用normalizedPeriod,看看你正在寻找的结果。 作为一个快速测试我修改JUnit测试案例,并添加一行:

period = period.normalizedStandard(); 

您创建一个从时间周期之后。

1

您使用可以不考虑天是年表精确。请使用IOSChronology.getInstanceUTC()来代替格里高利年谱。