I'm posting this with a different subject line so it will hopefully
come up in the archives if someone needs to search for it. I posted a
solution a while ago and since that time several have asked about it
but searching through the archives myself doesn't bring it up the
post, so here's a re-write:

Several have asked about using BeanUtils.copyProperties when you are
dealing with Dates. The default copyProperties works fine if you are
uisng java.sql.Date but not if you want to use java.util.Date.

The solution appears to create two custom converters that you can
reuse throughout your application. Two converters are created which I
named in this case DateBeanUtilsConverter and
StringBeanUtilsConverter. Code for them is at the end of this e-mail.
After you create them just add a static block at the top of your
Action class:

static {
        DateBeanUtilsConverter dateConverter = new DateBeanUtilsConverter();
        dateConverter.setFormatPattern( "MMddyyyy" );
        StringBeanUtilsConverterDate myStringConverter = new 
StringBeanUtilsConverterDate();
        myStringConverter.setFormatPattern( "MMddyyyy" );
        ConvertUtils.register( dateConverter, java.util.Date.class );
        ConvertUtils.register( myStringConverter, String.class ); 
    }
    
Notice you can add any kind of SimpleDateFormat pattern you want. Now
you just use BeanUtils.copyProperties( valueObject, formBean ) as
usual and all should be well.

Classes below:

//DateBeanUtilsConverter

import org.apache.commons.beanutils.Converter;
import java.text.*;
import java.util.*;

public class DateBeanUtilsConverter implements Converter {

    private String formatPattern = null;

    public void setFormatPattern(String formatPattern) {
        this.formatPattern = formatPattern;
    }

    public Object convert(Class type, Object value) {
        Date date = null;

        if (value != null
            && (value instanceof String)
            && (type == Date.class)) {
            try {

                String s = value.toString();
                SimpleDateFormat formatter =
                    new SimpleDateFormat(formatPattern);
                date = formatter.parse(s);

            } catch (Exception e) {
                //log error
            }
        }
        return date;
    }

}

//StringBeanUtilsConverterDate
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.converters.*;
import java.text.*;
import java.util.*;

public class StringBeanUtilsConverterDate implements Converter {
    private static final StringConverter stringConverter =
        new StringConverter();
    private String formatPattern = null;

    public void setFormatPattern(String formatPattern) {
        this.formatPattern = formatPattern;
    }

    public Object convert(Class type, Object value) {
        Object returnValue = null;

        if (value != null) {
            if (type == String.class && (value instanceof Date)) {
                SimpleDateFormat formatter =
                    new SimpleDateFormat(formatPattern);
                String dateString = formatter.format(value);
                returnValue = dateString;
            } else {
                returnValue = stringConverter.convert(type, value);
            }
        }
        return returnValue;
    }
}



-- 

Rick
mailto:maillist@;reumann.net


--
To unsubscribe, e-mail:   <mailto:struts-user-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:struts-user-help@;jakarta.apache.org>

Reply via email to