Calc the difference in months.

I have the following fields.

basicPolicyInfo_assoc_PolEffDate

basicPolicyInfo_assoc_PolExpDate

I want to find the number of months between the 2
Michael FranzCFOAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniel WilsonCommented:
have a look at http://technet.microsoft.com/en-us/library/ms189794.aspx

select datediff(month, asicPolicyInfo_assoc_PolEffDat, basicPolicyInfo_assoc_PolExpDate) from myTable;
0
Michael FranzCFOAuthor Commented:
sorry, I mispoke the program uses Java. Not sure what to do now.... I did find this....but could not get it to work.

monthDiff = endDate.get(Calendar.MONTH) - startDate.get(Calendar.MONTH);
0
Daniel WilsonCommented:
OK, so it's not a SQL question, but a Java question, and they're not "fields" but variables.  Right?

<Link to StackOverflow removed>
I'd have a look at the joda-time library.

Sorry, I'm no Java expert.
0
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Alexandre SimõesManager / Technology SpecialistCommented:
Date handling it's simply a mess in JAVA... a huge mess...
You'll fund yourself casting and converting to cast and convert again to get something done.

My advise is to use Joda-Time to deal with dates.

Use the monthsBetween to get it done.
0
Amitkumar PSr. ConsultantCommented:
0
dpearsonCommented:
Using Joda Time is certainly a solid choice.

If you don't wish to do that you can compute the difference like this, just using the standard JDK - assuming cal2 is after cal1:

int monthDiffs(Calendar cal1, Calendar cal2) {
               if (cal1.after(cal2))
                   throw new IllegalArgumentException("Cal2 should be after cal1") ;

            // Add in any year differences
            int months = (cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR))*12 ;
            
            // Then add the months different within this year
            months += cal2.get(Calendar.MONTH) - cal1.get(Calendar.MONTH) ;
      
            // Allow for which day we're on
            if (cal2.get(Calendar.DAY_OF_MONTH) < cal1.get(Calendar.DAY_OF_MONTH))
                  months -= 1 ;

               return months ;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zzynxSoftware engineerCommented:
Antoher way of doing it in plain java without Joda Time is simply in a loop where you keep adding a month until your date falls after the 2nd date:

int monthDiffs(date date1, Date date2) {
    if (date1.after(date2))
                   throw new IllegalArgumentException("Date2 should be after date1") ;    

    Calendar cal = Calendar.getInstance();
    cal.setTime(date1);
    int nrOfMonths = 0;

    do {
         cal.add(Calendar.MONTH, 1);
         nrOfMonths++;
    } while (cal.getTime().before(date2));

    return nrOfMonths; // or nrOfMonths-1; according to your taste (*)
}

Open in new window


(*) = If the dates are eg. 15-dec-2012 and 1-Jan-2013, do you consider that as a month difference of 1 or 0?
0
CEHJCommented:
http://technojeeves.com/joomla/index.php/free/71-difference-between-two-dates-in-java

will give you days. You can then compute months (which is somewhat subjective anyway)
0
Michael FranzCFOAuthor Commented:
thanks for all the help. I finally get to move on to completing the report... Yea!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.