1582年10月15日之前的日历到日期的转换。公历到儒略历的切换 [英] Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch

查看:204
本文介绍了1582年10月15日之前的日历到日期的转换。公历到儒略历的切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于公历的开始日期是1582年10月15日,请考虑以下测试,并请帮助我了解会发生什么情况:

Given that Gregorian Calendar start date is 15 Oct 1582, please consider the following tests and please help me understanding what happens:

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeFactory;
import org.junit.Test;

@Test
public void gregorianToDateConversion() {

    GregorianCalendar calendar = null;
    Date date = null;

    calendar = new GregorianCalendar(1582, 9, 04);
    date = calendar.getTime();
    System.out.println("new GregorianCalendar(1582, 9, 4) -> calendar[DAY_OF_MONTH ["+calendar.get(Calendar.DAY_OF_MONTH)+"], MONTH ["+calendar.get(Calendar.MONTH)+"], YEAR ["+calendar.get(Calendar.YEAR)+"]]");
    System.out.println("calendar.getTime() -> Date:" + date);

    calendar = new GregorianCalendar(1582, 9, 05);
    date = calendar.getTime();
    System.out.println("\nnew GregorianCalendar(1582, 9, 5) -> calendar[DAY_OF_MONTH ["+calendar.get(Calendar.DAY_OF_MONTH)+"], MONTH ["+calendar.get(Calendar.MONTH)+"], YEAR ["+calendar.get(Calendar.YEAR)+"]]");
    System.out.println("calendar.getTime() -> Date:" + date);

    calendar = new GregorianCalendar(1582, 9, 14);
    date = calendar.getTime();
    System.out.println("\nnew GregorianCalendar(1582, 9, 14) -> calendar[DAY_OF_MONTH ["+calendar.get(Calendar.DAY_OF_MONTH)+"], MONTH ["+calendar.get(Calendar.MONTH)+"], YEAR ["+calendar.get(Calendar.YEAR)+"]]");
    System.out.println("calendar.getTime() -> Date:" + date);

    calendar = new GregorianCalendar(1582, 9, 15);
    date = calendar.getTime();
    System.out.println("\nnew GregorianCalendar(1582, 9, 15) -> calendar[DAY_OF_MONTH ["+calendar.get(Calendar.DAY_OF_MONTH)+"], MONTH ["+calendar.get(Calendar.MONTH)+"], YEAR ["+calendar.get(Calendar.YEAR)+"]]");
    System.out.println("calendar.getTime() -> Date:" + date);

    String dateToParse = null;

    dateToParse = "1582-10-04";
    System.out.println("\nString to parse: " + dateToParse);
    calendar = parseDateTime(dateToParse);
    if(calendar != null) {
        date = calendar.getTime();
        System.out.println("datatypeFactory.newXMLGregorianCalendar(s).toGregorianCalendar() -> \n    calendar[DAY_OF_MONTH ["+calendar.get(Calendar.DAY_OF_MONTH)+"], MONTH ["+calendar.get(Calendar.MONTH)+"], YEAR ["+calendar.get(Calendar.YEAR)+"]]");
        System.out.println("calendar.getTime() -> Date: " + date);
    }

    dateToParse = "1582-10-05";
    System.out.println("\nString to parse: " + dateToParse);
    calendar = parseDateTime(dateToParse);
    if(calendar != null) {
        date = calendar.getTime();
        System.out.println("datatypeFactory.newXMLGregorianCalendar(s).toGregorianCalendar() -> \n    calendar[DAY_OF_MONTH ["+calendar.get(Calendar.DAY_OF_MONTH)+"], MONTH ["+calendar.get(Calendar.MONTH)+"], YEAR ["+calendar.get(Calendar.YEAR)+"]]");
        System.out.println("calendar.getTime() -> Date: " + date);
    }

    dateToParse = "1582-10-14";
    System.out.println("\nString to parse: " + dateToParse);
    calendar = parseDateTime(dateToParse);
    if(calendar != null) {
        date = calendar.getTime();
        System.out.println("datatypeFactory.newXMLGregorianCalendar(s).toGregorianCalendar() -> \n    calendar[DAY_OF_MONTH ["+calendar.get(Calendar.DAY_OF_MONTH)+"], MONTH ["+calendar.get(Calendar.MONTH)+"], YEAR ["+calendar.get(Calendar.YEAR)+"]]");
        System.out.println("calendar.getTime() -> Date: " + date);
    }

    dateToParse = "1582-10-15";
    System.out.println("\nString to parse: " + dateToParse);
    calendar = parseDateTime(dateToParse);
    if(calendar != null) {
        date = calendar.getTime();
        System.out.println("datatypeFactory.newXMLGregorianCalendar(s).toGregorianCalendar() -> \n    calendar[DAY_OF_MONTH ["+calendar.get(Calendar.DAY_OF_MONTH)+"], MONTH ["+calendar.get(Calendar.MONTH)+"], YEAR ["+calendar.get(Calendar.YEAR)+"]]");
        System.out.println("calendar.getTime() -> Date: " + date);
    }
}
private GregorianCalendar parseDateTime(String s) {
    DatatypeFactory datatypeFactory = null;
    try {
        datatypeFactory = DatatypeFactory.newInstance();
        return datatypeFactory.newXMLGregorianCalendar(s).toGregorianCalendar();
    } catch (Exception e) {
        e.printStackTrace();
    } 
    return null;
}

结果如下:

new GregorianCalendar(1582, 9, 4) -> calendar[DAY_OF_MONTH [4], MONTH [9], YEAR [1582]] **OK**
calendar.getTime() -> Date:Thu Oct 04 00:00:00 CET 1582 **OK**

new GregorianCalendar(1582, 9, 5) -> calendar[DAY_OF_MONTH [15], MONTH [9], YEAR [1582]] **+ 10 days ??**
calendar.getTime() -> Date:Fri Oct 15 00:00:00 CET 1582 **coherent with calendar**

new GregorianCalendar(1582, 9, 14) -> calendar[DAY_OF_MONTH [24], MONTH [9], YEAR [1582]] **+ 10 days ??**
calendar.getTime() -> Date:Sun Oct 24 00:00:00 CET 1582 **coherent with calendar**

new GregorianCalendar(1582, 9, 15) -> calendar[DAY_OF_MONTH [15], MONTH [9], YEAR [1582]] **OK**
calendar.getTime() -> Date:Fri Oct 15 00:00:00 CET 1582 **OK**

String to parse: 1582-10-04
datatypeFactory.newXMLGregorianCalendar(s).toGregorianCalendar() -> 
    calendar[DAY_OF_MONTH [4], MONTH [9], YEAR [1582]]
calendar.getTime() -> Date: Mon Sep 24 00:00:00 CET 1582 **Not coherent with calendar. Conversion to julian date?**

String to parse: 1582-10-05
datatypeFactory.newXMLGregorianCalendar(s).toGregorianCalendar() -> 
    calendar[DAY_OF_MONTH [5], MONTH [9], YEAR [1582]]
calendar.getTime() -> Date: Tue Sep 25 00:00:00 CET 1582 **Not coherent with calendar. Conversion to julian date?**

String to parse: 1582-10-14
datatypeFactory.newXMLGregorianCalendar(s).toGregorianCalendar() -> 
    calendar[DAY_OF_MONTH [14], MONTH [9], YEAR [1582]] **OK**
calendar.getTime() -> Date: Thu Oct 04 00:00:00 CET 1582 **Not coherent with calendar. Conversion to julian date?**

String to parse: 1582-10-15
datatypeFactory.newXMLGregorianCalendar(s).toGregorianCalendar() -> 
    calendar[DAY_OF_MONTH [15], MONTH [9], YEAR [1582]] **OK**
calendar.getTime() -> Date: Fri Oct 15 00:00:00 CET 1582 **OK**

您能澄清一下我吗怎么了?
非常感谢。

Could you clarify me what is happening? Thank you very much.

编辑:
谢谢,我已修正有关要解析的日期格式的错误:现在全部格式为yyyy-MM-gg,并且不会引发任何异常。

Thank you, I've fixed my mistakes about the format of the date to parse: now all have the form yyyy-MM-gg and no exceptions are thrown anymore.

现在,我尝试澄清自己的债务。

Now I try to clarify my dubts.

由什么决定我们是否使用多历的公历?

通过汇总:

java.util.GregorianCalendar代表1582年10月15日之前的儒略历和此日期之后的公历。

所以,这证明了新的GregorianCalendar(...)中的行为是正确的。

如果我这样做:

What determines whether or not we are using the proleptic gregorian calendar?
By summarizing:
java.util.GregorianCalendar represents a julian calendar for dates before 15 Oct 1582 and a gregorian calendar after this date. Is it right?
So, this justifies the behaviour in the new GregorianCalendar(...)...
If I do:

new GregorianCalendar(1582, 9, 4) 

我有

calendar[DAY_OF_MONTH [4], MONTH [9], YEAR [1582]]  

因为儒略历中存在1582年10月4日,而我创建的日历代表儒略日。

将此日历转换为日期,我们有:

because 4 Oct 1582 exists in the julian calendar and the calendar I've created represents a julian date.
Converting this calendar to date, we have:

Date:Thu Oct 04 00:00:00 CET 1582, consistent with Calendar.  

如果我这样做:

new GregorianCalendar(1582, 9, 5)   

我获得

calendar[DAY_OF_MONTH [15], MONTH [9], YEAR [1582]]  

因为10月5日在儒略历和公历中都不存在。因此,GregorianCalendar构造函数创建的日期恰好是10月4日之后的1天,即公历的第一天,即10月15日。然后,新日期在公历系统中表示。

我将具有相同的行为,创建一个新的日历,将日期从(1582,9,5)传递到(1582,9,14)。 >
在上面的示例中,转换后的java.util.Date再次与Calendar一致

because the day 5 of October neither exists in julian nor in gregorian calendar. So the GregorianCalendar constructor creates a date exactely 1 day after the 4 of October, that is the first day of the gregorian calendar, the 15 of October. The new date is then expressed in gregorian calendar system.
I'll have the same behaviour creating a new calendar passing dates from (1582, 9, 5) to (1582, 9, 14).
In the example above, the converted java.util.Date is again consistent with the Calendar

Date:Fri Oct 15 00:00:00 CET 1582  

如果我这样做:

new GregorianCalendar(1582, 9, 15)  

我有

calendar[DAY_OF_MONTH [15], MONTH [9], YEAR [1582]]  

因为公历中存在10月15日,因此在该系统中表示了新日期。
转换为日期的日历再次保持一致:

because 15 of October exists in gregorian calendar and the new date is expressed in this system. The Calendar converted into Date is again consistent:

Date:Fri Oct 15 00:00:00 CET 1582  

这意味着GregorianCalendar构造函数假定我在日历系统中输入在给定时期有效的日期:换句话说,假设我输入的日期早于10月04日或等于10月04日,则我认为该日期是根据儒略历计算的,并且该日期在此系统中表示,如果我输入的日期在10月5日至14日之间,则该计数有效从朱利安历法的结束日期(10月4日)到我输入的日期之间的天数,并将这几天加到公历的开始日期(10月15日),并用公历系统表示新的日期,如果我输入一个日期在10月15日(或等于10月15日)之后,假定我输入的日期是公历,并且新的日期在该系统中表示。

这是否正确?

这不是吗?多动的行为,对吗?如果该日历是公制的阳历,那么我希望可以使用

This means that the GregorianCalendar constructor assumes that I enter dates in the calendar system that is valid in the given epoch: in other words assumes that if I enter a date prior then or equal to 04 October I'm thinking the date according to the julian calendar and the date is expressed in this system, if I enter a date between 05 and 14 October it counts the number of days between the end date of julian calendar (04 october) and the date I entered and sums this days to the start date of gregorian calendar (15 October) and express the new date in gregorian system, and if I enter a date after or equal to 15 October it assumes that the date I entered is meant in gregorian calendar and the new date is expressed in this system.
Is all this correct?
This is not a proleptic behaviour, right? If the calendar was a proleptic gregorian calendar I would expect that initializing it with

new GregorianCalendar(1582, 9, 5)  

我会得到

calendar[DAY_OF_MONTH [5], MONTH [9], YEAR [1582]]  

如果公历系统中不存在该日期,则也设置10月5日,如果公历日历的第一天是10天,则等于9月25日的儒略日(如果我没有记错的话)。

如何确定是否使用公历阳历?

that is the 5 of October is set also if the date does not exists in the gregorian system and, being 10 days befor the first day of gregorian calendar, it would be equal to the julian date 25 september, if I'm not mistaken.
How can I decide to use the proleptic gregorian calendar or not?

解析一个字符串并通过XmlGregorianCalendar创建一个新的GregorianCalendar,这种行为在我看来似乎有所不同。

解析 1582-10-04我获得

Parsing a String and creating a new GregorianCalendar passing through the XmlGregorianCalendar, the behaviour seems to me different.
Parsing "1582-10-04" I obtain

calendar[DAY_OF_MONTH [4], MONTH [9], YEAR [1582]] 

(与上面的示例相同。),但有日期

(same as the example above..) but a date

Date: Mon Sep 24 00:00:00 CET 1582 

(在这里,我们有一个朱利安日期,对吗?但是在上面的示例中(GregorianCalendar是由其构造函数创建的),日期等于日历。为什么现在却不同了?

(Here we have a julian date, is it right? But in the example above (where the GregorianCalendar was created by its constructor) the Date was equal to the Calendar. Why now it is different?

解析字符串 1582-10-05

Parsing the string "1582-10-05" we have

calendar[DAY_OF_MONTH [5], MONTH [9], YEAR [1582]] 

(现在,我们有一个公历,对吗?)< br>
但又是不同的日期

(Now we have a Proleptic gregorian calendar, right?)
But again a different Date

Date: Tue Sep 25 00:00:00 CET 1582 (Julian Date? Why?)  

解析 1582-10-14的行为相同

Same behaviour parsing "1582-10-14"

calendar[DAY_OF_MONTH [14], MONTH [9], YEAR [1582]]  
Date: Thu Oct 04 00:00:00 CET 1582  

如果我们最终解析 1582-10-15,我们回到格里高利时代的广告都变得一致:

If we finally parse "1582-10-15", we come back in the Gregorian age ad all become consistent:

calendar[DAY_OF_MONTH [15], MONTH [9], YEAR [1582]]  
Date: Fri Oct 15 00:00:00 CET 1582  

您能帮助我理解所有这些行为吗?

非常感谢。

Can you help me to understand all this behaviours?
Thank you very much.

编辑2:

感谢您的回答,如有疑问对我来说仍然。
我尝试了以下代码:

Thank you for your answers, also if some doubt remains for me. I tried the following code:

GregorianCalendar proleptic = new GregorianCalendar();
proleptic.setGregorianChange(new Date(Long.MIN_VALUE));
proleptic.set(Calendar.DAY_OF_MONTH, 5);
proleptic.set(Calendar.MONTH, 9);
proleptic.set(Calendar.YEAR, 1582);
System.out.println("proleptic [DAY_OF_MONTH ["+proleptic.get(Calendar.DAY_OF_MONTH)+"], MONTH ["+proleptic.get(Calendar.MONTH)+"], YEAR ["+proleptic.get(Calendar.YEAR)+"]");
Date prolepticDate = proleptic.getTime();
System.out.println("prolepticDate ["+prolepticDate+"]");

输出为:

proleptic [DAY_OF_MONTH [5], MONTH [9], YEAR [1582]
prolepticDate [Tue Sep 25 00:24:07 CET 1582]

现在我的日历为DAY_OF_MONTH,与我设置的日历保持一致,所以我认为这是一种多事的行为,对吗?
但是我再次问为什么通过getTime()方法获得的日期不代表同一天,而是前10天。
再次感谢。

Now I have the calendar with DAY_OF_MONTH consistent with what I've set, so I think this is a proleptic behaviour, right? But again I ask why the Date, obtained by the getTime() method, does not represent the same day, but 10 days before. Thanks again.

编辑3:

问题是对我来说更清晰一点,但并不完全清楚。
我无法详细了解从GregorianCalendar到Date的转换是什么逻辑,以及为什么一个多用公历和传统的日历(每个日历和日期,月份和年份具有相同的参数)并不总是代表相同的时间的瞬间。
我做了一些进一步的测试。

The question is for me a bit clearer, but not completely. I did not understand in detail by what logic the conversion from GregorianCalendar to Date is done and why a proleptic Gregorian Calendar and a traditional one, each constructed with the same parameters as DAY, MONTH and YEAR, not always represent the same instant of time. I did some further tests.

GregorianCalendar proleptic = null;
GregorianCalendar calendar = null;
Date date = null;

// ---- JULIAN PERIOD ----
proleptic = new GregorianCalendar();
proleptic.clear(); 
proleptic.setGregorianChange(new Date(Long.MIN_VALUE));
proleptic.set(Calendar.DAY_OF_MONTH, 5);
proleptic.set(Calendar.MONTH, Calendar.FEBRUARY);
proleptic.set(Calendar.YEAR, 1582);
System.out.println("proleptic_calendar_5Feb1582 [DAY_OF_MONTH ["+proleptic.get(Calendar.DAY_OF_MONTH)+"], MONTH ["+proleptic.get(Calendar.MONTH)+"], YEAR ["+proleptic.get(Calendar.YEAR)+"]");
date = proleptic.getTime();
System.out.println("date_5Feb1582 from proleptic ["+date+"]");

calendar = new GregorianCalendar(1582, 1, 5);
date = calendar.getTime();
System.out.println("new GregorianCalendar(1582, 1, 5)            -> [DAY_OF_MONTH ["+calendar.get(Calendar.DAY_OF_MONTH)+"], MONTH ["+calendar.get(Calendar.MONTH)+"], YEAR ["+calendar.get(Calendar.YEAR)+"]]");
System.out.println("new GregorianCalendar(1582, 1, 5) .getTime() -> Date:" + date);

System.out.println("proleptic_calendar_5Feb1582 in millis ["+proleptic.getTimeInMillis()+"], new GregorianCalendar(1582, 1, 5) in millis ["+calendar.getTimeInMillis()+"], millis are equal ["+ (proleptic.getTimeInMillis() == calendar.getTimeInMillis()) +"]");
//--------

System.out.println("\n");

// ---- transition period ----
proleptic = new GregorianCalendar();
proleptic.clear(); 
proleptic.setGregorianChange(new Date(Long.MIN_VALUE));
proleptic.set(Calendar.DAY_OF_MONTH, 8);
proleptic.set(Calendar.MONTH, Calendar.OCTOBER);
proleptic.set(Calendar.YEAR, 1582);
System.out.println("proleptic_calendar_8Oct1582 [DAY_OF_MONTH ["+proleptic.get(Calendar.DAY_OF_MONTH)+"], MONTH ["+proleptic.get(Calendar.MONTH)+"], YEAR ["+proleptic.get(Calendar.YEAR)+"]");
date = proleptic.getTime();
System.out.println("date_5Oct1582 from proleptic ["+date+"]");

calendar = new GregorianCalendar(1582, 9, 8);
date = calendar.getTime();
System.out.println("new GregorianCalendar(1582, 9, 8)            -> [DAY_OF_MONTH ["+calendar.get(Calendar.DAY_OF_MONTH)+"], MONTH ["+calendar.get(Calendar.MONTH)+"], YEAR ["+calendar.get(Calendar.YEAR)+"]]");
System.out.println("new GregorianCalendar(1582, 9, 8) .getTime() -> Date:" + date);

System.out.println("proleptic_calendar_8Oct1582 in millis ["+proleptic.getTimeInMillis()+"], new GregorianCalendar(1582, 9, 8) in millis ["+calendar.getTimeInMillis()+"], millis are equal ["+ (proleptic.getTimeInMillis() == calendar.getTimeInMillis()) +"]");
//--------

System.out.println("\n");

// ---- GREGORIAN PERIOD ----
proleptic = new GregorianCalendar();
proleptic.clear(); 
proleptic.setGregorianChange(new Date(Long.MIN_VALUE));
proleptic.set(Calendar.DAY_OF_MONTH, 5);
proleptic.set(Calendar.MONTH, Calendar.DECEMBER);
proleptic.set(Calendar.YEAR, 1582);
System.out.println("proleptic_calendar_5Dec1582 [DAY_OF_MONTH ["+proleptic.get(Calendar.DAY_OF_MONTH)+"], MONTH ["+proleptic.get(Calendar.MONTH)+"], YEAR ["+proleptic.get(Calendar.YEAR)+"]");
date = proleptic.getTime();
System.out.println("date_5Dec1582 from proleptic ["+date+"]");

calendar = new GregorianCalendar(1582, 11, 5);
date = calendar.getTime();
System.out.println("new GregorianCalendar(1582, 11, 5)            -> [DAY_OF_MONTH ["+calendar.get(Calendar.DAY_OF_MONTH)+"], MONTH ["+calendar.get(Calendar.MONTH)+"], YEAR ["+calendar.get(Calendar.YEAR)+"]]");
System.out.println("new GregorianCalendar(1582, 11, 5) .getTime() -> Date:" + date);

System.out.println("proleptic_calendar_5Dec1582 in millis ["+proleptic.getTimeInMillis()+"], new GregorianCalendar(1582, 11, 5) in millis ["+calendar.getTimeInMillis()+"], millis are equal ["+ (proleptic.getTimeInMillis() == calendar.getTimeInMillis()) +"]");
//--------

输出为:

proleptic_calendar_5Feb1582 [DAY_OF_MONTH [5], MONTH [1], YEAR [1582]
date_5Feb1582 from proleptic [Fri Jan 26 00:00:00 CET 1582]
new GregorianCalendar(1582, 1, 5)            -> [DAY_OF_MONTH [5], MONTH [1], YEAR [1582]]
new GregorianCalendar(1582, 1, 5) .getTime() -> Date:Mon Feb 05 00:00:00 CET 1582
proleptic_calendar_5Feb1582 in millis [-12241069200000], new GregorianCalendar(1582, 1, 5) in millis [-12240205200000], millis are equal [false]

proleptic_calendar_8Oct1582 [DAY_OF_MONTH [8], MONTH [9], YEAR [1582]
date_5Oct1582 from proleptic [Fri Sep 28 00:00:00 CET 1582]
new GregorianCalendar(1582, 9, 8)            -> [DAY_OF_MONTH [18], MONTH [9], YEAR [1582]]
new GregorianCalendar(1582, 9, 8) .getTime() -> Date:Mon Oct 18 00:00:00 CET 1582
proleptic_calendar_8Oct1582 in millis [-12219901200000], new GregorianCalendar(1582, 9, 8) in millis [-12219037200000], millis are equal [false]

proleptic_calendar_5Dec1582 [DAY_OF_MONTH [5], MONTH [11], YEAR [1582]
date_5Dec1582 from proleptic [Sun Dec 05 00:00:00 CET 1582]
new GregorianCalendar(1582, 11, 5)            -> [DAY_OF_MONTH [5], MONTH [11], YEAR [1582]]
new GregorianCalendar(1582, 11, 5) .getTime() -> Date:Sun Dec 05 00:00:00 CET 1582
proleptic_calendar_5Dec1582 in millis [-12214890000000], new GregorianCalendar(1582, 11, 5) in millis [-12214890000000], millis are equal [true]

第一次检验(1582年2月5日)指儒略历的时期,第二次检验(1582年10月8日)属于过渡时期,即公历第三个时期(1582年12月5日)。
然后我想注意以下事情:

The first test (5 Feb 1582) refers to a period belonging to the Julian calendar, the second (8 Oct 1582) belongs to the period of transition, the third (5 Dec 1582) to the Gregorian Calendar period. Then I think to notice the following things:

朱利安时代


  • 多久的公历似乎代表了期望的日期

  • 如@Meno Hochschild所解释的,将获得的日期转换为儒略日期(-10天)

  • 传统的公历似乎也可以代表所需的日期...

  • 但是获取的日期与前一个日期不同

  • 实际上,两个公历代表两个不同的时刻,请参见毫秒值

  • the proleptic Gregorian Calendar seems to represent the desired date
  • the obtained Date, as explained by @Meno Hochschild, is converted to a Julian date (-10 days)
  • the traditional Gregorian Calendar also seems to represent the desired date...
  • but the obtained Date is different from the previous one
  • infact, the two Gregorian Calendar represent two different instant of time, see the millisecond values

过渡期


  • 阳历似乎代表了期望的日期

  • 所获得的Date再次转换为儒略历( -10天)

  • 传统的公历似乎是公历最初表示为日期(10月8日)的转换(10月8日= 4天)的结果多过朱利安历法的最大日期(10月4日)= 3天,比公历法的最小日期= 15 + 3 = 10月18日(这是一个假设)

  • 这次获得的日期似乎代表
  • 两个公历再次代表两个不同的时刻,请参见毫秒值

  • 上述传统公历的同一日期(这次是否不转换为朱利安日期?) li>
  • the proleptic Gregorian Calendar seems to represent the desired date
  • the obtained Date, again, is converted to a Julian date (-10 days)
  • the traditional Gregorian Calendar seems to be the effect of a conversion in Gregorian Calendar of a date (8 Oct) initially expressed in a proleptic Julian Calendar (8 Oct = 4 days more then Julian Calendar MAX DATE (4 Oct) = 3 days more then Gregorian Calendar MIN DATE = 15 + 3 = 18 Oct. (It is a supposition)
  • this time the obtained Date seems to represent the same date of the above traditional Gregorian Calendar (without being converted to julian date, this time?)
  • the two Gregorian Calendar again represent two different instant of time, see the millisecond values

格里高利时期


  • 多久的公历代表所需日期

  • 获得的日期表示相同日期

  • 传统公历代表所需日期

  • 获得的日期表示相同的日期

  • 两个公历表示相同的时间,相同的毫秒数

  • the proleptic Gregorian Calendar represent the desired date
  • the obtained Date represent the same date
  • the traditional Gregorian Calendar represent the desired date
  • the obtained Date represent the same date
  • the two Gregorian Calendar represents the same instant of time, same number of milliseconds

您能帮我一一解释所有这些结果吗?

Could you help me to explain one by one all of these results?

谢谢。

推荐答案

方法 newXMLGregorianCalendar (s)必须使用格式为 yyyy-MM-dd的字符串,该字符串在月份的一天中使用两位数。但是您已经定义了字符串 1582-10-4和 1582-10-5(而不是 1582-10-04和 1582-10-05)。

Method newXMLGregorianCalendar(s) must be feeded with a string in format "yyyy-MM-dd", that is using TWO digits for the day of month. But you have defined the strings "1582-10-4" and "1582-10-5" (instead of "1582-10-04" and "1582-10-05").

我已经测试了替换,并且可以正常工作。

I have tested the replacement, and it works.

请记住,XMLGregorianCalendar非常严格,并且根据ISO-8601。也就是说,它甚至在1582年之前都使用了公历leap年规则。这解释了您的陈述与日历不一致。转换为儒略日吗?。例如:

Remember that XMLGregorianCalendar is very strict and also uses the proleptic gregorian calendar according to the standard convention in ISO-8601. That means, it uses the gregorian leap year rules even before 1582. This explains your statement "Not coherent with calendar. Conversion to julian date?". For example:

教皇已于日期1582-10-04(旧儒略历)删除10天,因此在此之后立即删除了日期1582-10-15其次是新的公历。在阴历阳历中,日期1582-10-14的定义很明确,并且对应于1582-10-15的一天,因此(历史朱利安)日期1582-10-04。

The pope had removed 10 days following the date 1582-10-04 (in old julian calendar), so after this the date 1582-10-15 immediately followed in new gregorian calendar. In the proleptic gregorian calendar the date 1582-10-14 is well defined and corresponds to one day before 1582-10-15, hence the (historic julian) date 1582-10-04.

对OP的编辑后进行更新:

对您的问题由什么决定我们是否使用多义词的答案的简单答案公历?是:您决定。

The simple answer to your question "What determines whether or not we are using the proleptic gregorian calendar?" is: You decide.

详细信息::如果使用 XMLGregorianCalendar ,则使用多历公历。

In detail: If you use XMLGregorianCalendar then you use the proleptic gregorian calendar.

如果使用 java.util.GregorianCalendar ,则选择日期1582-10-15作为第一个默认为公历日。所有以前的日子都被认为是儒略历。但是,您可以覆盖默认切换日期。为此,您只需调用方法 setGregorianChange(Date),带有适当的参数。 new Date(Long.MIN_VALUE)的参数设置公历公历,而 new Date(Long.MAX_VALUE)

If you use java.util.GregorianCalendar then you have choosen the date 1582-10-15 as the first day of gregorian calendar by default. All former days are considered as being in julian calendar. You can override the default switch date however. For this to make work you just call the method setGregorianChange(Date) with an appropriate argument. An argument of new Date(Long.MIN_VALUE) sets the proleptic gregorian calendar while an argument of new Date(Long.MAX_VALUE) sets the proleptic julian calendar.

还要注意,

a)大多数国家都这样做了而不是在1582-10-15年(仅在主要的天主教国家)转换为公历。

a) most countries did not switch to gregorian calendar in 1582-10-15 (only major catholic countries),

b)历史日期还由其他因素决定,例如不同的年初(使得JDK中的完整julian-gregorian-calendar-implementation完全不足-当前没有库可以提供支持。)

b) historical dates are further determined by other factors like different start of year (which makes the whole julian-gregorian-calendar-implementation in JDK completely insufficient - currently there is no library which gives support).

在EDIT-2之后更新:

输出有益[DAY_OF_MONTH [5],MONTH [9],年[1582] 如预期的那样,并保留了多功的公历中定义的输入(1582-10-15的10天前应用了公历leap年规则向后)。

The output proleptic [DAY_OF_MONTH [5], MONTH [9], YEAR [1582] is as expected and conserves the input as defined in proleptic gregorian calendar (10 days before 1582-10-15 applying the gregorian leap year rules backwards).

输出 prolepticDate [9月25日星期二00:24:07 CET 15 82] 是一个完全不同的故事。但是,如果您记得您确实在 java.util.Date 上调用了方法 toString(),这是可以理解的。 此方法在内部实例化一个新的公历日历对象,默认开关为1582-10-15。一个 Date 对象不知道(也没有内部状态)有关设置公历更改日期的信息,因此其 toString()方法仅应用默认值。最后,只是将日历属性的日历对象重新计算/重新格式化为 Date.toString()的特定格式。即:1582-09-25(切换前十天)。

The output prolepticDate [Tue Sep 25 00:24:07 CET 1582] is a completely different story. But it is understandable if you remember that you really called the method toString() on java.util.Date. This method internally instantiates a new gregorian calendar object with the default switch of 1582-10-15. A Date-object has no idea (and no internal state) about setting the date for gregorian change, so its toString()-method just applies the default. Finally the proleptic calendar object is just recalculated/reformatted to a julian calendar representation in the specific format of Date.toString(). That is: 1582-09-25 (ten days before the switch).

由于@VGR的评论而更新:

我的测试验证了例如日期为1582-10-05的 XMLGregorianCalendar 和新的<$的特殊构造。 c $ c> GregorianCalendar 自unix-epoch以来的毫秒数相同,请参见此处:

My test verifies that for example for the proleptic date 1582-10-05 both XMLGregorianCalendar and a specialized construction of a new GregorianCalendar yield the same time in millis since unix-epoch, see here:

String s = "1582-10-05";
DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
GregorianCalendar xml = datatypeFactory.newXMLGregorianCalendar(s).toGregorianCalendar();

GregorianCalendar proleptic = new GregorianCalendar();
proleptic.clear(); // very important for proper comparison to reset time part to zero
proleptic.setGregorianChange(new Date(Long.MIN_VALUE));
proleptic.set(Calendar.DAY_OF_MONTH, 5);
proleptic.set(Calendar.MONTH, Calendar.OCTOBER);
proleptic.set(Calendar.YEAR, 1582);

boolean isEqual = (xml.getTimeInMillis() == proleptic.getTimeInMillis());

System.out.println("XML-millisSinceEpoch: " + xml.getTimeInMillis());
System.out.println("Proleptic-millisSinceEpoch: " + proleptic.getTimeInMillis());
System.out.println("XML==Proleptic (1582-10-05): " + isEqual);
System.out.println(
    "proleptic [DAY_OF_MONTH [" + proleptic.get(Calendar.DAY_OF_MONTH) + "], MONTH ["
    + proleptic.get(Calendar.MONTH) + "], YEAR [" + proleptic.get(Calendar.YEAR) + "]"
);
System.out.println("Date.toString() [" + proleptic.getTime() + "]");

输出:

XML-millisSinceEpoch: -12220160400000
Proleptic-millisSinceEpoch: -12220160400000
XML==Proleptic (1582-10-05): true
proleptic [DAY_OF_MONTH [5], MONTH [9], YEAR [1582]
Date.toString() [Tue Sep 25 00:00:00 CET 1582]

这篇关于1582年10月15日之前的日历到日期的转换。公历到儒略历的切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆