开发者

A bug in Java XMLGregorianCalendar conversion to a Java util.Date?

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.

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

1933-03开发者_开发知识库-03T03:03:03.161+05:53

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

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

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

Relevant code:

1) Converting to XMLGregorianCalendarfrom Date:

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

// pass 'date2' to file writing code

2) Converting to Date from XMLGregorianCalendar:

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


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.

EDIT: 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);

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

HOWEVER, if I change one line:

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

I then get:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜