Frage

I am using commons-beanutils 1.8.3 and and Date properties as java.util.Date.

When i am trying to copy Source to destination it's unable to copy date properties.

Following shows my previous issue but no one give the correct answer for me.

as a example my date value in source bean is Wed May 15 15:46:04 IST 2013, but BeanUtils unable to copy this to destination...

BeanUtills Date conversion Issue

War es hilfreich?

Lösung

This is the way I was able to solve the issue.

    public static void copyProperties(Object src, Object dest) throws IllegalAccessException,
        InvocationTargetException, NoSuchMethodException {

    java.util.Date defaultValue = null;
    Converter converter = new DateConverter(defaultValue);
    BeanUtilsBean beanUtilsBean = BeanUtilsBean.getInstance();
    beanUtilsBean.getConvertUtils().register(converter, java.util.Date.class);
    BeanUtils.copyProperties(dest, src);


}

Andere Tipps

In your case there is no need to use string conversion since src and dest properties have the same type. This will work fine

    for (Field property : attributes) {
        property.set(dest, property.get(src));
    }

Solved the problem with cloneBean function, like:

ClassType newClazz = null;
newClazz = (ClassType) BeanUtils.cloneBean(oldClazz);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top