|
Description:
|
The DateConverter class uses the "yyyy-MM-dd HH:mm:ss.S z" pattern by default. This pattern is ambiguous since it does not encode the era. For instance, consider the following:
XStream xs = new XStream();
Date input = new Date(-62135773200000L); // 01/01/0001T00:00:00.000+01:00 AD
Date output = (Date) xs.fromXML(xs.toXML(input));
if (input.getTime() != output.getTime()) {
System.out.println(input + " != " + output);
}
The output will indicate that the year is 2 instead of 1. To understand why this happens, remember that the Gregorian calendar has no year zero, so the timeline is:
..., 2 BC, 1 BC, AD 1, AD 2, ...
(see http://stackoverflow.com/questions/8440480/year-0000-in-java for more info)
In more technical terms this means that the date "01/01/0001T00:00:00.000+01:00 AD" becomes "0001-12-31 23:00:00.0 UTC" in the XML, without the era indicator!
As a result, when parsing "0001-12-31 23:00:00.0 UTC" again, the year 1 is incorrectly assumed to be AD, so the resulting date is "01/01/0002T00:00:00.000+01:00 AD".
The solution would be in add the era to the date format pattern ('G'): "yyyy-MM-dd HH:mm:ss.S z G":
XStream xs = new XStream();
xs.registerConverter(new DateConverter("yyyy-MM-dd HH:mm:ss.S z G", new String[0]));
Date input = new Date(-62135773200000L); // 01/01/0001T00:00:00.000+01:00 AD
Date output = (Date) xs.fromXML(xs.toXML(input));
if (input.getTime() != output.getTime()) {
System.out.println(input + " != " + output);
}
}
See also XSTR-556
|