The Java DynamicReports format date associated with the locale

The Java DynamicReports format date associated with the locale … here is a solution to the problem.

The Java DynamicReports format date associated with the locale

I’m using the DynamicReports API to build reports.

I’m setting the locale of the report and formatting the date column of the report, but the date is always formatted as 10/12/2009 10:54:44 AM No matter what the locale is.

The code is as follows:

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()));

Is there a way to automatically format dates based on the locale of the report, or do I need to use a custom ValueFormatter?

Solution

I also tried parameter mapping but it didn’t work

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

Dates cannot be formatted automatically.
The only way to do this is to use a pattern for date columns based on locale.

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

Related Problems and Solutions