1

I am new to Android.I have a requirement, I have a field to enter the Date Of Birth of a person.On successful selection I wanna return the total number of months from the DOB to current date.For example, if I entered DOB as 19/10/2012 I wanna return 36(months).I searched for this, but didn't find anything suitable to my requirement.Here is my current code which return sucessful data,

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();
    }


}
droidev
  • 7,352
  • 11
  • 62
  • 94
Noufal M
  • 163
  • 1
  • 4
  • 12

5 Answers5

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);
Arijit Mukherjee
  • 3,817
  • 2
  • 31
  • 51
3

To start with, I'd suggest using LocalDate instead of DateTime for the computations. Ideally, don't use java.util.Date at all, and take your input as LocalDate to start with (e.g. by parsing text straight to that, or wherever your data comes from.) Set the day of month to 1 in both dates, and then take the difference in months:

private static int monthsBetweenDates(LocalDate start, LocalDate end) {
    start = start.withDayOfMonth(1);
    end = end.withDayOfMonth(1);
    return Months.monthsBetween(start, end).getMonths();
}

UPDATE 1

see this link the OP is accepted the same answer because Months.monthsBetween() method is not working proper for him

UPDATE 2

LocalDate userEnteredDate = LocalDate.parse( new SimpleDateFormat("yyyy-MM-dd").format(date));    
LocaleDate currentDate =  LocalDate.parse( new SimpleDateFormat("yyyy-MM-dd").format(new Date()));

int months = monthsBetweenDates(userEnteredDate, currentDate)
Community
  • 1
  • 1
droidev
  • 7,352
  • 11
  • 62
  • 94
1

Using Joda-time library here, I was able to get the desired result. Try the below code it would give the desired the difference in months.

    DateTime date1 = new DateTime().withDate(2012, 10, 19);
    DateTime today = new DateTime().withDate(2015, 10, 19);
    // calculate month difference
    int diffMonths = Months.monthsBetween(date1.withDayOfMonth(1), today.withDayOfMonth(1)).getMonths();
Dory
  • 7,462
  • 7
  • 33
  • 55
0

Using JodaTime, it's really easy:

paulsm4
  • 114,292
  • 17
  • 138
  • 190
0

Use this code to calculate months between two dates

public static int monthsBetweenUsingJoda(Date d1, Date d2) {
    return Months.monthsBetween(new LocalDate(d1.getTime()), new LocalDate(d2.getTime())).getMonths();
}
waleedsarwar86
  • 2,354
  • 1
  • 19
  • 21