You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Chris Helck <Ch...@us.icap.com> on 2007/10/05 18:19:54 UTC

Beanutils: Problem with DateConverter

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 

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



**********************************************************************
This communication and all information (including, but not limited to,
 market prices/levels and data) contained therein (the "Information") is
 for informational purposes only, is confidential, may be legally
 privileged and is the intellectual property of ICAP plc and its affiliates
 ("ICAP") or third parties. No confidentiality or privilege is waived or
 lost by any mistransmission. The Information is not, and should not
 be construed as, an offer, bid or solicitation in relation to any
 financial instrument or as an official confirmation of any transaction.
 The Information is not warranted, including, but not limited, as to
 completeness, timeliness or accuracy and is subject to change
 without notice. ICAP assumes no liability for use or misuse of the
 Information. All representations and warranties are expressly
 disclaimed. The Information does not necessarily reflect the views of
 ICAP. Access to the Information by anyone else other than the
 recipient is unauthorized and any disclosure, copying, distribution or
 any action taken or omitted to be taken in reliance on it is prohibited. If
 you receive this message in error, please immediately delete it and all
 copies of it from your system, destroy any hard copies of it and
 notify the sender.
**********************************************************************


Re: Beanutils: Problem with DateConverter

Posted by Niall Pemberton <ni...@gmail.com>.
I replied to this on the dev@ list - please don't cross-post, thanks

http://tinyurl.com/3y2bqm

Niall

On 10/5/07, Chris Helck <Ch...@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
>
> 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: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: Beanutils: Problem with DateConverter

Posted by Niall Pemberton <ni...@gmail.com>.
On 10/5/07, Chris Helck <Ch...@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