本文介绍了1900年之前,JodaTime与Calendar之间的时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用JodaTime lib和java.util.Calendar时,过去同一日期获得的毫秒值不同。
例如,第一年AD

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);
}

默认使用DateTime使用ISOChronology,日历为GregorianCalendar(TH和JA语言环境除外) 。因此我设置了GregorianChronology,但没有任何改变。
执行的结果是

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?

推荐答案

做完一些测试后,我发现了接下来的事情:

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


  1. 问题是我的代码是使用欧洲/雅典时区的。自动取款机偏移了2个小时,直到1916年,它通常等于1:34:52(感谢乔恩·斯凯特(Jon Skeet)帮助)。但是只有Joda Time知道它=)。这就是为什么我从1582年到1916年(而不是我以前想象的1900年)之间存在差异的原因
    因此,自1582-10-15年以来,Sun的Calendar和Joda Time的行为相似(如果您使用UTC时区)

  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)

但是如果您尝试使用java.util.Calendar(cal.set(1582,9,14,0,0,0);)创建1582-10-14会在1582年10月24日星期日00:00:00 EET(正确的日期是乔达时间)。 05-14.10.1582也是如此。在1582-10-05之前,您将获得日历的正确字符串表示形式,但是值是不同的毫秒。而且差异在一定程度上变得越来越小。这就是为什么在我的示例中出现3天的延迟。据我所知,它与朱利安历法有关

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

感谢您的帮助

这篇关于1900年之前,JodaTime与Calendar之间的时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 09:14