0

I got the timestamp of '1000-01-01 00:00:00' in two different results. Does anybody know why?

TimeZone timeZone = TimeZone.getTimeZone(ZoneOffset.UTC);
GregorianCalendar calendar = new GregorianCalendar(timeZone);
calendar.clear();
calendar.set(1000, 0, 1, 0, 0, 0);
System.out.println(calendar.getTimeInMillis()); // print -30609792000000
System.out.println(ZonedDateTime.of(1000, 1, 1,0, 0, 0, 0, timeZone.toZoneId()).toInstant().toEpochMilli()); // print -30610224000000
Anish B.
  • 9,111
  • 3
  • 21
  • 41
ho yoje
  • 355
  • 2
  • 9
  • 2
    You've asked for 2 different dates. Look at the second field – hardillb Sep 18 '19 at 12:01
  • 1
    @hardillb Not so. The month of January is 0 with `GregorianCalendar` and 1 with `ZonedDateTIme`, so it’s the same date. – Ole V.V. Sep 18 '19 at 12:24
  • 2
    @OleV.V. well that was a bloody stupid decision by whoever created the API – hardillb Sep 18 '19 at 12:26
  • @hardillb Yes, month 0-11 was a stupid design decision. That is one of many reasons why `GregorianCalendar` class was supplanted years ago by the modern `ZonedDateTime` calendar. You should **no longer be using `GregorianCalendar`**. – Basil Bourque Sep 20 '19 at 01:47

1 Answers1

3

GregorianCalendar despite its name uses the Julian calendar for the time before the Gregorian calendar was introduced from 1582 and onward. ZonedDateTime by contrast uses the Proleptic Gregorian calendar, that is, extrapolates the Gregorian calendar into the centuries where it wasn’t used at that time.

So you are really using two calendar systems. Which explains why you are getting two different results.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161