commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bingel, Michael" <Michael.Bin...@paybox.net>
Subject Converter for java.util.Date ?
Date Mon, 12 May 2003 13:22:42 GMT
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);
            }
        }

    }

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message