Friday 8 November 2013

Dates in Jasper iReport - Expression for default values and counting the number of months b/w given two dates.

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 ReportStart(Start Date) and ReportEnd(Report End) parameters.

In some reports you need to give default parameters in your start date and end date.
Eg:
Start Date = Previous month 1st day
End Date= Previous month last day
Qn :
 How to write default expression for this ?
Ans:
NOTE: 
$P{cal}  is a parameter used to reduce the expression complexity.
$P{cal}=java.util.Calendar.getInstance()

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})
StartYYYY
Integer.parseInt(new SimpleDateFormat("yyyy").format($P{ReportStart}))
StartMM
Integer.parseInt(new SimpleDateFormat("MM").format($P{ReportStart}))
StartDD
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

CurrentYear:
$P{cal}.get(Calendar.Year)
CurrentMonth:
$P{cal}.get(Calendar.MONTH)+1
CurrentDayOfMonth:
$P{cal}.get(Calendar.DAY_OF_MONTH)


IV) Calculating Number of Months b/w given two dates

 Calculationsif(EndYear==StartYear )
{
    (EndMonth-StartMonth+1)+(12*(YendYear-StartYear))
}
else
{
    if(EndYear>StartYear)
    {
        (12-StartMonth)+EndMonth+1+(12*(EndYear-StartYear-1))
    }
    else
    print "0"
}
NumberOfMonths parameter expresssion:
(
    $P{EndYYYY}==$P{StartYYYY}
)
?
(
    ($P{EndMM}-$P{StartMM}+1)+(12*($P{EndYYYY}-$P{StartYYYY}))
)
:
(
        (
            $P{EndYYYY}>$P{StartYYYY}
        )
        ?
        (
            (12-$P{StartMM})+$P{EndMM}+1+(12*($P{EndYYYY}-$P{StartYYYY}-1))
        )
        :
        0
)

V) Counting the number of Exceeded Months from current date when your "End Date " parameter is having the value of greater than current date

(
    $P{ReportEnd}.compareTo($P{cal}.getTime())>0
)
?
(
  (12-($P{cal}.get(Calendar.MONTH)+1))+$P{EndMM}+12*($P{EndYYYY}-$P{cal}.get(Calendar.YEAR)-1)
)
:
0



 Source Code of the report
Download the report using below link.
https://drive.google.com/file/d/0BymV_QP4TGBEVU5wV0xwZ3JfZG8/edit?usp=sharing
(Note that there will be no preview of the report after clicking the link, click on Download)

How to execute the report ?
You need to create two parameter in the "Input Controls" section of the report after uploading the report to jasper server.
Input Controls should be : ReportStart, ReportEnd

Sadakar
BI developer