Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6436494

SimpleDateFormat cannot generate standard XML dateTime with timezone.

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 5.0
    • core-libs
    • x86
    • windows_xp

      A DESCRIPTION OF THE REQUEST :
      The standard XML formats for datetime, etc. cannot be
      implemented by a SimpleDateFormat. This is because SimpleDateFormat
      does not support generating timezones in the "+HH:MM" format, as
      required by the XML standard. It us thus impossible to create a
      standard XML value representing a datetime, such
      as "2006-12-23T14:03:02Z-08:00" without having to generate the text
      in custom-written Java code.

      See http://www.w3.org/TR/xmlschema-2/#dateTime for the XML format.

      Also see bug #4919632. This problem has existed for a long time.
      XML is not a new format! Please fix this issue!

      JUSTIFICATION :
      The implementation is trivial, and the lack of this feature needlessly complicates Java/XML interoperability.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I would like to see a new format specifier, say "Q", that generates a timezone in the format "+HH:MM" or "-HH:MM", and can also parse these formats. Then I could specify a standard XML dateTime as "yyyy-MM-dd'T'HH:mm:ss'Z'Q".
      ACTUAL -
      Timezones can be generated in formats "GMT+HH:MM", and in format "HHMM", but not "

      ---------- BEGIN SOURCE ----------
      N/A, because the feature does not exist.
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
            final SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss'Z'" );
            output.write( df.format( when ) );

            int nOffset = TimeUtils.millisToMinutes( df.getTimeZone().getOffset( when.getTime() ) );
            if( nOffset < 0 )
            {
               output.write( "-" );
               nOffset = -nOffset;
            }
            else
               output.write( "+" );

            // write hours and minutes of timezone offset
            final DecimalFormat nf = (DecimalFormat)NumberFormat.getInstance();
            nf.applyPattern( "00" );
            output.write( nf.format( nOffset / 60 ) );
            output.write( ":" );
            output.write( nf.format( nOffset % 60 ) );

            okutsu Masayoshi Okutsu
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: