I am trying to convert a date into milliseconds with the following code:
GregorianCalendar gc = new GregorianCalendar(TimeZone.getTimeZone("UTC")); gc.clear(); gc.set(1900, 1, 1); long left = gc.getTimeInMillis();
I get left=-2206310400000
, but when I check here, I should get -2208988800000.
What am I doing wrong?
Advertisement
Answer
You’re using 1 for the month number, which means February.
You mean
gc.set(1900, 0, 1);
From the docs:
month – the value used to set the MONTH calendar field. Month value is 0-based. e.g., 0 for January.
Yes, the Java date/time API is broken. If you’re doing any significant amount of work in dates/times, I’d suggest you use Joda Time instead.
long left = new DateTime(1900, 1, 1, 0, 0, DateTimeZone.UTC).getMillis();