You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by "Bingel, Michael" <Mi...@paybox.net> on 2003/05/12 15:22:42 UTC

Converter for java.util.Date ?

Hi folks,

I am currently working on a STRUTS customer care application and 
have some trouble with displaying dates. 

I figured out how to use a Converter for the parsing, which I 
registered as

  ConvertUtils.register (new DateConverter(null), java.util.Date.class);

but how do I format the output to my preferred format ? The
output is always displayed in extended format which I can not
parse back into the Date :-(

And does the BeanUtils.copyProperties() use the converter ?


Hope to get some useful input...

cheers   Mike





The important part of my current String2Date Converter:


    /**
     * The default format specified to our Constructor, if any.
     */
    private String[] defaultFormat = {"yyyy-MM-dd HH:mm:ss", "dd.MM.yyyy
HH:mm:ss", "yyyy-MM-dd", "dd.MM.yyyy", "MM/dd/yyyy"};


    /**
     * 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");
            }
        }

// start
        if ((value instanceof String) && (type == Date.class)) { // String
-> Date

            if (value == null || ((String)value).length() == 0) return null;
            for (int i = 0; i < defaultFormat.length; i++) {

                try {
                    SimpleDateFormat formatter = new
SimpleDateFormat(defaultFormat[i]);
                    return formatter.parse((String)value);
                } catch (ParseException pe) {
                    // if none of the conversion patterns worked
                    if (i == defaultFormat.length - 1) {
                        throw new ConversionException("Could not convert " +
(String)value + " to date: " + pe);
                    }
                }
            }

        } else if ((value instanceof Date) && (type == String.class)) { //
Date -> String

            SimpleDateFormat formatter = new
SimpleDateFormat(defaultFormat[0]);
            return formatter.format(value);

        } else if (type == String.class) {

            StringConverter sc = new StringConverter();
            return sc.convert(type, value);

        }
// end

        if (value instanceof Date) {
            return (value);
        }

        try {

            SimpleDateFormat formatter = new
SimpleDateFormat(defaultFormat[0]);
            return formatter.parse(value.toString());

        } catch (Exception e) {
            if (useDefault) {
                return (defaultValue);
            } else {
                throw new ConversionException(e);
            }
        }

    }