Thursday, 2 April 2015

Dates in Jasper iReport or Jasper Studio


1) Current Date (java.util.Date )
   new Date()

2) Yesterday (java.lang.String )
  new SimpleDateFormat("yyyy-MM-dd").format(new Date().getTime() - 1* 24 * 60 * 60 * 1000)

3) Year (java.lang.String or java.lang.Integer)
new SimpleDateFormat("yyyy").format($P{CurrentDate})

4) Month - (java.lang.String or java.lang.Integer)
new SimpleDateFormat("MM").format($P{CurrentDate})

NOTE : All are parameter expressions 


Output:


JRXML

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.0.0.final using JasperReports Library version 6.0.0  -->
<!-- 2015-04-02T11:41:40 -->
<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="Dates" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a16f4b6d-d5e0-4149-a914-a9c6f9a2e3f7">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <import value="org.apache.commons.lang.time.*"/>
    <parameter name="CurrentDate" class="java.util.Date">
        <defaultValueExpression><![CDATA[new Date()]]></defaultValueExpression>
    </parameter>
    <parameter name="YesterDay" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA[ new SimpleDateFormat("yyyy-MM-dd").format(new Date().getTime() - 1* 24 * 60 * 60 * 1000)]]></defaultValueExpression>
    </parameter>
    <parameter name="Year" class="java.lang.String" isForPrompting="false">
        <parameterDescription><![CDATA[]]></parameterDescription>
        <defaultValueExpression><![CDATA[new SimpleDateFormat("yyyy").format($P{CurrentDate})]]></defaultValueExpression>
    </parameter>
    <parameter name="Month" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA[new SimpleDateFormat("MM").format($P{CurrentDate})]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="130" splitType="Stretch">
            <textField>
                <reportElement x="180" y="0" width="100" height="30" uuid="a2b61b2f-e891-415e-b49e-e6c4d4fe62fe"/>
                <textElement textAlignment="Left" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$P{CurrentDate}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="180" y="30" width="100" height="30" uuid="2732b9cc-8dcc-4a64-9da6-2ca5273ff95b"/>
                <textElement textAlignment="Left" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$P{YesterDay}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="180" y="60" width="100" height="30" uuid="a19d13b2-ef38-4e9f-a0c7-9bd78d6b7968"/>
                <textElement textAlignment="Left" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$P{Year}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="180" y="90" width="100" height="30" uuid="3f2ee16c-a9eb-4530-a274-e806a11a1d91"/>
                <textElement textAlignment="Left" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$P{Month}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="80" y="0" width="100" height="30" uuid="6449e356-b6db-4a93-8b4a-f60aa1b66ae0"/>
                <textElement textAlignment="Left" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA["Current Date"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="80" y="30" width="100" height="30" uuid="ca7a0a48-71cd-4551-9214-127e7a60966b"/>
                <textElement textAlignment="Left" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA["Yesterday"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="80" y="60" width="100" height="30" uuid="a85e016c-eec8-41f4-82f5-f9ff189712be"/>
                <textElement textAlignment="Left" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA["Year"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="80" y="90" width="100" height="30" uuid="07aba093-af79-4752-a6fd-07e4a0bb90bf"/>
                <textElement textAlignment="Left" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA["Month"]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

You may be interested in exploring below

1) Date Parameters in iReport Designer OR Date Input Controls in JasperServer Repository 

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

3) Jasper Server Calendar Date input control date fomat change - Change date format from yy-mm-dd to mm/dd/yyyy 

4) Date input control default value in jasper iReport - Giving date value in default expression. 

5)  Easy way of Substracting,Adding Months or Days from parameter date in Jasper studio or iReport : Ex : Substracting 2 months from current date




No comments:

Post a Comment