gpt4 book ai didi

postgresql - java.sql.Timestamp 的 Vaadin 转换器

转载 作者:行者123 更新时间:2023-11-29 12:12:26 27 4
gpt4 key购买 nike

我正在使用 PostgreSQL 数据库,时间戳列具有类 java.sql.Timestamp。即使此类扩展了 java.util.Date,当我编辑 PopupDateFiels 时,我也会收到错误

无法将类型 java.util.Date 的值转换为模型类型类 java.sql.Timestamp。未设置转换器且类型不兼容。

默认转换器工厂无法正常工作。我试着写

dateField.setConverter(new DateToSqlDateConverter());

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
}

}

关于postgresql - java.sql.Timestamp 的 Vaadin 转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26547228/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com