我使用此代码计算输入pastdate和currentdate之间的月数.它使用JodaTime
- LocalDate date1 = new LocalDate(installmentStartDate2);
- LocalDate date2 = new LocalDate(new java.util.Date());
- PeriodType monthDay = PeriodType.yearMonthDayTime();
- Period difference = new Period(date1,date2,monthDay);
- int months = difference.getMonths();
- return months + 1;
现在,当我进入2013年1月1日,我得到10作为答案.但即使我进入2012年1月1日,问题仍然是10.
所以这意味着在计算时它不考虑年份.
我能做些什么来得到正确的答案,即当我进入2012年1月1日时.
JodaTime能做到吗?如是 ?怎么样 ?
如果不 ?还有其他方法吗?
解决方法
你的代码几乎是正确的.
你可以改变 :
- PeriodType monthDay = PeriodType.yearMonthDayTime();
至 :
- PeriodType monthDay = PeriodType.months();
它也会起作用.