I want to convert two dates to milliseconds, but the given result is wrong, is there anything wrong with code?, how to solve it otherwise…
GregorianCalendar c1 = new GregorianCalendar();
GregorianCalendar c2 = new GregorianCalendar();
c1.set(2013, 01, 31, 16, 44, 49);
c2.set(2013, 02, 01, 12, 59, 55);
System.out.println("c1 = "+c1.getTimeInMillis()+"\nc2 = "+ c2.getTimeInMillis());
output:
c1 = 1362300289619
c2 = 1362113995619
After caculating the time, it gives this result: 0Month -2Day -3Hour -44Min -54Sec
which is wrong. And must be something like this: 0Month 0Day 20Hour 15Min 6Sec.
c1 is bigger then c2, because it gives wrong result, but why it happened that c1 become bigger then c2, in such case it is not possible to calculate the time between two dates.
if someone knows please help me, thanks in advance.
Months are 0-based, so in your example, c1 is the 31st of February, which is interpreted as the 3rd of March (only 28 days in February) and c2 is on the 1st of March.