Problem with DataFormat and XML Serialise/Deserialise

From: Adrian Wiesmann (awiesman..omap.org)
Date: Tue May 22 2007 - 13:58:29 EDT

  • Next message: Adrian Wiesmann (JIRA): "[JIRA] Created: (CAY-792) Date has issues with different locales when using XMLEncoder/XMLDecoder"

    Hello

    I guess I found a problem within the XML Serialisation/Deserialisation
    code when working with Dates. But before posting a patch or bug in JIRA I
    thought I'd make sure that I really found a problem and am not hunting
    ghosts (the wrong ones :) ).

    The scenario is the following:

    Serialising a list of objects with the XMLEncoder:

    > xmlEncoder.encode(myId, dataObjectList);

    results in the date fields to be formatted like this:

    "Tue Mar 13 20:56:11 CET 2007"

    which basically represents this format:

    "EEE MMM dd HH:mm:ss zzz yyyy"

    That works like a charm. The resulting XML is well formatted.

    The problem is then, when you try to read back such a deserialised list
    with the help of the XMLDecoder into a DataContext:

    > XMLDecoder.decodeList(buffer, dataContext);

    Sometimes this resulted in our application in the following error:

    > Unparseable date: "Tue Mar 13 20:56:11 CET 2007"

    and first we thought this is an error with Windows because the error only
    happened on that system. But after doing some testing I think we have
    a locale problem:

    The XMLDecoder decodes Dates like this:

    > formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
    > formatter.parse("Tue Mar 13 20:56:11 CET 2007");

    The problem is, that the SimpleDateFormat seems to use the locale of the
    respective system, although we define the mask it should use in its
    constructor! I tried to use non lenient functionality:

    > formatter.setLenient(false);

    but that didn't work as well. Which basically means the XMLDecoder and
    XMLEncoder are not supporting different locales.

    Can somebody confirm this behaviour? (And if yes, shouldn't we change the
    format of the date/time as well as the formatting within the Decoder and
    Encoder?)

    Regards,
    Adrian

    P.S: This is some snippet to test the problem:

    String strDate = "Tue Mar 13 20:56:11 CET 2007";
    String strFormat = "EEE MMM dd HH:mm:ss zzz yyyy";
    DateFormat formatter = new SimpleDateFormat(strFormat, Locale.CHINA);
            

    try {
              System.out.println(formatter.parse(strDate));
    } catch (ParseException e) {
      e.printStackTrace();
    }

    formatter = new SimpleDateFormat(strFormat, Locale.CHINA);
    formatter.setLenient(false);
    try {
              System.out.println(formatter.parse(strDate));
    } catch (ParseException e) {
      e.printStackTrace();
    }

    formatter = new SimpleDateFormat(strFormat, Locale.US);
    try {
      System.out.println(formatter.parse(strDate));
    } catch (ParseException e) {
      e.printStackTrace();
    }



    This archive was generated by hypermail 2.0.0 : Tue May 22 2007 - 13:59:14 EDT