Return-Path: Delivered-To: apmail-jakarta-commons-user-archive@www.apache.org Received: (qmail 91387 invoked from network); 8 Mar 2004 15:12:50 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 8 Mar 2004 15:12:50 -0000 Received: (qmail 70386 invoked by uid 500); 8 Mar 2004 15:12:42 -0000 Delivered-To: apmail-jakarta-commons-user-archive@jakarta.apache.org Received: (qmail 70032 invoked by uid 500); 8 Mar 2004 15:12:39 -0000 Mailing-List: contact commons-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Users List" Reply-To: "Jakarta Commons Users List" Delivered-To: mailing list commons-user@jakarta.apache.org Received: (qmail 70015 invoked from network); 8 Mar 2004 15:12:38 -0000 Received: from unknown (HELO mailr-1.tiscali.it) (212.123.84.81) by daedalus.apache.org with SMTP; 8 Mar 2004 15:12:38 -0000 Received: from smtp.it.tiscali.com (HELO it.tiscali.com) (195.130.225.135) by mailr-1.tiscali.it with ESMTP; 08 Mar 2004 16:12:40 +0100 Received: (qmail 10474 invoked from network); 8 Mar 2004 15:12:39 -0000 Received: from unknown (HELO tin.it) (213.205.18.38) by 195.130.225.135 with SMTP; 8 Mar 2004 15:12:37 -0000 Message-ID: <404C8D61.8030804@tin.it> Date: Mon, 08 Mar 2004 16:12:33 +0100 From: Flavio Tordini User-Agent: Mozilla Thunderbird 0.5 (Windows/20040207) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Jakarta Commons Users List Subject: Re: [beanutils] java.util.Date converter References: <40473921.4040806@tin.it> <1078425833.404778e95451a@localhost> <404845E3.9070100@tin.it> <1078513610.4048cfca5636e@localhost> In-Reply-To: Content-Type: multipart/mixed; boundary="------------050801050904020904080109" X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N --------------050801050904020904080109 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit hi, here's my ISO 8601 converter. It is not really compliant (see javadocs), but it can be a starting point. In my application LocaleConverter is not the solution because I don't want date formats to rely on the locale. I read XML files and set JavaBeans properties with BeanUtils and I need a fixed international dateformat for the users of my app. I can also make this Converter more configurable, and let the constructor accept one or more custom date patterns, this way the user can choose the date format, without having to write its own class. flavio robert burrell donkin wrote: > On 5 Mar 2004, at 19:06, Craig R. McClanahan wrote: > >> Quoting Flavio Tordini : >> >>> hi craig, >>> thank you for your answer. i think the date formats to use are those in >>> ISO 8601. >>> >>> see: http://www.w3.org/TR/NOTE-datetime >>> >>> maybe the converter could be added to the beanutils codebase but not >>> used by default by ConvertUtils. what do you think? >>> >> >> That is one of at least twenty different formats for dates and times >> that people >> like to use (and would be pretty user-unfriendly in a locale that likes >> dd/mm/yyyy formatted dates). It doesn't seem reasonable to pick just >> one, so >> I'd prefer to let applications that want to use a particular format to >> register >> their own. > > > i probably support adding an ISO 8601 compatible convertor to beanutils. > ISO 8601 is not only a standard but also one which is commonly used. > (i'd thought about creating one before but i became concerned about > copyright usage and trademark issues.) adding a convertor based on that > w3c NOTE seems very reasonable to me. in addition, the basic date > related datatypes used in (w3c flavoured) xml schema is also based on > ISO8601 and are similar to those in the NOTE so it would be useful for > processing xml documents. > > if the worry is that adding this would establish an unhelpful precedent, > then maybe we could add it as an optional class. > > - robert > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org > For additional commands, e-mail: commons-user-help@jakarta.apache.org > > --------------050801050904020904080109 Content-Type: text/plain; name="ISO8601DateConverter.java" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="ISO8601DateConverter.java" package org.apache.commons.beanutils.converters; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.beanutils.ConversionException; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.Converter; /** *

* Standard {@link Converter} implementation that converts an incoming String * into a java.util.Date object roughly according to the ISO8601 * standard, optionally using a default value or throwing a * {@link ConversionException} if a conversion error occurs. *

* *

* Supported date formats (java.text.SimpleDateFormat patterns): *

    *
  • yyyy-MM-dd'T'HH:mm:ssZ
  • *
  • yyyy-MM-dd'T'HH:mm:ss
  • *
  • yyyy-MM-dd'T'HH:mmZ
  • *
  • yyyy-MM-dd'T'HH:mm
  • *
  • yyyy-MM-dd
  • *
  • yyyy-MM
  • *
  • yyyy
  • *
*

* *

* This class makes use of java.text.SimpleDateFormat to parse strings. * As a result it is not fully compliant with ISO8601, since SimpleDateFormat * uses a slightly different time zone designator (TZD) than ISO 8601: *

 * ISO 8601: "-00:00"
 * SimpleDateFormat: "-0000"
 * 
*

* * @author Flavio Tordini * @version $Id$ * @see http://www.w3.org/TR/NOTE-datetime */ public final class ISO8601DateConverter implements Converter { // ----------------------------------------------------------- Constructors /** * Create a {@link Converter} that will throw a {@link ConversionException} * if a conversion error occurs. */ public ISO8601DateConverter() { this.defaultValue = null; this.useDefault = false; } /** * Create a {@link Converter} that will return the specified default value * if a conversion error occurs. * * @param defaultValue * The default value to be returned */ public ISO8601DateConverter(Object defaultValue) { this.defaultValue = defaultValue; this.useDefault = true; } // ----------------------------------------------------- Instance Variables /** * ISO 8601 date formats. Order is important, must be from more specific to * less specific. */ private static final String[] patterns = { "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HH:mmZ", "yyyy-MM-dd'T'HH:mm", "yyyy-MM-dd", "yyyy-MM", "yyyy",}; /** * the SimpleDateFormat object used to parse strings. */ private final SimpleDateFormat dateFormat = new SimpleDateFormat(); /** * The default value specified to our Constructor, if any. */ private Object defaultValue = null; /** * Should we return the default value on conversion errors? */ private boolean useDefault = true; // --------------------------------------------------------- Public Methods /** * Convert the specified input object into an output object of the * specified type. * * @param type * Data type to which this value should be converted * @param value * The input value to be converted * * @exception ConversionException * if conversion cannot be performed successfully */ public Object convert(Class type, Object value) { if (value == null) { if (useDefault) { return (defaultValue); } else { throw new ConversionException("No value specified"); } } if (value instanceof Date) { return (value); } else if (value instanceof java.lang.String) { for (int i = 0; i < patterns.length; i++) { try { dateFormat.applyPattern(patterns[i]); return dateFormat.parse((String) value); } catch (Exception e) { // ignore } } if (useDefault) { return (defaultValue); } else { throw new ConversionException("Invalid date format."); } } if (useDefault) { return (defaultValue); } else { throw new ConversionException("Cannot convert."); } } public static final void main(String[] args) { ConvertUtils.register(new ISO8601DateConverter(), java.util.Date.class); String myDate = "2004-08-08T04:20:13"; long startTime = System.currentTimeMillis(); Date result = null; for (int i = 0; i < 1000; i++) { result = (Date) ConvertUtils.convert(myDate, java.util.Date.class); } long processTime = System.currentTimeMillis() - startTime; System.out.println(result + " time: " + processTime); } } --------------050801050904020904080109 Content-Type: text/plain; charset=us-ascii --------------------------------------------------------------------- To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-user-help@jakarta.apache.org --------------050801050904020904080109--