Jaspersoft BI Tools

iReport,Jaspersoft Design Studio,Jaspersoft Server, Schema Workbench,Analysis,Dashboards, Self Service BI, Domains,Topics,Ad-Hoc Reports, Jaspersoft ETL, Embedding BI,HTML-5 charts, Fusion Charts,JFree Charts,Drill down reports , Drill down Dashboards,Visualize.js, Performance Tuning, D3 Charts, Jaspersoft Customization

Thursday, July 06, 2017

Tips : Dates Calculations - Current Month, Previous Month, Next Months (add or substract 1 month, first date of months, day name, day number 0,1..6) in Jasper Reports

It is always fun playing with date related calculations in Jasper reports.

Here is another use case to find out below shown(2nd Image) dates in Jasper Reports.

Sample Design : (Click on image to get best view of content)


Sample Output: (Click on image to get best view of content)




Example Developed in : Jaspersoft Studio Professional 6.4
(NOTE: If you get any error while doing copy paste the below JRXML code, just replace the very first line of xml from other jrxml opened in your studio).


NOTE : 
Analise the calculations in Parameters ( In some cases, we can develop the same using variables)

JRXML : Developed in 6.4 Pro  (Copy - Paste below code in a JRXML opened in your studio)

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.4.0.final using JasperReports Library version 6.4.1  -->
<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="Previous and Next Months" pageWidth="1000" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="960" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="be22b853-8ecd-45d5-993a-456ecbf2733b">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <property name="com.jaspersoft.studio.unit." value="pixel"/>
    <property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/>
    <property name="ireport.jasperserver.url" value="http://localhost:8080/jasperserver-pro/"/>
    <property name="ireport.jasperserver.user" value="superuser"/>
    <property name="ireport.jasperserver.report.resource" value="/EarthlySystems/Previous_and_Next_Months_files/main_jrxml"/>
    <property name="ireport.jasperserver.reportUnit" value="/EarthlySystems/Previous_and_Next_Months"/>
    <import value="org.apache.commons.lang.time.*"/>
    <parameter name="Date" class="java.util.Date">
        <defaultValueExpression><![CDATA[new Date()]]></defaultValueExpression>
    </parameter>
    <parameter name="PreviousMonthDate" class="java.util.Date" isForPrompting="false">
        <defaultValueExpression><![CDATA[DateUtils.addMonths($P{Date},-1)]]></defaultValueExpression>
    </parameter>
    <parameter name="NextMonthDate" class="java.util.Date" isForPrompting="false">
        <defaultValueExpression><![CDATA[DateUtils.addMonths($P{Date},1)]]></defaultValueExpression>
    </parameter>
    <parameter name="FirstDateOfCurrentMonth" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA[new SimpleDateFormat("MM/dd/yyyy").format(java.util.Date.parse(MONTH($P{Date}) + "/1/" + YEAR($P{Date})))]]></defaultValueExpression>
    </parameter>
    <parameter name="FirstDayOfCurrentMonth" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA[new SimpleDateFormat("EEE").format(java.util.Date.parse(MONTH($P{Date}) + "/1/" + YEAR($P{Date})))]]></defaultValueExpression>
    </parameter>
    <parameter name="FirstDateOfPreviousMonth" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA[new SimpleDateFormat("MM/dd/yyyy").format(java.util.Date.parse(MONTH($P{PreviousMonthDate}) + "/1/" + YEAR($P{PreviousMonthDate})))]]></defaultValueExpression>
    </parameter>
    <parameter name="FirstDayOfPreviousMonth" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA[new SimpleDateFormat("EEE").format(java.util.Date.parse(MONTH($P{PreviousMonthDate}) + "/1/" + YEAR($P{PreviousMonthDate})))]]></defaultValueExpression>
    </parameter>
    <parameter name="FirstDateOfNextMonth" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA[new SimpleDateFormat("MM/dd/yyyy").format(java.util.Date.parse(MONTH($P{NextMonthDate}) + "/1/" + YEAR($P{NextMonthDate})))]]></defaultValueExpression>
    </parameter>
    <parameter name="FirstDayOfNextMonth" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA[new SimpleDateFormat("EEE").format(java.util.Date.parse(MONTH($P{NextMonthDate}) + "/1/" + YEAR($P{NextMonthDate})))]]></defaultValueExpression>
    </parameter>
    <parameter name="CurrentDateFirstDayValueString" class="java.lang.String" isForPrompting="false">
        <parameterDescription><![CDATA[]]></parameterDescription>
        <defaultValueExpression><![CDATA[$P{FirstDayOfCurrentMonth}.equals("Sun") ? "0":
$P{FirstDayOfCurrentMonth}.equals("Mon") ? "1" :
$P{FirstDayOfCurrentMonth}.equals("Tue") ? "2" :
$P{FirstDayOfCurrentMonth}.equals("Wed") ? "3" :
$P{FirstDayOfCurrentMonth}.equals("Thu") ? "4" :
$P{FirstDayOfCurrentMonth}.equals("Fri") ? "5" :
$P{FirstDayOfCurrentMonth}.equals("Sat") ? "6" : "100"]]></defaultValueExpression>
    </parameter>
    <parameter name="CurrentDateFirstDayValueInteger" class="java.lang.Integer" isForPrompting="false">
        <parameterDescription><![CDATA[]]></parameterDescription>
        <defaultValueExpression><![CDATA[Integer.parseInt($P{CurrentDateFirstDayValueString})]]></defaultValueExpression>
    </parameter>
    <parameter name="PreviousMonthFirstDayValueString" class="java.lang.String" isForPrompting="false">
        <parameterDescription><![CDATA[]]></parameterDescription>
        <defaultValueExpression><![CDATA[$P{FirstDayOfPreviousMonth}.equals("Sun") ? "0":
$P{FirstDayOfPreviousMonth}.equals("Mon") ? "1" :
$P{FirstDayOfPreviousMonth}.equals("Tue") ? "2" :
$P{FirstDayOfPreviousMonth}.equals("Wed") ? "3" :
$P{FirstDayOfPreviousMonth}.equals("Thu") ? "4" :
$P{FirstDayOfPreviousMonth}.equals("Fri") ? "5" :
$P{FirstDayOfPreviousMonth}.equals("Sat") ? "6" : "100"]]></defaultValueExpression>
    </parameter>
    <parameter name="PerviousMonthFirstDayValueInteger" class="java.lang.Integer" isForPrompting="false">
        <parameterDescription><![CDATA[]]></parameterDescription>
        <defaultValueExpression><![CDATA[Integer.parseInt($P{PreviousMonthFirstDayValueString})]]></defaultValueExpression>
    </parameter>
    <parameter name="NextMonthFirstDayValueString" class="java.lang.String" isForPrompting="false">
        <parameterDescription><![CDATA[]]></parameterDescription>
        <defaultValueExpression><![CDATA[$P{FirstDayOfNextMonth}.equals("Sun") ? "0":
$P{FirstDayOfNextMonth}.equals("Mon") ? "1" :
$P{FirstDayOfNextMonth}.equals("Tue") ? "2" :
$P{FirstDayOfNextMonth}.equals("Wed") ? "3" :
$P{FirstDayOfNextMonth}.equals("Thu") ? "4" :
$P{FirstDayOfNextMonth}.equals("Fri") ? "5" :
$P{FirstDayOfNextMonth}.equals("Sat") ? "6" : "100"]]></defaultValueExpression>
    </parameter>
    <parameter name="NextMonthFirstDayValueInteger" class="java.lang.Integer" isForPrompting="false">
        <parameterDescription><![CDATA[]]></parameterDescription>
        <defaultValueExpression><![CDATA[Integer.parseInt($P{NextMonthFirstDayValueString})]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="175" splitType="Stretch">
            <textField>
                <reportElement x="100" y="0" width="100" height="30" uuid="686f531e-d1c4-44ef-884d-378f2a36fc1e"/>
                <textElement verticalAlignment="Middle">
                    <font size="11" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{Date}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="100" y="30" width="100" height="30" uuid="5ca971f8-a82e-4cd2-97b5-9e1e8055e59f">
                    <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                </reportElement>
                <textElement verticalAlignment="Middle">
                    <font size="11" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{PreviousMonthDate}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="100" y="60" width="100" height="30" uuid="6f1e9e0c-16f7-4ff1-b14f-21d1da654222"/>
                <textElement verticalAlignment="Middle">
                    <font size="11" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{NextMonthDate}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="0" width="100" height="30" uuid="16e4c44e-16c8-477e-a31d-f8fe7582ae7f"/>
                <textElement verticalAlignment="Middle">
                    <font size="11" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["Current Date"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="30" width="100" height="30" uuid="0b313be3-2ed2-4f90-933b-cbc57673ea69"/>
                <textElement verticalAlignment="Middle">
                    <font size="11" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["Previous Month"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="60" width="100" height="30" uuid="a2585866-90c0-4b59-b68a-509247a5927e"/>
                <textElement verticalAlignment="Middle">
                    <font size="11" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["Next Month"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="390" y="0" width="100" height="30" uuid="09a6c4ad-4b9a-43be-a05e-bc57a5b18bf9"/>
                <textElement verticalAlignment="Middle">
                    <font size="11" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{FirstDateOfCurrentMonth}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="230" y="0" width="160" height="30" uuid="b67025b7-8677-4a3f-8f8d-1162bf7d26e3"/>
                <textElement verticalAlignment="Middle">
                    <font size="11" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["First Date of Current Month"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="670" y="0" width="100" height="30" uuid="421e66b1-d745-4117-bee0-0af184eaca3d"/>
                <textElement verticalAlignment="Middle">
                    <font size="11" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{FirstDayOfCurrentMonth}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="510" y="0" width="160" height="30" uuid="d180e9ba-ae84-4b94-9b51-43648bba475a"/>
                <textElement verticalAlignment="Middle">
                    <font size="11" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["First Day of Current Month"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="230" y="30" width="160" height="30" uuid="7157a773-e62f-4e0c-9f03-4696a30a9136"/>
                <textElement verticalAlignment="Middle">
                    <font size="11" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["First Date of Previous Month"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="390" y="30" width="100" height="30" uuid="3d3ed51e-f5f0-4892-9c42-ae52ef3b7651"/>
                <textElement verticalAlignment="Middle">
                    <font size="11" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{FirstDateOfPreviousMonth}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="510" y="30" width="160" height="30" uuid="b384ade7-ea31-45ec-a756-82c37b8f6e02"/>
                <textElement verticalAlignment="Middle">
                    <font size="11" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["First Day of Previous Month"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="670" y="30" width="100" height="30" uuid="4990802a-be00-4306-91f6-1a5e50c4bc98"/>
                <textElement verticalAlignment="Middle">
                    <font size="11" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{FirstDayOfPreviousMonth}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="230" y="60" width="160" height="30" uuid="87c32b3a-5794-4853-b5de-129f0273b839"/>
                <textElement verticalAlignment="Middle">
                    <font size="11" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["First Date of Next  Month"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="390" y="60" width="100" height="30" uuid="5d12680a-5c90-4b9f-af78-c41df1c5167d"/>
                <textElement verticalAlignment="Middle">
                    <font size="11" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{FirstDateOfNextMonth}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="510" y="60" width="160" height="30" uuid="c981572c-6f18-4eb9-a7b9-d875d19f26ca"/>
                <textElement verticalAlignment="Middle">
                    <font size="11" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["First Day of Next  Month"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="670" y="60" width="100" height="30" uuid="77c38b0a-3206-4767-9519-0e497db7cbe0"/>
                <textElement verticalAlignment="Middle">
                    <font size="11" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{FirstDayOfNextMonth}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="770" y="0" width="80" height="30" uuid="259b0efd-cf1c-4963-a4e3-1785d602d59c"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font size="11" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{CurrentDateFirstDayValueInteger}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="770" y="30" width="80" height="30" uuid="f10baf74-a727-48d9-bfcf-1b6d3452736f"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font size="11" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{PerviousMonthFirstDayValueInteger}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="770" y="60" width="80" height="30" uuid="71be88e2-4893-41d5-bef4-6bbdb4cb7c6a"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font size="11" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{NextMonthFirstDayValueInteger}]]></textFieldExpression>
            </textField>
            <textField hyperlinkType="ReportExecution" hyperlinkTarget="Blank">
                <reportElement x="139" y="126" width="281" height="30" uuid="7d969a03-940c-4081-8210-d90686c0ae5c"/>
                <textElement>
                    <font size="14"/>
                </textElement>
                <textFieldExpression><![CDATA["Previous Month"]]></textFieldExpression>
                <hyperlinkParameter name="_report">
                    <hyperlinkParameterExpression><![CDATA["/EarthlySystems/Previous_and_Next_Months"]]></hyperlinkParameterExpression>
                </hyperlinkParameter>
                <hyperlinkParameter name="Date">
                    <hyperlinkParameterExpression><![CDATA[$P{PreviousMonthDate}]]></hyperlinkParameterExpression>
                </hyperlinkParameter>
                <hyperlinkParameter name="DayReturnValueInteger">
                    <hyperlinkParameterExpression><![CDATA[$P{PerviousMonthFirstDayValueInteger}]]></hyperlinkParameterExpression>
                </hyperlinkParameter>
            </textField>
            <textField hyperlinkType="ReportExecution" hyperlinkTarget="Blank">
                <reportElement x="440" y="126" width="281" height="30" uuid="64116c95-e582-476e-b5ca-fe88e26c7d77"/>
                <textElement>
                    <font size="14"/>
                </textElement>
                <textFieldExpression><![CDATA["Next  Month"]]></textFieldExpression>
                <hyperlinkParameter name="_report">
                    <hyperlinkParameterExpression><![CDATA["/EarthlySystems/Previous_and_Next_Months"]]></hyperlinkParameterExpression>
                </hyperlinkParameter>
                <hyperlinkParameter name="Date">
                    <hyperlinkParameterExpression><![CDATA[$P{NextMonthDate}]]></hyperlinkParameterExpression>
                </hyperlinkParameter>
                <hyperlinkParameter name="DayReturnValueInteger">
                    <hyperlinkParameterExpression><![CDATA[$P{NextMonthFirstDayValueInteger}]]></hyperlinkParameterExpression>
                </hyperlinkParameter>
            </textField>
        </band>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="125" splitType="Stretch"/>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>


I hope it helps some one in community.!

Cheers.!
Sadakar Pochampalli

Wednesday, July 05, 2017

Jaspersoft 6.4 : New download Information page- Quick difference between CE & EE

Jaspersoft 6.4 Highlights

  • Extended dashboard authoring for hyperlinking, formatting, and editing
  • Granular admin exporting control
  • Built-in support for popular technologies like Salesforce and PhantomJS
  • Updated interface for designing charts in Jaspersoft Studio

Read Detailed Highlights here : Click this link

Tip : replaceAll method to remove extra & in a field text in Jasper Reports

This expression in Jasper reports is useful for a text that need to get into a new line whenever an & occurs but the problem is though the text gets into a new line it is displaying an extra & on output. 

Hence, using another replaceAll function from core java for field object we can overcome getting extra &.


Sample Expression:
 $F{ProjectDescription}.replaceAll("lt;br&gt;", "<br>" ).replaceAll("&","")

Tip Credits: Nimmana