javadate-differencejava.util.calendar

Months elapsed function not giving proper result


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.

I want to consider April in case of 1986-04-02, however the current date that is January 2014 (Month January), it should calculate only till December 2013. Tricky.

Any hints where I am going wrong?

Benchmark


Solution

  • I think that 333 and 638 are both good answers...

    Let me explain :

    April included to December 1986 = 9 months + 27 full years + 0 months in 2014 = 333.

    Same thinking applied to the other :

    November included to december 1960 = 2 months + 53 full years + 0 months in 2014 = 638.

    Both numbers are right from the point of view : first month included, last month excluded