1900 年之前的 JodaTime 和 Calendar 之间的差异
使用 JodaTime lib 和 java.util.Calendar 时,我在过去的同一日期得到不同的值(以毫秒为单位)。 例如,第一年 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);
}
默认情况下 DateTime 使用 ISOChronology,日历为 GregorianCalendar(TH 和 JA 区域设置除外)。所以我设置了公历纪年法,但没有任何改变。 执行的结果是
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
有人可以建议我是否做错了什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测是,这与您的时区有关。指定 UTC 以将其从等式中删除。
一些时区(例如巴黎)发生了一些“有趣”的转变。
例如,zoneinfo 表明欧洲/雅典在 1916 年之前的偏移量为 1:34:52(1895 年从 LMT 缩写改为 AMT)。
直到 1880 年,白俄罗斯的偏移量为 1:50:16。
也许 Java 使用的是不同的时区数据源,其中不包括这些奇怪的情况?
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?
做了一些测试后,我发现了接下来的事情:
问题是我的代码是它使用欧洲/雅典时区。 Atm 时差为 2 小时,直到 1916 年,它曾经等于 1:34:52(感谢 Jon Skeet 的帮助)。但只有乔达时间知道=)。这就是为什么我从 1582 年到 1916 年(而不是我之前认为的 1900 年)之间存在差异
因此,自 1582 年 10 月 15 日起,太阳日历和乔达时间的行为相似(如果您使用 UTC 时区)
但如果您尝试使用 java.util.Calendar (cal.set(1582, 9, 14, 0, 0, 0);) 您将得到 1582 年 10 月 24 日星期日 00:00:00 EET 1582(以及乔达时间的正确日期)。 1582 年 10 月 5 日至 14 日也是如此。在 1582 年 10 月 5 日之前,您将获得日历的正确字符串表示形式,但毫秒值不同。而且按照某种规则,差异会变得越来越小。这就是为什么在我的例子中延迟了 3 天。据我记得它与儒略历有关
谢谢大家的帮助
After doing some tests I've found out next things:
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)
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