使用BeanUtils.copyProperties时进行时区转换

时间:2017-06-01 15:12:49

标签: java hibernate timezone date-conversion apache-commons-beanutils

我有一个Hibernate映射的模型类,如下所示,

@Entity
@Table(name = "USER")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "User_SEQ")
    @SequenceGenerator(sequenceName = "User_SEQ", allocationSize = 1, name = "User_SEQ")
    private Integer id;

    private Date createdOn;
}

我有另一个模型类,如下所示,我用它来自REST API的响应。该类的对象基本上被序列化为JSON

public class UserDTO {
    private Integer id;

    private Date createdOn;
}

由于变量名相同,我使用BeanUtils.copyProperties将所有字段的值从User复制到UserDTO(为简单起见,目前仅显示2个字段)

数据存储在数据库中的GMT中,因此createdOn字段包含GMT中的TIMESTAMP。我想在本地时区返回日期,例如"欧/哥本哈根&#34 ;.为此,我使用以下代码(使用apache commons),

java.util.Date defaultValue = null;
DateConverter dateConverter = new DateConverter(defaultValue);
dateConverter.setTimeZone(TimeZone.getTimeZone("Europe/Copenhagen"));
BeanUtilsBean beanUtilsBean = BeanUtilsBean.getInstance();
beanUtilsBean.getConvertUtils().register(dateConverter, java.util.Date.class);
BeanUtils.copyProperties(userDTO, user);

代码执行,但userDTO仍包含GMT中的日期。时区转换无效。我在这里缺少什么?

EDIT1:

我尝试通过手动传递日期进行检查,如下所示,

@Test
public void testBeanUtilsDateConverter() throws Exception {
    System.out.println("\ntestBeanUtilsDateConverter - Start");
    Date date = new GregorianCalendar(2014, Calendar.FEBRUARY, 11, 18, 30, 10).getTime();
    Date newDate = new Date();

    DateConverter converter = new DateConverter();
    converter.setTimeZone(TimeZone.getTimeZone("Europe/Copenhagen"));

    BeanUtilsBean beanUtilsBean = BeanUtilsBean.getInstance();
    beanUtilsBean.getConvertUtils().deregister(java.util.Date.class);
    beanUtilsBean.getConvertUtils().register(converter, java.util.Date.class);

    System.out.println(newDate);
    BeanUtils.copyProperties(newDate, date);
    System.out.println(date);
    System.out.println(newDate);

    System.out.println("testBeanUtilsDateConverter - End");
}

输出:

testBeanUtilsDateConverter - Start
Fri Jun 02 14:04:08 IST 2017
Tue Feb 11 18:30:10 IST 2014
Tue Feb 11 18:30:10 IST 2014
testBeanUtilsDateConverter - End

所以BeanUtils肯定会将日期复制到目标,但转换器要么没有被调用,要么因为某些原因没有做任何事情

EDIT2 - 使用自定义转换器

class MyDateConverter implements Converter {

    public MyDateConverter() {
        System.out.println("Instantiating MyDateConverter");
    }

    @Override
    public Object convert(Class Date, Object value) {
        System.out.println("Inside convert()");
        if (value == null) {
            System.out.println("Value is null");
            return (Date) null;
        } else {
            System.out.println("Doing date conversion");
            Date date = new GregorianCalendar(2015, Calendar.JANUARY, 12, 9, 45, 15).getTime();
            return date;
        }
    }
}

@Test
public void testCustomDateConverter() throws Exception {
    System.out.println("\ntestCustomDateConverter - Start");
    Date date = new GregorianCalendar(2014, Calendar.FEBRUARY, 11, 18, 30, 10).getTime();
    Date newDate = new Date();

    BeanUtilsBean beanUtilsBean = BeanUtilsBean.getInstance();
    beanUtilsBean.getConvertUtils().deregister(java.util.Date.class);
    beanUtilsBean.getConvertUtils().register(new MyDateConverter(), java.util.Date.class);

    System.out.println(newDate);
    BeanUtils.copyProperties(newDate, date);
    System.out.println(date);
    System.out.println(newDate);

    System.out.println("testCustomDateConverter - End");
}

输出:

testCustomDateConverter - Start
Instantiating MyDateConverter
Fri Jun 02 14:04:09 IST 2017
Tue Feb 11 18:30:10 IST 2014
Tue Feb 11 18:30:10 IST 2014
testCustomDateConverter - End

同样的结果。自定义转换器正在实例化,但不会调用convert方法。我甚至尝试了一个自定义Long转换器来检查它是否与使用Date类有关,但即使这样也没有被调用。注册部分缺少BeanUtils无法调用重写转换函数的内容。

0 个答案:

没有答案