Dates in Jasper iReport – Expression for default values

This post teach you how to play with default date parameters & calculations on dates in jasper iReport.
In this tutorial you learn how to make use of  “SimpleDateFormat” class and java.util.Calendar class.

I) Let us start with Report Start(Start Date) and Report End(Report End) parameters.

In some reports you need to give default parameters in your start date and end date.
Start Date = Previous month 1st day
End Date= Previous month last day
Qn :
 How to write default expression for this ?
$P{cal}  is a parameter used to reduce the expression complexity.
Start Date: ( java.util.Date)
$P{cal}.add(java.util.Calendar.MONTH, -1) ||
$P{cal}.set(java.util.Calendar.DAY_OF_MONTH, 1)
? null : $P{cal}.getTime()
 End Date(java.util.Date)
$P{cal}.set(java.util.Calendar.DAY_OF_MONTH, $P{cal}.getActualMaximum(java.util.Calendar.DAY_OF_MONTH))
? null : $P{cal}.getTime()
Sample output(i.e., default values in the date place holders would be)
Star Date :  2013-10-01
End Date : 2013-10-31

II) To get the individuals of a date using SimpleDateFormat class.

From Report Start  Date($P{ReportStart})
Integer.parseInt(new SimpleDateFormat(“yyyy”).format($P{ReportStart}))
Integer.parseInt(new SimpleDateFormat(“MM”).format($P{ReportStart}))
Integer.parseInt(new SimpleDateFormat(“dd”).format($P{ReportStart})) 
Similarly you can extract the parts of the date for ReportEnd parameter($P{ReportEnd} )

III) To get the current parts of the date from Calendar class in iReport


Leave a Reply

Your email address will not be published. Required fields are marked *