Java XMLGregorianCalendar转换为Java util.Date的错误? [英] A bug in Java XMLGregorianCalendar conversion to a Java util.Date?

查看:139
本文介绍了Java XMLGregorianCalendar转换为Java util.Date的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过将基于RAP的UI中的日期作为Java Date对象读取,并将其作为 XMLGregorianCalendar 传递,我正在将日期/时间值写入XML文件。反对实际的文件编写代码。相应的类是自动生成的,我无法控制它们。我输入的日期是:

I'm writing a date/time value to an XML file by reading the date from a RAP based UI as a Java Date object, and passing it as an XMLGregorianCalendar object to actual file writing code. The corresponding classes are auto generated and I don't have control over them. The date I entered was:


03-03-1933:03:03:03。

03-03-1933:03:03:03.

写入文件时被转换为以下字符串:

It got converted to the following string when written in the file:


1933- 03-03T03:03:03.161 + 05:53

1933-03-03T03:03:03.161+05:53

现在,当我读回日期以将其显示在UI中进行编辑时,它显示为:

Now, when I read the date back to show it in the UI for edit, it appeared there as:


03-03-1933:03:03:23

03-03-1933:03:03:23

请注意在实际秒数值上增加了20秒。

Note the extra 20 seconds added to the actual seconds value.

为什么会这样? API中有一些错误吗?任何帮助将不胜感激!

Why is this happening? Is it some bug in the API? Any help will be much appreciated!

相关代码:

1)转换为 XMLGregorianCalendar 来自 Date

GregorianCalendar calendar = new GregorianCalendar(); 
calendar.setTimeInMillis(date.getTime());
XMLGregorianCalendar date2;
date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);

// pass 'date2' to file writing code

2)转换从 XMLGregorianCalendar Date

XMLGregorianCalendar cal = getDateFromFile(); // XML date read from file
Date date = cal.toGregorianCalendar().getTime();

// show Date object in UI, dateCtrl and timeCtrl are SWT DateTime objects

GregorianCalendar calendar = new GregorianCalendar();  
calendar.setTime( date );

dateCtrl.setDate( calendar.get( GregorianCalendar.YEAR ),
    calendar.get( GregorianCalendar.MONTH ),
    calendar.get( GregorianCalendar.DAY_OF_MONTH ) );

timeCtrl.setTime( calendar.get( GregorianCalendar.HOUR_OF_DAY ),
    calendar.get( GregorianCalendar.MINUTE ),
    calendar.get( GregorianCalendar.SECOND) );


推荐答案

大约在1968年之前,存在各种奇怪的偏移量,尤其是在世界上欠发达的地区。您没有说您使用的是什么语言环境,但是如果是在印度,则曾经有一种叫 Howra Mean Time的东西有这种偏移。我不知道这是否在1933年生效。您可能需要为您的语言环境下载 tz数据库并检查配置

Prior to about 1968 there were all sorts of weird offsets, especially in less-developed parts of the world. You don't say what locale you're using, but if it's in India, there was once something called Howra Mean Time that had that offset. I don't know if it was in effect in 1933 however. You will likely have to download the tz database for your locale and check the configuration for that date.

编辑:要验证确切的情况,请尝试:

To verify exactly what is happening, try:

GregorianCalendar c = new GregorianCalendar();
TimeZone tz = c.getTimeZone();
System.out.println(tz);

int tzo = tz.getOffset(date.getTime());
System.out.println(
      tzo/3600000 + ":" + 
      (tzo/60000)%60 + ":" + 
      (tzo/1000)%60 + "." + 
      tzo%1000);

这将告诉您系统认为当前时区是什么,并且时区偏移对有问题的日期是1933年。当我在系统中运行该日期时,我会得到:

This will tell you what the system thinks the current timezone is, and the timezone offset in effect on the problematic date in 1933. When I run this in my system I get:

sun.util.calendar.ZoneInfo[id="America/Chicago",offset=-21600000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/Chicago,offset=-21600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]
-6:0:0.0

但是,如果我更改一行: / p>

HOWEVER, if I change one line:

GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone("IST"));

然后我得到:

sun.util.calendar.ZoneInfo[id="IST",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]
5:53:20.0

这篇关于Java XMLGregorianCalendar转换为Java util.Date的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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