java XMLGregorianCalendar 到字符串转换错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13470880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XMLGregorianCalendar to String conversion error
提问by user1063226
The following code snippet works perfectly for dates with year above 1600
or so. But it gives wrong output for years like 1001, 1500, 1400
etc. Can you explain why?
以下代码段非常适用于年份以上的日期1600
。但它多年来一直给出错误的输出1001, 1500, 1400
等等。你能解释一下为什么吗?
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
XMLGregorianCalendar cal DatatypeFactory.newInstance().newXMLGregorianCalendar("1600-01-01");
String birthDate = format.format(cal.toGregorianCalendar().getTime());
System.out.println(birthDate);
回答by maloney
Im pretty sure the Gregorian Calender started in the year 1582 which would explain why it works for 1600 but not for 1500.
我很确定公历日历始于 1582 年,这可以解释为什么它适用于 1600 而不适用于 1500。
回答by Kurrija
The reason for the DateFormat displaying the 'wrong' year after converting the XMLGregorianCalendar to a GregorianCalendaris the fact that GregorianCalendar.getGregorianChange() defaults to 1582-10-15.
将 XMLGregorianCalendar 转换为GregorianCalendar后 DateFormat 显示“错误”年份的原因是 GregorianCalendar.getGregorianChange() 默认为 1582-10-15。
If the GregorianCalendar is set to a date before the Gregorian Calendar change date, it will actually be representing dates in the Julian calendar, which results in the year appearing to be 'wrong', because the length of a year is different for these two calendars.
如果 GregorianCalendar 设置为 Gregorian Calendar 更改日期之前的日期,它实际上将代表儒略历中的日期,这会导致年份看起来“错误”,因为这两个日历的年份长度不同.
A fix for this could be using GregorianCalendar.setGregorianChange() to set this value to an earlier date in order to avoid it changing to the Julian calendar.
对此的修复可能是使用 GregorianCalendar.setGregorianChange() 将此值设置为较早的日期,以避免它更改为儒略历。
回答by user2014545
I'm not pretty sure. But I think that it refers to Epoch (date), but... epoch starts in 1970 (not 1960).
我不太确定。但我认为它指的是 Epoch(日期),但是...... epoch 始于 1970 年(不是 1960 年)。
Also Gregorian Calendar has some limitations (I can't mention each one, but..), for example, You can't use a date from 'zero' year, this can not interpreted correctly (I think Date is out of Anno Domini, and instead you should refer to Before Christ... I'm not sure).
此外,公历也有一些限制(我不能一一提及,但是..),例如,您不能使用“零”年的日期,这无法正确解释(我认为日期超出了 Anno Domini ,而你应该参考在基督之前......我不确定)。
However... You should look around for "epoch". And I hope this could be useful to find the right answer.
但是......你应该四处寻找“纪元”。我希望这有助于找到正确的答案。