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

4 comments:

  1. I´d like to count number of sundays in date range, in expression editor in IReport. StartDate and EndDate are taken from database fields in format dd/mm/yy. How could I do this?

    ReplyDelete
  2. Hi, Could you look into this code ?
    http://stackoverflow.com/questions/6210906/how-to-determine-number-saturdays-and-sundays-comes-between-two-dates-in-java-sc

    ReplyDelete
  3. Hi Sadakar, I would like to display a report with date range in Jasper report. The date range could be anything and the breakdown in the report has to be date wise. Could you please let me know how can I achieve here?

    ReplyDelete
  4. Could you please provide more details ? Do you mean you want the data grouped by date in detail ?

    ReplyDelete