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:
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:
HOWEVER, if I change one line:
I then get: