2012-11-07 138 views
1

這是從JODA API返回有意義嗎?JODA期間不返回正確的值?

DateTime sd = new DateTime(1973,1,1,0,0,0); 
DateTime ed = new DateTime(1973,1,31,23,59,59); 
Period p = new Period(sd,ed); 

StringBuilder buf = new StringBuilder(); 
buf.append(p.getYears()).append(" years ") 
     .append(p.getMonths()).append(" months ") 
     .append(p.getDays()).append(" days"); 
System.out.println(buf.toString()); 

回報0年0個月2天?

+2

不打印週數是4 –

+0

能JODA不2天轉換4周1個月這個案例? – ed4becky

+0

'sd'和'ed'是在同一個月,所以我不會指望有幾個月。 –

回答

1

嘗試使用PeriodFormatter

System.out.println(PeriodFormat.getDefault().print(p)); 

輸出:

4 weeks, 2 days, 23 hours, 59 minutes and 59 seconds 
+0

在這種情況下,JODA是否可以將4周2天轉換爲1個月? – ed4becky

+0

爲什麼要這樣?它錯過了一個月是一個月。 –

+0

有道理。但是,在顯示之前在結束日期(ed.plusSeconds(1))中添加第二個參數並不會改變結果 – ed4becky

相關問題