You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by John Moore <jo...@jmsd.co.uk> on 2005/04/28 13:26:49 UTC
[BeanUtils]Date-String conversion question
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