【问题标题】:Formatting two Entity fields of the same type differently in a table在表中以不同方式格式化两个相同类型的实体字段
【发布时间】: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


【解决方案1】:

我会说最干净的解决方案是创建两个Converters,然后将转换器应用于列,如下所示:

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;
    }
}

【讨论】:

  • 这正是我想要的。效果很好!
猜你喜欢
  • 2020-08-02
  • 2017-08-20
  • 2014-11-26
  • 1970-01-01
  • 2013-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-02
相关资源
最近更新 更多