commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From John Moore <j...@jmsd.co.uk>
Subject [BeanUtils]Date-String conversion question
Date Thu, 28 Apr 2005 11:26:49 GMT
I am using BeanUtils.copyProperties in a Struts based app to copy data 
both ways between my form beans and my business objects. Originally I 
was finding that it was tripping up on copying data to a Date field from 
a String in the form bean. I was getting an argument type mismatch 
error, which surprised me because I thought copyProperties could handle 
String <->Date conversions out of the box. Anyway, I got around it by 
registering a DateLocaleConverter, in a method such as this

    protected BusinessObject convertFormToObject(ActionForm form, 
BusinessObject bo) throws Exception{
        String pattern = "yyyy-MM-dd HH:mm:ss";
        Locale locale = Locale.getDefault();
        DateLocaleConverter converter = new 
DateLocaleConverter(locale,pattern);
        converter.setLenient(true);
        ConvertUtils.register(converter, java.util.Date.class);
        BeanUtils.copyProperties(bo,form);
        return bo;
    }

What I'd like to do now is to have the same thing working the other way 
as well. In the following method, I was hoping that the Strings 
resulting from the conversion would follow the prescribed pattern, but 
they don't, they default to the 'Thu Apr 28 12:26:15 BST 2005' format. 
How do I do this?

    protected ActionForm convertObjectToForm(BusinessObject 
bo,ActionForm form) throws Exception{
        String pattern = "yyyy-MM-dd HH:mm:ss";
        Locale locale = Locale.getDefault();
        DateLocaleConverter converter = new 
DateLocaleConverter(locale,pattern);
        converter.setLenient(true);
        ConvertUtils.register(converter, java.util.Date.class);
        BeanUtils.copyProperties(form,bo);
        return form;
    }


J
-- 
==============================================
John Moore  -  Norwich, UK  -  john@jmsd.co.uk
==============================================

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


Mime
View raw message