在 table 中以不同方式格式化两个相同类型的实体字段
Formatting two Entity fields of the same type differently in a table
我正在使用带有实体 类 的 JPA 容器从 MySQL 填充 Table。
其中两个字段是 GregorianCalendar 字段(一个是日期,一个是时间),我正在寻找一种方法 format/convert 它们分开,以便日期显示为短日期(例如 dd-MM-yyyy)和根据语言环境,时间显示为 24 小时短时间 (HH:mm)。
根据我的搜索结果,我了解到格式化日期和时间的最佳方法是覆盖默认值 table,所以这就是我所做的:
final Table opTable = new Table() {
private static final long serialVersionUID = 1L;
@Override
protected String formatPropertyValue(Object rowId, Object colId, Property property) {
Object v = property.getValue();
if (v instanceof GregorianCalendar) {
GregorianCalendar datez = (GregorianCalendar) v;
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMANY);
String formattedDate = df.format(datez.getTime());
return formattedDate;
}
if (v instanceof GregorianCalendar) {
GregorianCalendar timeValue = (GregorianCalendar) v;
SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss");
fmt.setCalendar(timeValue);
String timeFormatted = fmt.format(timeValue.getTime());
return timeFormatted;
}
return super.formatPropertyValue(rowId, colId, property);
}
};
并填充 Table:
bookings = JPAContainerFactory.make(BookingEntity.class, CSAdminUI.PERSISTENCE_UNIT);
opTable = new Table(null, bookings); opTable.setContainerDataSource(bookings);
opTable.setVisibleColumns(new Object[] { "operator.rowid", "activity.name", "startDate", "startTime", "done", "price", "operatorCost", "paymentType", "voucherCode", "hints", "timeLeft", "succeded", "teamPicture", "comment" });
第一个条件是唯一被应用的条件,因为这两个字段都是 GregorianCalendar 的实例。
有没有其他方法可以引用这些字段,或者我可以有选择地设置它们的格式,同时记住它们都是同一类型?
我想说最干净的解决方案是创建两个 Converter
,然后将转换器应用到列,如下所示:
table.setConverter("myPropertyId", new MyConverter());
这里有一个关于 Converter
实现的例子:
public class MyConverter implements Converter<String, GregorianCalendar> {
@Override
public GregorianCalendar convertToModel(String value, Class<? extends GregorianCalendar> targetType, Locale locale) throws ConversionException {
throw new ConversionException("Converting from Presentation to Model is not supported.");
}
@Override
public String convertToPresentation(GregorianCalendar value, Class<? extends String> targetType, Locale locale) throws ConversionException {
// TODO do the conversion here
return null;
}
@Override
public Class<GregorianCalendar> getModelType() {
return GregorianCalendar.class;
}
@Override
public Class<String> getPresentationType() {
return String.class;
}
}
我正在使用带有实体 类 的 JPA 容器从 MySQL 填充 Table。 其中两个字段是 GregorianCalendar 字段(一个是日期,一个是时间),我正在寻找一种方法 format/convert 它们分开,以便日期显示为短日期(例如 dd-MM-yyyy)和根据语言环境,时间显示为 24 小时短时间 (HH:mm)。
根据我的搜索结果,我了解到格式化日期和时间的最佳方法是覆盖默认值 table,所以这就是我所做的:
final Table opTable = new Table() {
private static final long serialVersionUID = 1L;
@Override
protected String formatPropertyValue(Object rowId, Object colId, Property property) {
Object v = property.getValue();
if (v instanceof GregorianCalendar) {
GregorianCalendar datez = (GregorianCalendar) v;
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMANY);
String formattedDate = df.format(datez.getTime());
return formattedDate;
}
if (v instanceof GregorianCalendar) {
GregorianCalendar timeValue = (GregorianCalendar) v;
SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss");
fmt.setCalendar(timeValue);
String timeFormatted = fmt.format(timeValue.getTime());
return timeFormatted;
}
return super.formatPropertyValue(rowId, colId, property);
}
};
并填充 Table:
bookings = JPAContainerFactory.make(BookingEntity.class, CSAdminUI.PERSISTENCE_UNIT);
opTable = new Table(null, bookings); opTable.setContainerDataSource(bookings);
opTable.setVisibleColumns(new Object[] { "operator.rowid", "activity.name", "startDate", "startTime", "done", "price", "operatorCost", "paymentType", "voucherCode", "hints", "timeLeft", "succeded", "teamPicture", "comment" });
第一个条件是唯一被应用的条件,因为这两个字段都是 GregorianCalendar 的实例。 有没有其他方法可以引用这些字段,或者我可以有选择地设置它们的格式,同时记住它们都是同一类型?
我想说最干净的解决方案是创建两个 Converter
,然后将转换器应用到列,如下所示:
table.setConverter("myPropertyId", new MyConverter());
这里有一个关于 Converter
实现的例子:
public class MyConverter implements Converter<String, GregorianCalendar> {
@Override
public GregorianCalendar convertToModel(String value, Class<? extends GregorianCalendar> targetType, Locale locale) throws ConversionException {
throw new ConversionException("Converting from Presentation to Model is not supported.");
}
@Override
public String convertToPresentation(GregorianCalendar value, Class<? extends String> targetType, Locale locale) throws ConversionException {
// TODO do the conversion here
return null;
}
@Override
public Class<GregorianCalendar> getModelType() {
return GregorianCalendar.class;
}
@Override
public Class<String> getPresentationType() {
return String.class;
}
}