Writing a date converter is pretty simple; the only reason there isn't one included in the beanutils distribution is that there's not a very easy way to configure the date format in a universal way.

Below is an example. This one is implemented as a static inner class within the only class which uses it, but you could do it differently:

static class SimpleDateFormatDateConverter implements org.apache.commons.beanutils.Converter
{


        String dateFormat = null;

        SimpleDateFormatDateConverter(String dateFormat)
        {
            this.dateFormat = dateFormat;
        }

        public Object convert(Class clazz, Object obj)
        {

if (obj == null) return null;
if (obj instanceof java.util.Date) return obj;
String toParse = obj.toString();
if (toParse.trim().length() == 0) return null;
java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat(dateFormat);
try
{
return fmt.parse(obj.toString());
}
catch (Exception ex)
{
throw new org.apache.commons.beanutils.ConversionException(ex);
}



} }


Then, elsewhere in your Java code, you would register the converter doing something like this:
String dateFormat = "yyyy-MM-dd";
ConvertUtils.register(new SimpleDateFormatDateConverter(dateFormat), java.util.Date.class);


Since this code was written, beanutils has made good progress away from using static methods for everything, which makes it easier to have Converters in different parts of your app that behave differently. See http://jakarta.apache.org/commons/beanutils/api/index.html and more specifically http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils/BeanUtilsBean.html#BeanUtilsBean(org.apache.commons.beanutils.ConvertUtilsBean,%20org.apache.commons.beanutils.PropertyUtilsBean) for the doc details.

Joe


At 12:13 PM -0300 11/5/04, Gabriel França Campolina wrote:
Hi,

I'd like know how I can use BeanUtils.copyProperties(form, model),
when I my objects have name equals but types noequals, example, in my
form I have a attribuite named "dateInsert" of type string, but in my
model I have this attribuite of the type java.util.Date. How I
specified to BeanUtils this type of conversion. I read some articles
in internet saw that I can implements a inteface converter, but i
don't know use. SomeBody can hope me, with link or example use this
classe???

Thanks folks,

--------------------------------------------------------------------------
Gabriel França Campolina
Sun Certified Programmer for the Java 2 Plataform

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--
Joe Germuska
[EMAIL PROTECTED]
http://blog.germuska.com
"In fact, when I die, if I don't hear 'A Love Supreme,' I'll turn back; I'll know I'm in the wrong place."
- Carlos Santana

Reply via email to