Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
868 views
in Technique[技术] by (71.8m points)

java - Timezone conversion and date formatting in jasper reports?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...