1 2 Previous Next 23 Replies Latest reply: Feb 1, 2009 10:57 PM by 807588

# Number of months between two dates

Hi,

I want to find the number of months difference between two months

can any one help me?
• ###### 1. Re: Number of months between two dates
Please explain your requirement better . The user inputs "January","April" and the program outputs 2?
• ###### 2. Re: Number of months between two dates
Hi,

The user input is November(11) and system date is January(1). the diff is -10, its not applicable.
How did I get exact difference
• ###### 3. Re: Number of months between two dates
You still have not clarified your requirements.
• ###### 4. Re: Number of months between two dates
First of all, to get exact guidance, you'll need to ask an exact question.

[How to ask questions the smart way|http://catb.org/~esr/faqs/smart-questions.html]

db
• ###### 5. Re: Number of months between two dates
sankar21 wrote:
Hi,

The user input is November(11) and system date is January(1). the diff is -10, its not applicable.
How did I get exact difference
Absolute value? Take a look at the Math class.
• ###### 6. Re: Number of months between two dates
codingMonkey wrote:
sankar21 wrote:
Hi,

The user input is November(11) and system date is January(1). the diff is -10, its not applicable.
How did I get exact difference
Absolute value? Take a look at the Math class.
I'm just guessing here , i think the OP needed the answer 2.
How many months from January(1) to November(11) ? 11-1=*10 months* (you can argue that there are just 9 months in between January & November)
How many months from November(11) to January(1) ?1-11=-10 , and to avoid the overflow you do -10+12 = *2 months* .

Again i dont know what the requirements of the OP are , just making a guess.
• ###### 7. Re: Number of months between two dates
starting date is 02/11/2008
ending date is 22/01/2009

I need the difference between this two date and get the answer in month
• ###### 8. Re: Number of months between two dates
okay , give the answer for that input too . And also explain how you got that answer.
• ###### 9. Re: Number of months between two dates
also work out the answer in this case
start date : 23-Oct-2008
end date : 22-Jan2009
• ###### 10. Re: Number of months between two dates
sankar21 wrote:
I need the difference between this two date and get the answer in month
You can always use Joda:
``````import org.joda.time.LocalDate;
import org.joda.time.Months;

public class MonthsDifferenceDemo {

public static void main(String[] args) {
// starting date is 02/11/2008
// ending date is 22/01/2009
LocalDate start = new LocalDate("2008-11-02");
LocalDate end = new LocalDate("2009-01-22");
assert 2 == Months.monthsBetween(start, end).getMonths();
}
}``````
How easy is that?

~
• ###### 11. Re: Number of months between two dates
sankar21 wrote:
starting date is 02/11/2008
ending date is 22/01/2009

I need the difference between this two date and get the answer in month
You STILL haven't made it clear enough. Do you only want full months, rounded down? Or do you want the total number of months "touched"? E.g., Is 1/31/08 to 2/1/09 2 months? One month? Zero months? What about 1/31/08 to 2/28/09?
• ###### 12. Re: Number of months between two dates
jverd wrote:
sankar21 wrote:
starting date is 02/11/2008
ending date is 22/01/2009

I need the difference between this two date and get the answer in month
You STILL haven't made it clear enough. Do you only want full months, rounded down? Or do you want the total number of months "touched"? E.g., Is 1/31/08 to 2/1/09 2 months? One month? Zero months? What about 1/31/08 to 2/28/09?
if starting date is 1/31/08 to 2/1/08 then answer is one month
• ###### 13. Re: Number of months between two dates
sankar21 wrote:
if starting date is 1/31/08 to 2/1/08 then answer is one month
assuming you meant 2/1/ *09* in that statement
``````private static int getMonthsBetween(Calendar c, Calendar c1)
{
if (c.before(c1))
return getMonthsBetween(c1, c) * -1;
int months = 0;
int offset = 0;
if (c.get(Calendar.YEAR) - c1.get(Calendar.YEAR) > 1)
months = 12 * (c.get(Calendar.YEAR) - c1.get(Calendar.YEAR) - 1);
offset = c.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
if (offset < 0)
offset += 12;
return months + offset;
}``````
• ###### 14. Re: Number of months between two dates
thank you

its working fine
1 2 Previous Next