0

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?

Benchmark

Skynet
  • 7,820
  • 5
  • 44
  • 80
  • I guess it is a boundary bug, you just have to check the boundary case. I mean the given months are inclusive or exclusive in the calculations... – Ahmed Hamdy Jan 08 '14 at 10:13
  • 1
    I would use [Joda Time](http://www.joda.org/joda-time/) for this sort of calculation. – mikea Jan 08 '14 at 10:13
  • My project is at a stage where I cannot revert back, it means a lot of rework, also I am working in a platform where use of Joda is not suggested(mobile). – Skynet Jan 08 '14 at 10:18
  • 1
    Between 1986 and 2014, i count 27 full years, hence 324 months, to what I add 8 month from May to december 1986, that is to say 332 months when you told us 333 were the correct answer [first and last months excluded as you said : "a month is only added once its a complete month."]. How do you make your counting ? – Grooveek Jan 08 '14 at 10:19
  • Not the first but the last. – Skynet Jan 08 '14 at 10:20
  • OK, so 638 is the good answer... (53*12 + 2) – Grooveek Jan 08 '14 at 10:22
  • Wah I am confused let me calculate! – Skynet Jan 08 '14 at 10:22
  • 1
    Dates are always confusing :-) – Grooveek Jan 08 '14 at 10:23
  • Hey the number of years between 1986 to 2014 is 28 though – Skynet Jan 08 '14 at 10:41
  • 27 for both start and end excluded (non-full years) – Grooveek Jan 08 '14 at 10:53
  • April of 1986 to April of 2014 is 28 years, that means 28*12 = 336 minus the three months which are yet to come. That is 333. – Skynet Jan 08 '14 at 10:56
  • 4 months if you exclude the current one ;-) – Grooveek Jan 08 '14 at 11:23
  • Ok, I think I begin to understand the way you see those things. You'll have to decide which number of months you'll want to have. in fact, your code above doesn't reflect the calculation you've just done, which involves a date-to-date month comparison. (and I was wrong for the 4 months above-i'm confused, too, now :-D) – Grooveek Jan 08 '14 at 11:29
  • Yes 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! – Skynet Jan 08 '14 at 11:34

2 Answers2

1

Why not use Joda Period instead as discussed here:

Period p = new Period(from, to, PeriodType.months().withDaysRemoved());
int months = p.getMonths() + 1;
Community
  • 1
  • 1
Oleg Sklyar
  • 9,834
  • 6
  • 39
  • 62
1

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

Grooveek
  • 10,046
  • 1
  • 27
  • 37
  • Did you check the link at the bottom I provided "Benchmark" its a bit misleading. – Skynet Jan 08 '14 at 11:44
  • Haven't seen it before, but that's what I say : they give 53 years 1 months 28 days from date to date... If you count by full months, you don't have to care about days. You have a mandatory month included. It's like you'd perform all your calculations from the first of each month [Benchmark revisited](http://www.calculator.net/age-calculator.html?today=11%2F01%2F1960&ageat=01%2F01%2F2014&x=76&y=15) and [revisited again](http://www.calculator.net/age-calculator.html?today=04%2F01%2F1986&ageat=01%2F01%2F2014&x=74&y=15) – Grooveek Jan 08 '14 at 11:50
  • Wow man I really appreciate your help from the core of heart :) – Skynet Jan 08 '14 at 11:52
  • I am considering that our calculation is correct, somehow the website is misleading. – Skynet Jan 08 '14 at 12:02
  • I guess there is no way with which I get a day precision in this function – Skynet Jan 08 '14 at 12:04
  • And what is a day precision ? If you take into account daylight saving times and leap years, you'll have days off while talking about months versus number of days. Date computations are all about choices, and the website you talk about make choices. First of all, all months are not 30 days. Between the fifth of February and the fifth of March, do you have one month ? [Joda Tim source code](https://github.com/JodaOrg/joda-time) could be inspiring, I think... – Grooveek Jan 08 '14 at 12:13
  • By day precision I meant if its November 11 ideally a month should complete on December 11. My bad despite a lot of buzz around Joda I could not use it this time. Truly a stitch in time saves nine. – Skynet Jan 08 '14 at 12:16
  • Though I would need time to understand this, check out the last answer on this page: http://stackoverflow.com/questions/13566630/number-of-months-between-2-java-util-date-excluding-day-of-month – Skynet Jan 08 '14 at 12:29