Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GregorianCalendar time discontinuity between two days

Tags:

java

I tried to compare the number of ms between two dates and I have problems with some specific dates. example for the 2015 february the 1st:

System.out.println(new GregorianCalendar(2015, 01, 30, 12, 0, 0).getTimeInMillis());
System.out.println(new GregorianCalendar(2015, 01, 31, 12, 0, 0).getTimeInMillis());
System.out.println(new GregorianCalendar(2015, 02, 01, 12, 0, 0).getTimeInMillis());

this returns time in ms which are not ordered chronologically:

1425294000000 
1425380400000 
1425207600000

I already know how to avoid the problem using another class (DateFormat). But I do not understand the problem.

like image 629
Jonathan Avatar asked Dec 03 '25 01:12

Jonathan


1 Answers

31st of February doesn't exist. Month (2nd argument) of the GregorianCalendar constructor is a 0-based index. [0-11]

like image 148
Perfect28 Avatar answered Dec 05 '25 15:12

Perfect28



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!