calculate difference in months

dhanu sudha
Greenhorn
Posts: 11
How to calculate difference in months between 2 dates?

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
I think that's going to lead to the question of what is a month? 365.25/12 days? What's a month before March 30? Two months before March 30? Does (March 30 - 2 months) == (March 30 - 1 month - 1 month)?
Now that I'm confused (how about you?) GregorianCalendar class has an add method that lets you specify +-n of some unit. See if it will take month as the unit. Then try those cases above and see if you like the results!

Vinod Chandana
Ranch Hand
Posts: 59
Originally posted by dhanu sudha:
How to calculate difference in months between 2 dates?

hi Dhanu Sudha,
Try this. Code is pretty self-explainatory.
Regards,
Vinod.
import java.util.GregorianCalendar;
public class CalPractice {
public static void main(String[] args) {
GregorianCalendar c1 = new GregorianCalendar();
GregorianCalendar c2 = new GregorianCalendar();
c1.set(2003, 10, 10);
c2.set(2003, 10, 6);
System.out.println((c1.getTimeInMillis() - c2.getTimeInMillis())/86400000);
}

}