【发布时间】:2015-11-04 11:18:33
【问题描述】:
我正在使用带有实体类的 JPA 容器来填充来自 MySQL 的表。 其中两个字段是 GregorianCalendar 字段(一个是日期,一个是时间),我正在寻找一种分别格式化/转换它们的方法,以便日期显示为短日期(例如 dd-MM-yyyy)并且时间出现根据区域设置,最短 24 小时 (HH:mm)。
根据我的搜索结果,我知道格式化日期和时间的最佳方法是覆盖默认表,所以这就是我所做的:
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);
}
};
并填充表格:
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 的实例。 有没有其他方法可以引用这些字段,或者我可以有选择地格式化它们,记住它们都是同一类型?
【问题讨论】:
-
我认为
Property property参数包含属性名称,因此您可以更改属性名称的行为 -
我不认为
Property property拥有名称,但它拥有实际价值。它似乎与property.getValue()几乎相同 打印property.toString()提供与打印property.getValue()相同的输出 -
属性通常不仅仅是一个值,还有更多的信息。你如何填充你的表?
-
我使用 JPA 容器,然后设置可见字段。这是一些代码:
bookings = JPAContainerFactory.make(BookingEntity.class, DCSAdminUI.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" });
标签: java jpa vaadin vaadin7 gregorian-calendar