我正在使用具有實體類的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()'相同的輸出' – Mimzo
屬性通常不僅是一個值,而且還有更多信息。你如何填充你的桌子? –