Reputation: 327
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?
Upvotes: 2
Views: 10072
Reputation: 1
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.
Upvotes: 0
Reputation: 21710
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.
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
Upvotes: 7
Reputation: 5093
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")
Upvotes: 1