Search code examples
javajasper-reports

Using java.time.LocalDate in a field with Jasper Report


I have an application developed in NetBeans 8.1 with iReport and JasperReport version 5.5.0 plugin. In my application, I have a jasper report with a JRBeanCollectionDataSource.

LocalDate dateFrom = jDateChooser1.getDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
LocalDate dateUntil = jDateChooser2.getDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
List<Registru> regs = db.getRegistreFrom(dateFrom, dateUntil);

Map<String, Object> params = new HashMap<>();
String absolutePath = "....\\rapoarte\\";
params.put("SUBREPORT_DIR", absolutePath);
InputStream in = getClass().getResourceAsStream("/rapoarte/registreListare.jasper");
JasperPrint jp = JasperFillManager.fillReport(in, params, new JRBeanCollectionDataSource(regs,false));

// ... code here

JRBeanCollectionDataSource takes a parameter of List of Report class returned from the database

public class Registru {
   private int id;
   private int nrReg;
   private LocalDate date;
   // ... getters and setters
}

The problem is that in the report the fields are :

<field name="id" class="java.lang.Integer"/>
<field name="nrReg" class="java.lang.Integer"/>
<field name="date" class="java.time.LocalDate"/>

but when I compile it, it gives me an error

java.time.LocalDate cannot be resolved to a type

It works only if I put <field name="date class="java.lang.Object"/> but I cannot format the date.

I want to format the date of pattern "dd-MM-yyyy". Do I have to add something to the jasper's classpath?


Solution

  • This problem is related to the JTD compiler (compiler for the jrxml) that you are using.

    You will need a recent version of Eclipse Java Compilers (ecj) to use java-8, check the jasper-distribution.

    To use pattern on the java.time.LocalDate or java.time.LocalDateTime see this question for more information