This discussion is archived
5 Replies Latest reply: Mar 16, 2010 12:45 PM by DrClap RSS

Days difference between 2 dates - 4th April

807580 Newbie
Currently Being Moderated
Following is the code that gives the difference between 2 dates in number of days.
Calendar beginCal = Calendar.getInstance();
beginCal.set(2010, Calendar.APRIL, 2, 8, 58);
Calendar endCal = Calendar.getInstance();
endCal.set(2010, Calendar.APRIL, 10, 8, 58);
long diff = endCal.getTimeInMillis() - beginCal.getTimeInMillis();
System.out.println((diff/1000/60/60/24) + 1);
I am expecting abive code to return 9, as the number of days between 2 and 10, including 2, is 9. But interestingly this gives 8 as output.
Calendar beginCal = Calendar.getInstance();
beginCal.set(2010, Calendar.APRIL, 3, 8, 58);
Calendar endCal = Calendar.getInstance();
endCal.set(2010, Calendar.APRIL, 4, 8, 58);
long diff = endCal.getTimeInMillis() - beginCal.getTimeInMillis();
System.out.println((diff/1000/60/60/24) + 1);
Above code should have returned 2.

I am using j2sdk1.4.2_04. Am I missing something here? Any idea?