// when dialog box is closed, below method will be called. @Override public void onDateSet( DatePicker view, int selectedYear, int selectedMonth, int selectedDay) { year = selectedYear; month = selectedMonth; day = selectedDay; // Show selected date expDate.setText( new StringBuilder() .append(day) .append("-") .append(month + 1) .append("-") .append(year)); StringBuilder sDate; StringBuilder eDate; eDate = new StringBuilder() .append(year) .append("-") .append(month + 1) .append("-") .append(day); if (edDate.getText().toString().trim().length() == 0) { edDate.setText( new StringBuilder() .append(day) .append("-") .append(month + 1) .append("-") .append(year)); sDate = new StringBuilder() .append(year) .append("-") .append(month + 1) .append("-") .append(day); } else { String tempDate = edDate.getText().toString(); int tempdays = Integer.valueOf(tempDate.substring(0, edDate.getText().toString().indexOf("-"))); int tempmonth = Integer.valueOf( tempDate.substring( edDate.getText().toString().indexOf("-") + 1, edDate.getText().toString().lastIndexOf("-"))); int tempyear = Integer.valueOf( tempDate.substring( edDate.getText().toString().lastIndexOf("-") + 1, edDate.length())); sDate = new StringBuilder() .append(tempyear) .append("-") .append(tempmonth) .append("-") .append(tempdays); } LocalDate startDate = new LocalDate(sDate.toString().trim()); LocalDate endDate = new LocalDate(eDate.toString().trim()); Period period = new Period(startDate, endDate, PeriodType.months()); Log.e("############## dif", "" + period.getMonths()); if (period.getMonths() <= 0) { edTerm.setText("0"); } else { edTerm.setText("" + period.getMonths()); } }