我正在使用 PostgreSQL 数据库,时间戳列具有 java.sql.Timestamp 类。即使此类扩展了 java.util.Date,当我编辑 PopupDateFiels 时,我也会收到错误
无法将 java.util.Date 类型的值转换为模型类型类 java.sql.Timestamp。未设置转换器且类型不兼容。
默认转换器工厂无法正常工作。我试着写
dateField.setConverter(new DateToSqlDateConverter());
or
dateField.setConverter(StringToDateConverter.class);
结果相同。
通过单击日历中的某一天,我可以看到欧洲格式的有效日期和时间“23.10.2014 13.44”,但提交失败并在控制台上显示类似消息:
Caused by: com.vaadin.data.util.converter.Converter$ConversionException: Could not convert value to Timestamp
at com.vaadin.ui.AbstractField.convertToModel(AbstractField.java:725)
at com.vaadin.ui.AbstractField.getConvertedValue(AbstractField.java:811)
at com.vaadin.ui.AbstractField.commit(AbstractField.java:247)
... 42 more
Caused by: com.vaadin.data.util.converter.Converter$ConversionException: Unable to convert value of type java.util.Date to model type class java.sql.Timestamp. No converter is set and the types are not compatible.
at com.vaadin.data.util.converter.ConverterUtil.convertToModel(ConverterUtil.java:181)
at com.vaadin.ui.AbstractField.convertToModel(AbstractField.java:745)
... 45 more
我在哪里可以获得合适的转换器?谢谢你的建议。
我推荐这样的方式:
PopupDateField pdf = new PopupDateField();
Timestamp ts = new Timestamp(System.currentTimeMillis());
ObjectProperty<Timestamp> prop = new ObjectProperty<Timestamp>(ts);
pdf.setPropertyDataSource(prop);
pdf.setConverter(MyConverter.INSTANCE);
使用此转换器:
public class MyConverter implements Converter<Date, Timestamp> {
private static final long serialVersionUID = 1L;
public static final MyConverter INSTANCE = new MyConverter();
@Override
public Timestamp convertToModel(Date value,
Class<? extends Timestamp> targetType, Locale locale)
throws ConversionException {
return value == null ? null : new Timestamp(value.getTime());
}
@Override
public Date convertToPresentation(Timestamp value,
Class<? extends Date> targetType, Locale locale)
throws ConversionException {
return new Date(value.getTime());
}
@Override
public Class<Timestamp> getModelType() {
return Timestamp.class;
}
@Override
public Class<Date> getPresentationType() {
return Date.class;
}
private Object readResolve() {
return INSTANCE; // preserves singleton property
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)