2016-08-03 44 views
2

我想兩個日期之間獲得剩餘的年,月,和天天: 所以我已經使用約達時間這樣做:剩餘的年,月,使用約達時間(安卓)

DateTime endDate = new DateTime(2018,12,25,0,0); 
DateTime startDate = new DateTime(); 
Period period = new Period(startDate,endDate,PeriodType.yearMonthDay()); 

PeriodFormatter formatter = new PeriodFormatterBuilder().appendYears().appendSuffix(" Year "). 
      appendMonths().appendSuffix(" Month ").appendDays().appendSuffix(" Day ").appendHours()..toFormatter(); 

String time = formatter.print(period); 

這給我的字符串時間:2年4個月22日

但是,我希望剩餘年數,月份,天數的每個數字的整數值。 所以,取而代之的「2年4月22日」,我想設置我的變量:

int year = 2 
int month = 4 
int day = 22 

有什麼辦法來獲取這些值分別,而不是獲得一個字符串?非常感謝! :)

+1

閱讀[文檔](http://joda-time.sourceforge.net/apidocs/org/joda/time/Period.html)!有getter方法。 – Andreas

回答

1

我有同樣的要求一次,這裏是代碼片段

LocalDate d=LocalDate.of(yy,mm,dd); 
    LocalDate d2=LocalDate.of(yy, mm, dd); 
    Period p=Period.between(d, d2); 
    long day,month,year; 
    day=p.getDays(); 
    month=p.getMonths(); 
    year=p.getYears(); 
    System.out.println(day+" : "+month+" : "+year); 
0

調用DateTime類提供的方法,只是減去它們。多年下面是一個例子:

int year = (int) dateTime#year#getField() - (int) dateTime2#year#getField()

未經測試的代碼!我會在以後尋找到它,但總的想法是一樣的,獲得的字段信息然後就減去它得到的值