简体   繁体   English

DynamicReports不将模式应用于格式ZonedDateTime

[英]DynamicReports doesn't apply pattern to format ZonedDateTime

I have a model class that contains field of type ZonedDateTime . 我有一个模型类,其中包含ZonedDateTime类型的字段 When generate report data is shown as: 生成报告数据时显示为:

2017-08-17T16:09:03+03:00[Europe/Chisinau] 2017-08-17T16:09:03 + 03:00 [欧洲/基希讷乌]

To format that date I use method setPattern("dd.MM.yyyy") but nothing changes. 为了格式化该日期,我使用了setPattern("dd.MM.yyyy")但是没有任何变化。

All stuff is generated via reflection, check source: 所有的东西都是通过反射生成的,请检查源:

for (Field field : entityClass.getDeclaredFields()) {
    String fieldName = field.getName();
    if (usedFields.contains(fieldName)) {
        field.setAccessible(true);
        if(field.getType().isAssignableFrom(Date.class) || field.getType().isAssignableFrom(ZonedDateTime.class)){
            report.addColumn(Columns.column(fieldName, fieldName, field.getType()).setPattern("dd.MM.yyyy"));
        }else {
            report.addColumn(Columns.column(fieldName, fieldName, field.getType()));
        }
    }
}

Generate report where list is a generic list of elements. 生成报告 ,其中list是元素的常规列表。

report
.title(
        Components.text("Documents Report")
        .setStyle(getHeaderCenteredBoldStyle())
        .setHorizontalTextAlignment(HorizontalTextAlignment.CENTER))
        .pageFooter(Components.pageXofY())
.setColumnTitleStyle(getColumnTitleStyle())
.highlightDetailEvenRows()
.setDataSource(list);

Generated PDF 生成的PDF

生成pdf Any ideas? 有任何想法吗?

It seems that setPattern works fine for java.util.Date , but not for java.time.ZonedDateTime . 看来setPattern对于java.util.Date可以正常工作,但对于java.time.ZonedDateTime不能。 I've created a simple entity: 我创建了一个简单的实体:

public class Entity {

    private ZonedDateTime zonedDateTime = ZonedDateTime.parse("2017-08-17T16:09:03+03:00[Europe/Chisinau]");

    private Date date = new Date();

    // getters and setters
}

Then I've used your code ( for (Field field : entityClass.getDeclaredFields()) etc ) to add the columns. 然后,我使用了您的代码( for (Field field : entityClass.getDeclaredFields()) etc )来添加列。 For the data source list , I've created it manually, just to test: 对于数据源list ,我已经手动创建了它,只是为了测试:

DRDataSource ds = new DRDataSource("zonedDateTime", "date");
ds.add(entity.getZonedDateTime(), entity.getDate());

In the resulting report, the ZonedDateTime wasn't formatted (it's shown as 2017-08-17T16:09:03+03:00[Europe/Chisinau] ), but the Date was correctly displayed as 21.08.2017 . 在结果报告中,未格式化ZonedDateTime (显示为2017-08-17T16:09:03+03:00[Europe/Chisinau] ),但是Date正确显示为21.08.2017

One way to fix this is to set a value formatter and use a java.time.format.DateTimeFormatter to convert the ZonedDateTime to a String : 解决此问题的一种方法是设置值格式器,并使用java.time.format.DateTimeFormatterZonedDateTime转换为String

if (field.getType().isAssignableFrom(ZonedDateTime.class)) {
    // ZonedDateTime, use a value formatter
    report.addColumn(Columns.column(fieldName, fieldName, ZonedDateTime.class)
        // set a custom value formatter
        .setValueFormatter(new AbstractValueFormatter<String, ZonedDateTime>() {
            @Override
            public String format(ZonedDateTime value, ReportParameters reportParameters) {
                // convert ZonedDateTime to dd.MM.yyyy format
                DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd.MM.yyyy");
                return value.format(fmt);
            }
        }));
} else if (field.getType().isAssignableFrom(Date.class)) {
    // java.util.Date: setPattern works
    report.addColumn(Columns.column(fieldName, fieldName, field.getType()).setPattern("dd.MM.yyyy"));
...

With this, the ZonedDateTime is displayed with the format defined in the DateTimeFormatter : 这样, ZonedDateTime DateTimeFormatter定义的格式显示ZonedDateTime

17.08.2017 2017年8月17日

In this example I'm creating the DateTimeFormatter inside the inner class, but it's better to create just one formatter outside of the if (it can be in a static final field, as it's immutable and thread-safe) and reuse it accross your application. 在此示例中,我在内部类内部创建DateTimeFormatter ,但最好在if外部(它可以位于static final字段中,因为它是不可变的且线程安全的)之外仅创建一个格式化程序,并在应用程序中重用它。


Another alternative is to manually format the ZonedDateTime to a String , using the same java.time.format.DateTimeFormatter , but in the data source list. 另一种选择是使用相同的java.time.format.DateTimeFormatter ,但在数据源列表中,将ZonedDateTime手动格式化为String I also had to change the type of the column to String (inside the for ): 我还必须将列的类型更改为String (在for ):

if (field.getType().isAssignableFrom(ZonedDateTime.class)) {
    // ZonedDateTime, manually convert to String
    report.addColumn(Columns.column(fieldName, fieldName, String.class));
} else if (field.getType().isAssignableFrom(Date.class)) {
    // java.util.Date: setPattern works
    report.addColumn(Columns.column(fieldName, fieldName, field.getType()).setPattern("dd.MM.yyyy"));
} else {
    report.addColumn(Columns.column(fieldName, fieldName, field.getType()));
}

Then, in the data source list , I had to use the DateTimeFormatter to format the ZonedDateTime to a String : 然后,在数据源list ,我必须使用DateTimeFormatterZonedDateTimeString

// create formatter
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd.MM.yyyy");

// format the ZonedDateTime
ds.add(entity.getZonedDateTime().format(fmt), entity.getDate());

With this, the ZonedDateTime is now shown as 17.08.2017 . 这样, ZonedDateTime现在显示为17.08.2017

I'm not sure how you're creating the list of values, but you'll have to change it to format the ZonedDateTime in the way described above (or do it internally in the entity). 我不确定如何创建值list ,但必须更改它以上述方式设置ZonedDateTime格式(或在实体内部进行设置)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM