commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Niall Pemberton" <niall.pember...@gmail.com>
Subject Re: Beanutils: Problem with DateConverter
Date Mon, 08 Oct 2007 17:03:07 GMT
On 10/5/07, Chris Helck <Chris.Helck@us.icap.com> wrote:
> Hi,
>
> I'm trying to use the DateConverter, and it just doesn't seem to work
> (or I don't understand it). Here's my code:
>
>         DateConverter converter = new DateConverter();
>         converter.setPattern("MM/dd/yy");
>         ConvertUtils.register(converter, java.util.Date.class);
>
>         System.out.println(ConvertUtils.convert(new Date()));
>
> The output is: Fri Oct 05 12:22:34 EDT 2007
> I expected to see: 10/5/07

The ConvertUtilsBean's convert(Object) method delegates conversion to
String to the converter registered for String.class (rather than the
date converter in your case). This is the old behaviour of BeanUtils
and the convert(Object) method continues to work that way for
compatibility reasons. A new convert(Object, Class) method has been
added in BeanUtils 1.8.0 which delegates conversion to String to the
converter registered for the input type (date in your case) - so
changing your code to the following should produce the result you
expected:

System.out.println(ConvertUtils.convert(new Date(), String.class));

Alternatively if you want to use the old convert(Object) method - but
have it use the new mechanism when deciding which Converter impl to
delegate to for String conversions then you can configure to use the
BeanUtislBean2/ConvertUtilsBean2 instances in the following way:

BeanUtilsBean.setInstance(new BeanUtilsBean2());

If you execute the above before running your code then it should also
produce the result you expect.

Niall


> I'm using version 1.8.0-BETA that I rebuilt from the source. I'm aware
> of the bug BEANUTILS-283, but I think I've got the fix.
>
> Regards,
> Christopher Helck
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message