robert:

Thanks for the assistance.  As it happens storing the value as a Timestamp
suits my purposes and it works with "off the shelf" BeanUtils.  However I'm
sure your solution will come in handy with other nonstandard data I need to
deal with.

Greg


-----Original Message-----
From: robert burrell donkin
[mailto:[EMAIL PROTECTED]]
Sent: Saturday, December 07, 2002 3:26 PM
To: Jakarta Commons Users List
Subject: Re: BeanUtils and Dates


hi greg

the standard SqlDateConvertor in beanutils uses java.sql.Date.valueOf to
perform the conversion. this method requires a date in sql date format ie
yyyy-mm-dd. i don't think that the conversion will work given an input in
your format.

there is an easy way to make this work in beanutils. you can create a
custom converter and then register it to handle your java.sql.date's.

i'd create a custom convertor by extending SqlDateConverter and overriding
convert with something like

<untested-code-warning>

public Object convert(Class type, Object value) {

        if (value == null) {
                return super.convert(type, value);
        }

        return super.convert(type, value.toString().substring(0, 10));

}

</untested-code-warning>

see java docs for explanation of how to register a custom converter.

- robert

On Friday, December 6, 2002, at 06:32 PM, Greg Dunn wrote:

> I'm using BeanUtils to populate a bean with records from a resultSet via
> a
> HashMap built using the metadata.getColumnName for property names and the
> rst.getString for values in the usual way.  A date field in the resultSet
> (we'll call it "myDate") is causing an exception.
>
> Debug log shows ConvertUtils is attempting to "Convert string '2002-09-25
> 01:05:43.0' to class 'java.sql.Date'" but it cannot and throws a
> ConversionException.
>
> My bean imports java.sql.Date, and the instance variable is:
>
>       private Date myDate = null;
>
> My setter is:
>
>       public void setMyDate(Date myDate) {
>               this.myDate = myDate;
>       }
>
> How do I get the date into my bean as a date?  Am I stuck with setting it
> as
> a String?
>
> Greg
>
>
> --
> To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED].
> org>
> For additional commands, e-mail: <mailto:[EMAIL PROTECTED].
> org>
>


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




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

Reply via email to