有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

jasper报告与区域设置相关的Java DynamicReports格式日期

我正在使用DynamicReports API构建报告

我正在设置报告的区域设置,并格式化报告的日期列,但无论区域设置是什么,日期的格式始终为10/12/2009 10:54:44AM

代码如下所示:

rep.setTemplate(Templates.reportTemplate.setLocale(res.getLocale()));
...
if (rs.getString(i).contains("00:00:00"))
   rep.addColumn(col.column(title,  name,  type.dateType()));
else
   rep.addColumn(col.column(title,  name,  type.dateYearToSecondType()));

是否有一种方法可以自动格式化与报告的区域设置相关的日期,或者让我使用自定义的值格式化程序


共 (2) 个答案

  1. # 1 楼答案

    我还尝试了参数映射,但没有成功

    JasperReportBuilder rep = report()
       .setDataSource(query, conn.getConnection())
       .setParameter(JRParameter.REPORT_LOCALE, res.getLocale());
       .setTemplate(Templates.reportTemplate.setLocale(res.getLocale()));
    

    无法自动设置日期的格式。 唯一的方法是对区域设置的日期列使用模式

    TextColumnBuilder<Date> column = col.column(title,  name,  type.dateType());
    if (res.getLocale().equals("EN") {
      column.setPattern("dd.MM.yyyy");
    }
    else if (res.getLocale().equals("US") {
      column.setPattern("MM/dd/yyyy");
    }
    else {
       ...
    }
    rep.addColumn(column);
    
  2. # 2 楼答案

    我用这种方式在动态jasper中设置了日期字段的格式

    ColumnBuilder time = ColumnBuilder.getNew();
    time.setTitle("Login Time");
    time.setWidth(200);
    time.setColumnProperty("emp.logintime", Date.class.getName()).setPattern("dd/MM/yyyy hh:mm:ss a");
    drb.addColumn(time.build());