【问题标题】:BeanUtills Date conversion IssueBeanUtills 日期转换问题
【发布时间】:2013-05-15 09:22:50
【问题描述】:

我尝试使用 BeanUtills 将 Data(java.util.Date) 值从源复制到目标。它给出了日期到字符串的转换异常。

这类问题的解决方案是什么?

我的实现如下..

 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;
}

}

================================================ =============

我的复制属性方法如下

    public static void copyProperties(Object src, Object dest) throws   llegalAccessException,InvocationTargetException, NoSuchMethodException {
         Field[] attributes = dest.getClass().getDeclaredFields();
         for (Field property : attributes) {              
           BeanUtils.setProperty(dest, property.getName(), BeanUtils.getProperty(
                src, property.getName()));
    }
}

【问题讨论】:

    标签: java type-conversion


    【解决方案1】:

    最新版本的 BeanUtils 不支持直接复制 Date 属性。您需要实现一个转换器(也是 benutils 包的一部分)并将该转换器与您的复制属性方法一起使用。这是为了避免任何错误导致两个对象中日期属性格式的任何差异。像下面这样的东西对你有用

        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);
        }
    

    如果您确定两个对象中的日期格式保持不变,我建议您使用 PropertyUtils。只有当您的 src 和目标上的 Date 属性的 Date 格式可能不同时,您才需要使用转换器。

    【讨论】:

    • 现在我没有得到以前的异常......但是我在 Bean1 中的日期值是 2013 年 5 月 15 日星期三 15:46:04 IST。我试图将该值复制到 Bean2。我在 Bean2 中的日期为空。
    • 正如我所说,最好使用 PropertyUtils,除非这两个对象中的日期格式不同。例如,当您在一个对象中将日期作为字符串而在另一个对象中作为日期时,这很有用,在这种情况下,您可以轻松应用转换器。在你的情况下,你不需要通过这个 hessel。无论如何,你能发布你最新的代码吗,我会看看有什么问题。
    【解决方案2】:

    可以简单地使用反射来执行复制。 Field 类有 getset 方法,可以很容易地应用在这个场景中。

    public static void main(String[] args) throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException {
        Bean1 bean1 = new Bean1();
        bean1.setDate(new Date());
    
        Bean2 bean2 = new Bean2();
        System.out.println(bean2.getDate());
    
        copyProperties(bean1, bean2);
    
        System.out.println(bean2.getDate());    
    }
    
    public static void copyProperties(Object src, Object dest) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
        Field[] attributes = dest.getClass().getDeclaredFields();
        for (Field property : attributes) {
            boolean isPrivate = false;
            if(!property.isAccessible()){
                property.setAccessible(true);
                isPrivate = true;
            }
            Field srcField = src.getClass().getDeclaredField(property.getName());
            property.set(dest, srcField.get(src));
    
            if(isPrivate){
                property.setAccessible(false);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多