Uploaded image for project: 'Application Server 3  4  5 and 6'
  1. Application Server 3 4 5 and 6
  2. JBAS-2774

problem using org.jboss.util.propertyeditor.DateEditor with a locale configuration different by english

XMLWordPrintable

      hi.....using getValue() method of org.jboss.util.propertyeditor.DateEditor with a locale configuration different by english, there is a java.text.ParseException and no Date returns. Below I added a patch in org.jboss.util.propertyeditor.DateEditor class to resolve this problem. I rewrote the getAsText() method so it can to returns a LOCALE String format , after that getValue() method can to parse it right. I added also a formatsForText attribute. It is similar to formats attribute but it is in a ENGLISH format so I can to create a String in a right manner in the getAsText()

      /*

      • JBoss, Home of Professional Open Source
        *
      • Distributable under LGPL license.
      • See terms of license at gnu.org.
        */
        package org.jboss.util.propertyeditor;

      import java.text.DateFormat;
      import java.text.DateFormatSymbols;
      import java.text.ParseException;
      import java.text.SimpleDateFormat;
      import java.util.Date;
      import java.util.Locale;

      import org.jboss.util.NestedRuntimeException;
      import org.jboss.util.propertyeditor.TextPropertyEditorSupport;

      /**

      • A property editor for {@link java.util.Date}

        .

      • @version <tt>$Revision: 1.1.2.2 $</tt>
      • @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
      • @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
      • @author Scott.Stark@jboss.org
        */
        public class DateEditor extends TextPropertyEditorSupport {
        static DateFormat[] formats;

      static DateFormat[] formatsForText;

      static {
      String defaultFormat = System.getProperty(
      "org.jboss.util.propertyeditor.DateEditor.format",
      "MMM d, yyyy");
      formats = new DateFormat[]

      { new SimpleDateFormat(defaultFormat), // Tue Jan 04 00:00:00 PST 2005 new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy"), // Wed, 4 Jul 2001 12:08:56 -0700 new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z") }

      ;

      /**

      • I fill formatsForText using a ENGLISH
      • format, so getAsText() method can to return a java.lang.String in a
      • LOCALE depending format
      • @author Luca Stancapiano (l.stancapiano@k-tech.it)
        */
        DateFormatSymbols dateFormatSymbols = new DateFormatSymbols(
        Locale.ENGLISH);
        formatsForText = new DateFormat[] { new SimpleDateFormat(defaultFormat, dateFormatSymbols), // Tue Jan 04 00:00:00 PST 2005 new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy", dateFormatSymbols), // Wed, 4 Jul 2001 12:08:56 -0700 new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", dateFormatSymbols) }

        ;
        }

      /**

      • I rewrite getAsText() method because
      • java.beans.PropertyEditorSupport class uses
      • toString() method of java.util.Date to return a String format
      • but toString() method always returns a ENGLISH format String.
      • If you are in an other country, you get problems to receive the
      • java.util.Date by getValue() because it's in a different LOCALE format.
      • This method returns a String format depending by your LOCALE settings
      • @author Luca Stancapiano (l.stancapiano@k-tech.it)
      • @return a String object
      • */
        public String getAsText() {
        String str = super.getAsText();
        int n = 0;
        Exception ex = null;
        do {
        try

        { DateFormat df = formats[n]; DateFormat dfForText = formatsForText[n]; Date date = dfForText.parse(str); return df.format(date); }

        catch (Exception e)

        Unknown macro: { try { DateFormat df = formats[n]; Date date = df.parse(str); return df.format(date); } catch (Exception e2) { ex = e2; } }

        n++;
        } while (n < formats.length);
        throw new NestedRuntimeException(ex);
        }

      /**

      • Returns a Date for the input object converted to a string. It tries the
      • default date format as specified by the following SimpleDateFormat
      • formats in order:
      • org.jboss.util.propertyeditor.DateEditor.format system property (or MMM
      • d, yyyy, if not specified), EEE MMM d HH:mm:ss z yyyy EEE, d MMM yyyy
      • HH:mm:ss Z
      • @return a Date object
      • */
        public Object getValue() {
        int n = 0;
        ParseException ex = null;
        do

        Unknown macro: { try { DateFormat df = formats[n]; return df.parse(getAsText()); } catch (ParseException e) { ex = e; } n++; }

        while (n < formats.length);
        throw new NestedRuntimeException(ex);
        }

      }

              dandread1@redhat.com Dimitrios Andreadis
              luca.stancapiano@vige.it Luca Stancapiano (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved:

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0 minutes
                  0m
                  Logged:
                  Time Spent - 4 hours
                  4h