2011-05-07 47 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()來代替格里高利年譜。