2

I am working with a Jasper report where I want to show selected date range along with time. I have used following expression to format the date but it shows time in GMT time zone.

new SimpleDateFormat("dd-MMM-yyyy").format($P{START_DATE})+" "+new SimpleDateFormat("HH:mm").format($P{startTime})

The above code gives date as 01-Mar-2019 14:30 which should be 01-Mar-2019 8:00PM as per IST.

How can I handle timezone to show correct time?

Pratik Rawlekar
  • 327
  • 4
  • 14
  • Have you tried setting the timezone? – J_D Mar 04 '19 at 09:40
  • 2
    Yes I tried but not able to write it in a single statement. I tried following approaches. new SimpleDateFormat("HH:mm").setTimeZone('IST').format($P{startTime}) - This gives error as format() cannot be applied on a void type as setTimeZone() returns void. – Pratik Rawlekar Mar 04 '19 at 09:43
  • Is the requirement to write it in a single statement? – J_D Mar 04 '19 at 09:44
  • 1
    Yes. We cannot write multiline expression/java code(One line will be object creation, calling setTimezone() on this object and then call format() to format my input date) in jasper. – Pratik Rawlekar Mar 04 '19 at 09:47
  • I recommend you don’t use `SimpleDateFormat`. That class is notoriously troublesome and long outdated. Instead use `DateTimeFormatter` and other classes from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Mar 05 '19 at 11:27

3 Answers3

6

Probably you should just set TimeZone to your environment and in general you should avoid using the old java.util.Date class.

If you are running on java 8 or above, you can use similar code to display the time in desired time zone.

<textField>
    <reportElement x="0" y="0" width="100" height="30" uuid="ac702c94-69e5-4439-9d32-3c944119dbe6"/>
    <textFieldExpression>
       <![CDATA[java.time.format.DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm").
     withZone(java.time.ZoneId.of("Asia/Calcutta")).format($P{START_DATE}.toInstant())]]>
   </textFieldExpression>
</textField>

If you are not on java 8 you can use libraries like ThreeTen-Backport or Joda-Time to have this functionality.

Full example with different zoneId on same java.util.Date

jrxml

<?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="TimeZone" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="b3143043-a16b-43db-81c4-6313b0d4922c">
    <parameter name="START_DATE" class="java.util.Date">
        <defaultValueExpression><![CDATA[new java.util.Date()]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <title>
        <band height="60" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="150" height="20" uuid="a0353412-1861-4c12-ac26-b40a6768a88c"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[America/New_York]]></text>
            </staticText>
            <staticText>
                <reportElement x="150" y="0" width="150" height="20" uuid="28b938b9-d117-4447-91d2-b5bb9334bad6"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Europe/Rome]]></text>
            </staticText>
            <staticText>
                <reportElement x="300" y="0" width="150" height="20" uuid="adceb53f-555d-4be3-bd1e-c9d55b90d90d"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Asia/Calcutta]]></text>
            </staticText>
            <textField>
                <reportElement x="0" y="20" width="150" height="20" uuid="ebf48192-f394-447b-8264-e66c56289f54"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[java.time.format.DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm").withZone(java.time.ZoneId.of("America/New_York")).format($P{START_DATE}.toInstant())]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="150" y="20" width="150" height="20" uuid="ebf48192-f394-447b-8264-e66c56289f54"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[java.time.format.DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm").withZone(java.time.ZoneId.of("Europe/Rome")).format($P{START_DATE}.toInstant())]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="300" y="20" width="150" height="20" uuid="ac702c94-69e5-4439-9d32-3c944119dbe6"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[java.time.format.DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm").withZone(java.time.ZoneId.of("Asia/Calcutta")).format($P{START_DATE}.toInstant())]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

Output

result

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
1

Use the builtin date formatting function to format values according to the report time zone (the value of the REPORT_TIME_ZONE parameter):

DATEFORMAT($P{START_DATE}, "dd-MMM-yyyy") + " " + DATEFORMAT($P{startTime}, "HH:mm")
dada67
  • 4,723
  • 17
  • 19
  • I am getting below error after using it. Caused by: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: ": "+DATEFORMAT($P{START_DATE}, "dd-MMM-yyyy") + " " + DATEFORMAT($P{startTime}, "HH:mm")+" to "+DATEFORMAT($P{END_DATE}, "dd-MMM-yyyy") + " " + DATEFORMAT($P{endTime}, "HH:mm") – Pratik Rawlekar Mar 04 '19 at 10:16
  • Post the full exception stacktrace (including Caused by.. lines). – dada67 Mar 04 '19 at 10:48
  • Caused by: java.lang.NoClassDefFoundError: net.sf.jasperreports.functions.standard.DateTimeFunctions at details_summary_1551698058964_17203.DATEFORMAT(details_summary_1551698058964_17203:1209) at details_summary_1551698058964_17203.evaluate(details_summary_1551698058964_17203:401) at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:251) ... 19 more – Pratik Rawlekar Mar 04 '19 at 11:18
  • Is there a jasperreports-functions jar on the classpath of the application that runs the report? – dada67 Mar 04 '19 at 13:27
0

To change the date and time displayed in the footer of a Jasper report, you will need to modify the report template. Here are the general steps to follow:

Open the Jasper report in the JasperSoft Studio or other report design tool.

Select the "Page Footer" section of the report template.

In the "Properties" pane, locate the "Date Format" property and modify it according to the format you want to display. For example, you can use the following format to display the date and time in Malawi time zone:

new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz").format(new java.util.Date(),new java.util.Locale("en", "MW"))

Preview the report to confirm that the date and time are now displayed in the correct format.