Solved

Date time difference in months

Posted on 2006-10-24
5
226 Views
Last Modified: 2008-01-09
If i have two dates as Calendar instances:  2006-23-02 00:00:00 and 2002-12-09 00:00:00 then i need the number of months between the two dates. how can i do this?

Note: the dates never have times.

// currently this is what i have but it's not completely accurate because i assume that each month is 30 days
private int getNumMonths(Calendar mostrecent, Calendar old, boolean cleartime) {
        if(cleartime) {
            mostrecent.set(Calendar.MILLISECOND, 0);
            mostrecent.set(Calendar.SECOND, 0);
            mostrecent.set(Calendar.MINUTE, 0);
            mostrecent.set(Calendar.HOUR_OF_DAY, 0);

            old.set(Calendar.MILLISECOND, 0);
            old.set(Calendar.SECOND, 0);
            old.set(Calendar.MINUTE, 0);
            old.set(Calendar.HOUR_OF_DAY, 0);
        }
       
        // difference between the 2 dates in millis
        long difference = mostrecent.getTimeInMillis() - old.getTimeInMillis();

        // set the calendar object representing the difference
        Calendar diff = Calendar.getInstance();
        diff.setTimeInMillis(difference);

        long diffMonths = (difference/(24*60*60*1000))/30;
        return(StringTools.strToInt("" + diffMonths));
}
0
Comment
Question by:tbboyett
  • 2
  • 2
5 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 17798439
>>>    long difference = mostrecent.getTimeInMillis() - old.getTimeInMillis();
That's already nearly the answer,

       return(difference/(24*60*60*1000*30);// returns a long, rounded down.
or:

       return(difference/(24.0*60*60*1000*30);// returns a double with a fraction.

Don't forget to type your method long or double.
;JOOP!
0
 
LVL 5

Author Comment

by:tbboyett
ID: 17798516
thanks for the response.

>> return(difference/(24*60*60*1000*30);
this returns a (negative) number that is way off from the number of months between the two dates
0
 
LVL 9

Assisted Solution

by:shinobun
shinobun earned 100 total points
ID: 17798922
Take the difference of (year * 12 + month).
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 300 total points
ID: 17801208
Of course negative if you don't control the sequence of the dates.
Then use:

       return(Math.abs(difference/(24*60*60*1000*30));// returns a long, rounded down.
or:

       return(Math.abs(difference/(24.0*60*60*1000*30));// returns a double with a fraction.

;JOOP!
0
 
LVL 5

Author Comment

by:tbboyett
ID: 17803280
Thanks guys for your help, but i've found a solution that works for what i need.

here it is incase anyone needs it:

private int getNumMonthBetweenTwoDates(Calendar mostrecent, Calendar old, boolean cleartime) {    
        boolean done = false;
        int numMonths = 0;
       
        if(cleartime) {
            mostrecent.set(Calendar.MILLISECOND, 0);
            mostrecent.set(Calendar.SECOND, 0);
            mostrecent.set(Calendar.MINUTE, 0);
            mostrecent.set(Calendar.HOUR_OF_DAY, 0);

            old.set(Calendar.MILLISECOND, 0);
            old.set(Calendar.SECOND, 0);
            old.set(Calendar.MINUTE, 0);
            old.set(Calendar.HOUR_OF_DAY, 0);
        }
       
        while(!done) {
            mostrecent.add(Calendar.MONTH, -1);
            if((mostrecent.get(Calendar.MONTH) == old.get(Calendar.MONTH)) &&
               (mostrecent.get(Calendar.YEAR) == old.get(Calendar.YEAR))) {
               // found the old date month
               
               numMonths ++; // add an extra month to round up
               done = true;
            }
            numMonths ++;
        }
       
        return (numMonths);
}
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse buid path vs tomcat lib path 10 34
How to convert from xls to xlsx using java 7 55
jboss 7.1 start up error 1 40
What browser will run Java? 7 63
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question