Category Archives: JasperServer


Dynamic Dates For JasperServer

 Adding download for jrxml and jar file. 

Here is what the report should look like:

Clone bitbucket repo to get code:

Https: git clone
SSh: git clone


Many of the reports I design require a predefined set of dates for the user to choose. In addition I also like to allow the user to pick any start and end date they would like. The ability to choose a set of dates is especially important when we want to schedule reports, a static start and end date do not cut in this case. Out of the box, JasperServer does not have this functionality and I have been through several revisions on the way to my current implementation of  this feature.

First you need to come up with the list of dates that you would like to provide to the user. In my case I use the following:

When the user chooses “User Defined” we then use the following to allow for user settable start and end dates.

Setting up these input selectors is the easy part, the real work comes in the back-end code that interprets the choices and sets the appropriate start and end dates for the chosen time slice. In my case I decided to use Java to create a class that will return a date given a string that corresponds to the users pick list displayed above.

The prototypes for the functions are:

The Date parameter in the second set is there for the case when the user chooses “User Defined”, in this case I send in the date that they picked in order to have a single call to cover all choices.

The next step is to implement the  java code to calculate the dates from the user picks. Here is a snippet of the getStartDate and getEndDate methods for “Previous Month”:

Okay so now we know how to calculate a date given a string representation of that time slice. How do we integrate this into a JasperReport ? This is the real challenge that is needed to make it work.

First we need to create a set of parameters in the jrxml file that help us calculate and store the dates.

  • PickDate – (String) This will be filled in with the date choice (“Yesterday”, This Week”, etc).
  • StartDate – (Date) This is the users set start date (“User Defined”)
  • EndDate-  (Date) This is the users set end date  (“User Defined”)
  • StartDateChoice – (String) This is where we call the getStartDate method.
  • EndDateChoice – (String) This is where we call the getEndDate method.

For StartDateChoice we will set the “Default Value Expression” to: com.reptics.dateutils.DateByString.getStartDate($P{DateChoice},$P{StartDate})

We do something similar with EndDateChoice and then we need to use $P(EndDateChoice} in the query when looking for a date. This replaces strings like “This Year” with the date we calculated from the Java code.

To make this all work you need to define the correct input selectors in JasperServer for DateChoice, StartDate and EndDate.