Difference between JodaTime and Calendar for years before 1900

扶醉桌前 提交于 2019-12-12 13:31:29

问题


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?


回答1:


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?




回答2:


After doing some tests I've found out next things:

  1. The problem is my code is that it uses Europe/Athens time zone. Atm it has offset 2 hrs and until 1916 it used to be equal 1:34:52 (thanx Jon Skeet for help). But only Joda Time knows about it =). That's why I had difference since 1582 till 1916 years (not 1900 as I supposed before) So since 1582-10-15 both Sun's Calendar and Joda Time acts similar (if you use UTC time zone)

  2. But if you try to create 1582-10-14 using java.util.Calendar (cal.set(1582, 9, 14, 0, 0, 0);) you'll get Sun Oct 24 00:00:00 EET 1582 (and right date in Joda Time). The same true for 05-14.10.1582. Before 1582-10-05 you'll get right string representation for Calendar, but different milliseconds value. And the difference becoming less by some rule. That's why in my example it was 3 days lag. As far as I remember it's connected with Julian calendar

Thanks all for your help



来源:https://stackoverflow.com/questions/4680198/difference-between-jodatime-and-calendar-for-years-before-1900

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!