如何获取从特定日期开始的总月数?

Nou*_*l M 1 datetime android jodatime

我是 Android 新手。我有一个要求,我有一个字段可以输入一个人的出生日期。成功选择后,我想返回从出生日期到当前日期的总月数。例如,如果我输入出生日期截至2012年10月19日,我想返回36(个月)。我搜索了这个,但没有找到任何适合我的要求的东西。这是我当前返回成功数据的代码,

private void showDate(int year, int month, int day) {

    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(0);
    cal.set(year, month, day);
    Date date = cal.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

    if(System.currentTimeMillis() > date.getTime()) {
        edtDate.setText(sdf.format(date));
        LocalDate date1 = new LocalDate(date);
        LocalDate date2 = new LocalDate(new java.util.Date());
        PeriodType monthDay = PeriodType.yearMonthDayTime();
        Period difference = new Period(date1, date2, monthDay);
        int months = difference.getMonths();
        months=months + 1;
        System.out.println("16102015:Nunber of Months"+months);
    }else{
        Toast.makeText(mActivity,getResources().getString(R.string.date_validationmsg),Toast.LENGTH_LONG).show();
    }


}
Run Code Online (Sandbox Code Playgroud)

Ari*_*jee 5

Calendar startCalendar = new GregorianCalendar();
startCalendar.setTime(startDate);
Calendar endCalendar = new GregorianCalendar();
endCalendar.setTime(endDate);

int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
int diffMonth = diffYear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
Run Code Online (Sandbox Code Playgroud)