Search code examples
jasper-reports

How to get last date of the month in Jasper Report date parameter input control?


I am working on a jasper report using ireport and in that I am taking two input dates START_DATE and END_DATE from user.

Now what I want is the END_DATE should be last date of the month for START_DATE. I searched on internet but I couldn't get write/get a condition which I can write in default expression for END_DATE parameter.

Can someone please suggest me a way to get this?

I tried with SQL query with an additional parameter DATE_DIFF to retrieve data for one month only. My SQL query looks like this:

SELECT EAD_PGM_CASH_SW, EAD_PGM_FS_SW, EAD_PGM_MCL_SW,COUNT(*) as CNT
FROM EAD_CASE A, EAD_PROGRAM B
WHERE A.UNIQUE_TRACE_HI = B.TO_EAD_CASE_LNK_HI
AND  A.UNIQUE_TRACE_LO = B.TO_EAD_CASE_LNK_LO
AND  A.EAD_CASE_ID = B.EAD_PGM_CASE_NUM
AND  A.EAD_CS_BATCH_DT >= $P{START_DATE}
AND  A.EAD_CS_BATCH_DT <= $P{END_DATE}
AND  $P{START_DATE}< $P{END_DATE}
AND  $P{DATE_DIFF} <= 30
AND (B.EAD_PGM_CASH_SW, B.EAD_PGM_FS_SW, B.EAD_PGM_MCL_SW) IN   (('N','Y','N'),('Y','Y','Y'),('N','N','Y'),('N','Y','Y'),('Y','N','Y'), ('Y','N','N'))
GROUP BY  EAD_PGM_CASH_SW, EAD_PGM_FS_SW, EAD_PGM_MCL_SW
ORDER BY  EAD_PGM_CASH_SW, EAD_PGM_FS_SW, EAD_PGM_MCL_SW

But this way is not proper. I want to get it using some expression in "Default Value Expression" field in ireport.


Solution

  • Have a look to this blog - Date calculation in iReport.

    I have tried this and it is working fine. Here is my jasper file-

    <?xml version="1.0" encoding="UTF-8"?>
    <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report1" language="groovy" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="d6133c1e-7de4-4386-9c92-a6c4f7cece05">
        <property name="ireport.zoom" value="1.0"/>
        <property name="ireport.x" value="0"/>
        <property name="ireport.y" value="0"/>
        <parameter name="cal" class="java.util.Calendar" isForPrompting="false">
            <defaultValueExpression><![CDATA[Calendar.getInstance()]]></defaultValueExpression>
        </parameter>
        <parameter name="lDate" class="java.util.Date" isForPrompting="false">
            <defaultValueExpression><![CDATA[(
     $P{cal}.set(new Date().getYear()+1900, new Date().getMonth(), new Date().getDate()) ||
     $P{cal}.set(Calendar.DAY_OF_MONTH, $P{cal}.getActualMaximum(Calendar.DAY_OF_MONTH))
    )
    ? null : $P{cal}.getTime()]]></defaultValueExpression>
        </parameter>
        <columnHeader>
            <band height="46" splitType="Stretch">
                <textField>
                    <reportElement x="127" y="0" width="291" height="46" uuid="bb060132-5aa4-4a75-817a-650f672c707c"/>
                    <textFieldExpression><![CDATA[$P{lDate}]]></textFieldExpression>
                </textField>
            </band>
        </columnHeader>
    </jasperReport>
    

    Note - I have used new Date() for testing.