import java.util.GregorianCalendar;
public class FullYear {
public static void main(String[] args) {
GregorianCalendar cal1 = new GregorianCalendar(2011,2,9);
GregorianCalendar cal2 = new GregorianCalendar(2011,2,12);
long ms1 = cal1.getTime().getTime();
long ms2 = cal2.getTime().getTime();
long difMs = ms2-ms1;
long msPerDay = 1000*60*60*24;
System.out.println(difMs);
System.out.println(msPerDay);
double days = difMs / msPerDay;
System.out.println(days);
GregorianCalendar cal11 = new GregorianCalendar(2011,2,9);
GregorianCalendar cal22 = new GregorianCalendar(2011,2,19);
long ms11 = cal11.getTime().getTime();
long ms22 = cal22.getTime().getTime();
long difMs1 = ms22-ms11;
long msPerDay1 = 1000*60*60*24;
System.out.println(difMs1);
System.out.println(msPerDay1);
double days2 = difMs1 / msPerDay1;
System.out.println(days2);
}
}
This outputs 3.0 and 9.0 (I expect 10!!). This does integer division, so 9.0 is actually 9.95. However, my question is…
- The difference between February 9th and February 19th is 860400000 milliseconds, or 239 hours.
- The difference between February 9th and February 12th is 259200000 milliseconds, or 72 hours.
Shouldn’t the first difference be 240 hours?
You are calculating the differences for March, not February, and you have engulfed the onset of Daylight Saving Time.