Я использовал приведенный ниже код, и он прекрасно работает для меня.Работает и для более низких версий.Попробуйте это
public static DateBean getDateDifferenceInDDMMYYYY(Date from, Date to)
{
Calendar fromDate=Calendar.getInstance();
Calendar toDate=Calendar.getInstance();
fromDate.setTime(from);
toDate.setTime(to);
int increment = 0;
int year,month,day;
System.out.println(fromDate.getActualMaximum(Calendar.DAY_OF_MONTH));
if (fromDate.get(Calendar.DAY_OF_MONTH) > toDate.get(Calendar.DAY_OF_MONTH))
{
increment =fromDate.getActualMaximum(Calendar.DAY_OF_MONTH);
}
System.out.println("increment"+increment);
// DAY CALCULATION
if (increment != 0) {
day = (toDate.get(Calendar.DAY_OF_MONTH) + increment) - fromDate.get(Calendar.DAY_OF_MONTH);
increment = 1;
} else {
day = toDate.get(Calendar.DAY_OF_MONTH) - fromDate.get(Calendar.DAY_OF_MONTH);
}
// MONTH CALCULATION
if ((fromDate.get(Calendar.MONTH) + increment) > toDate.get(Calendar.MONTH)) {
month = (toDate.get(Calendar.MONTH) + 12) - (fromDate.get(Calendar.MONTH) + increment);
increment = 1;
} else {
month = (toDate.get(Calendar.MONTH)) - (fromDate.get(Calendar.MONTH) + increment);
increment = 0;
}
// YEAR CALCULATION
year = toDate.get(Calendar.YEAR) - (fromDate.get(Calendar.YEAR) + increment);
DateBean dateDifference = new DateBean(""+year, ""+month, ""+day);
System.out.println(year+"\tYears\t\t"+month+"\tMonths\t\t"+day+"Days");
return dateDifference;
}
и ваш DateBean, как показано ниже
public class DateBean
{
String year, months, days;
public DateBean(String year, String months, String days)
{
this.year = year;
this.months = months;
this.days = days;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getMonths() {
return months;
}
public void setMonths(String months) {
this.months = months;
}
public String getDays() {
return days;
}
public void setDays(String days) {
this.days = days;
}
}