碧玉报告中的时区转换和日期格式? [英] Timezone conversion and date formatting in jasper reports?

查看:69
本文介绍了碧玉报告中的时区转换和日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个Jasper报表,我想在其中显示选定的日期范围以及时间.我已经使用以下表达式来格式化日期,但是它以格林尼治标准时间(GMT)时区显示时间.

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

上面的代码给出的日期为IST的2019年3月1日14:30,应为2019年3月1日8:00 PM.

如何处理时区以显示正确的时间?

解决方案

可能您应该将TimeZone设置为您的环境,并且通常您应该

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?

解决方案

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

这篇关于碧玉报告中的时区转换和日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆