Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between JodaTime and Calendar for years before 1900

I'm getting different values in milliseconds for the same date in past while using JodaTime lib and java.util.Calendar. For example for the first year AD

void test() {
    int year = 1;
    DateTime dt = new DateTime(year, 1,1,0,0,0,0);
    dt = dt.toDateTime(GregorianChronology.getInstance());

    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(year, 0, 1, 0, 0, 0);

    DateTime endDate = new DateTime(cal.getTimeInMillis());
    endDate = endDate.toDateTime(GregorianChronology.getInstance());

    System.out.println("JodaTime: " + dt);
    System.out.println("JodaTime, ms: " + dt.getMillis());
    System.out.println("Calendar: " + cal.getTime());
    System.out.println("Calendar, ms: " + cal.getTimeInMillis());
    System.out.println("JodaTime by Calendar: " + endDate);
}

By default DateTime use ISOChronology and Calendar is GregorianCalendar (except TH and JA locales). So I set GregorianChronology, but nothing changed. Result of execution is

JodaTime:       0001-01-01T00:00:00.000+01:34:52
JodaTime, ms:   -62135602492000
Calendar:       Sat Jan 01 00:00:00 EET 1
Calendar, ms:   -62135776800000
JodaTime by Calendar:   0000-12-29T23:34:52.000+01:34:52

Could someone suggest am I wrong with something?

like image 995
Yury Khrol Avatar asked Nov 19 '25 22:11

Yury Khrol


1 Answers

My guess is that it's to do with your time zone. Specify UTC to remove that from the equation.

Some time zones (e.g. Paris) have had some "interesting" transitions.

For example, zoneinfo suggests that Europe/Athens had an offset of 1:34:52 until 1916 (switching from an abbreviation of LMT to AMT in 1895).

Belarus had an offset of 1:50:16 until 1880.

Perhaps Java is using a different source of time zone data which doesn't include these oddities?

like image 173
Jon Skeet Avatar answered Nov 21 '25 10:11

Jon Skeet