Sorry, i've tried to code for date Months
and Days
remaining, unfortunately i get the wrong result. Any helps would be appreciated. Thanks!
SimpleDateFormat formatter= new SimpleDateFormat("dd-MM-yyyy");
String sdate = "08-02-2016";
String edate = "02-02-2017";
Date startdate = formatter.parse(sdate);
Date enddate = formatter.parse(eddate );
Calendar startCalendar = new GregorianCalendar();
startCalendar.setTime(startdate);
Calendar endCalendar = new GregorianCalendar();
endCalendar.setTime(enddate);
int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR); //effdate - currdate
int diffMonth = diffYear * 12 + endCalendar.get(Calendar.MONTH) -startCalendar.get(Calendar.MONTH);
int diffDay= endCalendar.get(Calendar.DAY_OF_MONTH) -startCalendar.get(Calendar.DAY_OF_MONTH);
Expected result : 11 months, 25 days
P/s : JodaTime not applicable.
I would strongly advice you to use a robust api for reliable result, but if you insist of doing it manually, try with the following, it seems giving the correct result(at least for your test case):