2017-02-18 87 views
-1
DateTimeZone timeZone = DateTimeZone.forID("America/Montreal"); 

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy/MM/dd").withZone(timeZone); 
DateTime dateTimeStart = formatter.parseDateTime("2012/01/01"); 
DateTime dateTimeStop = formatter.parseDateTime("2017/06/12"); 
Period period = new Period(dateTimeStart, dateTimeStop); 
PeriodFormatter periodFormatter = PeriodFormat.getDefault(); 

String output = periodFormatter.print(period); 
System.out.println(output); 

實際輸出爲5 years, 5 months, 1 week and 4 days
我想輸出(推薦):5 years, 5 months, 11 days天應中顯示,而周

+3

**閱讀文檔。**你做了,你會發現['Period.normalizedStandard(PeriodType type)'](http://joda-time.sourceforge.net/apidocs/org/joda/時間/ Period.html#normalizedStandard(org.joda.time.PeriodType))。 *由於缺乏研究而減少投票。* – Andreas

回答

1

如果您閱讀手冊...

Period period = new Period(dateTimeStart, dateTimeStop, PeriodType.forFields(
     new DurationFieldType[]{ 
       DurationFieldType.years(), 
       DurationFieldType.months(), 
       DurationFieldType.days(), 
       DurationFieldType.hours(), 
       DurationFieldType.minutes(), 
       DurationFieldType.seconds(), 
       DurationFieldType.millis(), 
     })); 
+0

感謝人完美的作品 –

相關問題