Hi,

There are many way to do the calculation.

You can try in the following way

 

Let date1 and date2 are two Date object

                Calendar calendar1 = Calendar.getInstance();

                calendar1.setTime(date1);

                Calendar calendar2 = Calendar.getInstance();

                calendar2.setTime(date2);

                long monthDiff = Math.abs(calendar2.get(Calendar.MONTH) -
calendar1.get(Calendar.MONTH) + (calendar2.get(Calendar.YEAR) -
calendar1.get(Calendar.YEAR)) * 12);

 

here monthDiff is your desired result

 

You can also do the same calculation with the code below

long monthDiff = Math.abs(date2.getMonth() - date1.getMonth() +
(date2.getYear()-date1.getYear()) * 12);

but getMonth() and getYear() methods are Deprecated.

 

 

 

From: java-ee-j2ee-programming-with-passion@googlegroups.com
[mailto:java-ee-j2ee-programming-with-pass...@googlegroups.com] On Behalf Of
sachin bolmal
Sent: Friday, December 18, 2009 9:26 AM
To: java-ee-j2ee-programming-with-passion@googlegroups.com
Subject: [java ee programming] Months between two dates

 

Hi Everybody,

 

Plz Help me in calculating no of months between two dates.

-- 
You received this message because you are subscribed to the Google
Groups "Java EE (J2EE) Programming with Passion!" group.
To post to this group, send email to
java-ee-j2ee-programming-with-passion@googlegroups.com
To unsubscribe from this group, send email to
java-ee-j2ee-programming-with-passion+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/java-ee-j2ee-programming-with-passion?hl=en?h
l=en

-- 
You received this message because you are subscribed to the Google
Groups "Java EE (J2EE) Programming with Passion!" group.
To post to this group, send email to
java-ee-j2ee-programming-with-passion@googlegroups.com
To unsubscribe from this group, send email to
java-ee-j2ee-programming-with-passion+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/java-ee-j2ee-programming-with-passion?hl=en?hl=en

Reply via email to