XStream
  1. XStream
  2. XSTR-711

DateConverter uses an ambiguous default date format pattern

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.4.2
    • Fix Version/s: 1.4.4
    • Component/s: Converters
    • Labels:
      None
    • JDK version and platform:
      1.6.0_26

      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

        People

        • Assignee:
          Jörg Schaible
          Reporter:
          Erwin Vervaet
        • Votes:
          0 Vote for this issue
          Watchers:
          2 Start watching this issue

          Dates

          • Created:
            Updated:
            Resolved: