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 <[EMAIL PROTECTED]>:
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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
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; /** * <p> * Standard [EMAIL PROTECTED] Converter} implementation that converts an incoming String * into a <code>java.util.Date</code> object roughly according to the ISO8601 * standard, optionally using a default value or throwing a * [EMAIL PROTECTED] ConversionException} if a conversion error occurs. * </p> * * <p> * Supported date formats (<code>java.text.SimpleDateFormat</code> patterns): * <ul> * <li>yyyy-MM-dd'T'HH:mm:ssZ</li> * <li>yyyy-MM-dd'T'HH:mm:ss</li> * <li>yyyy-MM-dd'T'HH:mmZ</li> * <li>yyyy-MM-dd'T'HH:mm</li> * <li>yyyy-MM-dd</li> * <li>yyyy-MM</li> * <li>yyyy</li> * </ul> * </p> * * <p> * This class makes use of <code>java.text.SimpleDateFormat</code> 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: * <pre> * ISO 8601: "-00:00" * SimpleDateFormat: "-0000" * </pre> * </p> * * @author Flavio Tordini * @version $Id$ * @see <a href="http://www.w3.org/TR/NOTE-datetime">http://www.w3.org/TR/NOTE-datetime</a> */ public final class ISO8601DateConverter implements Converter { // ----------------------------------------------------------- Constructors /** * Create a [EMAIL PROTECTED] Converter} that will throw a [EMAIL PROTECTED] ConversionException} * if a conversion error occurs. */ public ISO8601DateConverter() { this.defaultValue = null; this.useDefault = false; } /** * Create a [EMAIL PROTECTED] 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 <code>SimpleDateFormat</code> 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); } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
