commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From robert burrell donkin <robertburrelldon...@blueyonder.co.uk>
Subject Re: BeanUtils and Dates
Date Sat, 07 Dec 2002 21:26:03 GMT
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:commons-user-unsubscribe@jakarta.apache.
> org>
> For additional commands, e-mail: <mailto:commons-user-help@jakarta.apache.
> org>
>


Mime
View raw message