I have two dates: (YYYY-dd-mm)
1)1960-11-11
2)1986-04-02
I have the following function which intends to calculate the months elapsed since arbitary dates, the above two are my test conditions. This function calculates months without considering the current month. That is a month is only added once its a complete month.
The function is:
public static final int months(Date date1) {// tricky
Calendar calFirst = Calendar.getInstance();
calFirst.setTime(date1);
System.out.println(""+calFirst.getTime());
Calendar calNow = Calendar.getInstance();
calNow.setTime(getCurrentDate());
System.out.println(calNow.getTime());
int m2 = calNow.get(Calendar.YEAR) * 12 + calNow.get((Calendar.MONTH));
System.out.println(calNow.get(Calendar.YEAR) * 12 );
System.out.println(calNow.get(Calendar.MONTH));
//23832
int m1 = calFirst.get(Calendar.YEAR) * 12 + (calFirst.get(Calendar.MONTH));
System.out.println(calFirst.get(Calendar.YEAR) * 12 );
System.out.println(calFirst.get(Calendar.MONTH));
//24168
return Math.abs(m2 - (m1));
}
Result for 1986-04-02:
Wed Apr 02 00:00:00 IST 1986
Wed Jan 08 00:00:00 IST 2014
24168
0
23832
3
333
Which is correct
Result for 1960-11-11:
Fri Nov 11 00:00:00 IST 1960
Wed Jan 08 00:00:00 IST 2014
24168
0
23520
10
638
Which is wrong.
Any hints where I am going wrong?