java - BeanUtills Date conversion Issue -
i tried copy data(java.util.date) value source target using beanutills. gives date string conversion exception.
what solution kind of issue?
my implementation follows..
import java.util.date; public class bean1 { private date date; public bean1() { } public date getdate() { return date; } public void setdate(date date) { this.date = date; } } ===========================================================
import java.util.date; public class bean2 { private date date; public bean2() { } public date getdate() { return date; } public void setdate(date date) { this.date = date; } }
===========================================================
my copy property method follows
public static void copyproperties(object src, object dest) throws llegalaccessexception,invocationtargetexception, nosuchmethodexception { field[] attributes = dest.getclass().getdeclaredfields(); (field property : attributes) { beanutils.setproperty(dest, property.getname(), beanutils.getproperty( src, property.getname())); } }
the latest version of beanutils not support direct copy of date properties. you'd need implement converter (also part of benutils package) , use converter copy properties method. avoid errors resulting in differences in format of date properties in 2 objects. following work you
public static void copyproperties(object arg0, object arg1) throws illegalaccessexception, invocationtargetexception { java.util.date defaultvalue = null; converter converter = new dateconverter(defaultvalue); beanutilsbean beanutilsbean = beanutilsbean.getinstance(); beanutilsbean.getconvertutils().register(converter, java.util.date.class); beanutilsbean.copyproperties(arg0, arg1); } i suggest using propertyutils if sure date format in both of objects remain same. need use converters if there chance date format of date properties on src , destination may different.
Comments
Post a Comment