我有2个班级
public class A
{
public java.sql.Time startAt;
}
public class B
{
public int startAt;
}
如果我尝试映射它,我会收到错误,它无法将Time转换为Integer(自1970年1月1日起的毫秒数)。阅读我需要定义客户转换器的文档。我的问题是
答案 0 :(得分:0)
你创建一个这样的自定义类:
import java.util.Date;
import org.dozer.DozerConverter;
import org.joda.time.DateTime;
public class JodaTimeToDateConverter extends DozerConverter<Date, DateTime> {
public JodaTimeToDateConverter() {
super(Date.class, DateTime.class);
}
@Override
public DateTime convertTo(Date source, DateTime destination) {
DateTime result = null;
if(source != null) {
result = new DateTime(source.getTime());
}
return result;
}
@Override
public Date convertFrom(DateTime source, Date destination) {
Date result = null;
if(source != null) {
result = new Date(source.getMillis());
}
return result;
}
}
然后,您将一个configure
块添加到dozer xml:
<configuration>
<custom-converters>
<converter type="foo.bar.CustomConverterClass">
<class-a>java.util.Date</class-a>
<class-b>org.joda.time.DateTime</class-b>
</converter>
</configuration>