jasper报告如何根据用户时区打印数据

时间:2017-02-23 12:28:04

标签: timezone jasper-reports

根据用户的时区,有人可以帮我打印数据吗?我们有一个场景,用户将通过时区,并根据该值返回。我找到了根据用户传递时区修改日期参数的方法,并从数据库中获取结果。但之后我应该如何在时区中写入数据。我是否需要将单个日期列转换为在时区上打印,或者根据时区转换一次并打印所有值。

让我告诉您,我们尝试在现有报告中实施此功能,因此我们正在寻找最少变更的流程。

请查找示例报告的源代码。

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0  -->
<!-- 2017-02-27T18:40:31 -->
<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="TimeZoneQuery" pageWidth="842" pageHeight="595" orientation="Landscape" whenNoDataType="NoDataSection" columnWidth="842" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" isIgnorePagination="true" uuid="6af36fbc-520f-436a-a8b5-1510d55e7474">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <style name="column header" mode="Opaque" forecolor="#000000" backcolor="#D2D2D2" fill="Solid" vTextAlign="Middle" vImageAlign="Middle" fontName="Tahoma" fontSize="11" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false">
        <box>
            <topPen lineWidth="0.25" lineStyle="Solid" lineColor="#979991"/>
            <leftPen lineWidth="0.25" lineStyle="Solid" lineColor="#979991"/>
            <bottomPen lineWidth="0.25" lineStyle="Solid" lineColor="#979991"/>
            <rightPen lineWidth="0.25" lineStyle="Solid" lineColor="#979991"/>
        </box>
    </style>
    <style name="detail" forecolor="#000000" fill="Solid" vTextAlign="Middle" vImageAlign="Middle" fontName="Tahoma" fontSize="11" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false">
        <box>
            <pen lineWidth="0.25" lineColor="#666666"/>
            <topPen lineWidth="0.25" lineColor="#979991"/>
            <leftPen lineWidth="0.25" lineColor="#979991"/>
            <bottomPen lineWidth="0.25" lineColor="#979991"/>
            <rightPen lineWidth="0.25" lineColor="#979991"/>
        </box>
    </style>
    <parameter name="FromDate" class="java.lang.String">
        <parameterDescription><![CDATA[]]></parameterDescription>
    </parameter>
    <parameter name="ToDate" class="java.lang.String">
        <parameterDescription><![CDATA[]]></parameterDescription>
    </parameter>
    <parameter name="userTimezone" class="java.lang.String"/>
    <parameter name="FromDateTZ" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["'"+$P{REPORT_FORMAT_FACTORY}.createDateFormat("yyyy-MM-dd HH:mm:ss",$P{REPORT_LOCALE}, java.util.TimeZone.getTimeZone($P{userTimezone})).format(new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse($P{FromDate}+" 00:00:00'"))+"'"]]></defaultValueExpression>
    </parameter>
    <parameter name="ToDateTZ" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["'"+$P{REPORT_FORMAT_FACTORY}.createDateFormat("yyyy-MM-dd HH:mm:ss",$P{REPORT_LOCALE}, java.util.TimeZone.getTimeZone($P{userTimezone})).format(new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse($P{ToDate}+" 00:00:00'"))+"'"]]></defaultValueExpression>
    </parameter>
    <parameter name="PrintTZ" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA[$P{REPORT_TIME_ZONE}.getTimeZone($P{userTimezone})]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[select job.transcribed_date::text,job.created_Date::text, job.delivered_date::text,job.JOB_ID, job.client_facility_iD,job.DICTATION_LENGTH
from DW.job_f job 
where job.transcribed_date between to_timestamp( $P!{FromDateTZ} ,'YYYY-MM-DD HH24:MI:SS') and to_timestamp( $P!{ToDateTZ}  ,'YYYY-MM-DD HH24:MI:SS')]]>
    </queryString>
    <field name="transcribed_date" class="java.lang.String"/>
    <field name="created_date" class="java.lang.String"/>
    <field name="delivered_date" class="java.lang.String"/>
    <field name="job_id" class="java.math.BigDecimal"/>
    <field name="client_facility_id" class="java.math.BigDecimal"/>
    <field name="dictation_length" class="java.math.BigDecimal"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="120" splitType="Stretch">
            <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
            <textField>
                <reportElement x="0" y="0" width="842" height="30" uuid="68bf2fd7-6d2a-4101-aa84-2f1dc73aa782"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="Tahoma" size="11" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["Report Header Section"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="50" width="842" height="20" uuid="7e08b87a-bfd3-4c97-85c2-0495253e1ee9"/>
                <textElement verticalAlignment="Middle">
                    <font fontName="Tahoma" size="10" isItalic="true"/>
                </textElement>
                <textFieldExpression><![CDATA["Time Run : "+new SimpleDateFormat("MM-dd-YYYY HH:mm:ss").format(new Date())]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="30" width="842" height="20" uuid="d6500562-18ff-4426-b997-c928ea4fac7f"/>
                <textElement textAlignment="Center">
                    <font fontName="Tahoma" size="11"/>
                </textElement>
                <textFieldExpression><![CDATA["Report Description"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="100" width="842" height="20" uuid="5ac7e312-67d6-4cb5-b140-db61aec3a23e"/>
                <box>
                    <topPen lineWidth="0.5" lineColor="#999999"/>
                </box>
                <textFieldExpression><![CDATA[""]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="70" width="422" height="30" uuid="9c55c0f6-3bdd-4a6e-89ea-c68f35afde77"/>
                <textElement textAlignment="Left" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA["Date Between "+$P{FromDateTZ} +"  and  "+$P{ToDateTZ}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="422" y="70" width="420" height="30" uuid="a3f557b3-cd79-42b6-b5b4-ab1d959edb3f"/>
            </textField>
            <textField>
                <reportElement x="422" y="70" width="280" height="30" uuid="a6716a87-4ac6-469a-9340-808348561b83"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$P{REPORT_TIME_ZONE}.toString()]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="0" width="0" height="0" uuid="8f70b2ad-ad64-4bef-8d7e-fd35dcf761dc">
                    <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <textFieldExpression><![CDATA[$P{REPORT_TIME_ZONE}.getTimeZone($P{userTimezone})]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <pageHeader>
        <band splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="30" splitType="Stretch">
            <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
            <textField>
                <reportElement style="column header" stretchType="RelativeToTallestObject" x="0" y="0" width="142" height="30" uuid="47351e64-0eed-4b3f-8e65-bbd4aa71707b"/>
                <textFieldExpression><![CDATA["transcribed_date"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="column header" stretchType="RelativeToTallestObject" x="142" y="0" width="140" height="30" uuid="5b44c994-90f9-4861-9dbd-37b0f2a4325f"/>
                <textFieldExpression><![CDATA["created_date"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="column header" stretchType="RelativeToTallestObject" x="282" y="0" width="140" height="30" uuid="118a7ea2-1a87-46ad-887b-9258ac5b39f5"/>
                <textFieldExpression><![CDATA["delivered_date"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="column header" stretchType="RelativeToTallestObject" x="422" y="0" width="140" height="30" uuid="16275c85-468f-4f0f-bc11-56741eca0c01"/>
                <textFieldExpression><![CDATA["job_id"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="column header" stretchType="RelativeToTallestObject" x="562" y="0" width="140" height="30" uuid="60ba0fee-da2a-40d1-9a15-c7dd6a699e6d"/>
                <textFieldExpression><![CDATA["client_facility_id"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="column header" stretchType="RelativeToTallestObject" x="702" y="0" width="140" height="30" uuid="7e7b7403-55d3-4414-b9a5-f55e9a4503c3"/>
                <textFieldExpression><![CDATA["dictation_length"]]></textFieldExpression>
            </textField>
        </band>
    </columnHeader>
    <detail>
        <band height="30" splitType="Stretch">
            <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
            <textField>
                <reportElement style="detail" stretchType="RelativeToTallestObject" x="0" y="0" width="142" height="30" uuid="ac7dc00e-7886-4ada-84f2-a626739b4390">
                    <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                </reportElement>
                <textFieldExpression><![CDATA[$F{transcribed_date}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="detail" stretchType="RelativeToTallestObject" x="142" y="0" width="140" height="30" uuid="58f2d24f-451d-429b-a6ba-adf3b6095093"/>
                <textFieldExpression><![CDATA[$F{created_date}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="detail" stretchType="RelativeToTallestObject" x="282" y="0" width="140" height="30" uuid="256ad4fb-2886-4368-8097-635c2991064d"/>
                <textFieldExpression><![CDATA[$F{delivered_date}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="detail" stretchType="RelativeToTallestObject" x="422" y="0" width="140" height="30" uuid="2beac647-69e2-4fb6-bbe2-9a9730038a56"/>
                <textFieldExpression><![CDATA[$F{job_id}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="detail" stretchType="RelativeToTallestObject" x="562" y="0" width="140" height="30" uuid="a88d32dd-1cc3-4bdc-9ef9-540b022377c9"/>
                <textFieldExpression><![CDATA[$F{client_facility_id}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="detail" stretchType="RelativeToTallestObject" x="702" y="0" width="140" height="30" uuid="82fb777d-e46f-448f-a4c5-dab3b9ca0b3f"/>
                <textFieldExpression><![CDATA[$F{dictation_length}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band splitType="Stretch">
            <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
        </band>
    </summary>
    <noData>
        <band height="40">
            <staticText>
                <reportElement x="0" y="0" width="842" height="40" uuid="2de331a1-b95b-4154-99f7-095e5cade32c"/>
                <textElement verticalAlignment="Middle">
                    <font fontName="Tahoma" size="11" isBold="false"/>
                </textElement>
                <text><![CDATA[No Results
    The specified criteria didn't result in any data. This is often caused by applying filters and/or selections that are too restrictive or that contain incorrect values. Please check your Analysis Filters and try again. ]]></text>
            </staticText>
        </band>
    </noData>
</jasperReport>

如果你看到这是接受三个参数的构建。两个日期字段和一个用户时区提示。根据提供的日期过滤器,我将这些过滤器转换为用户时区格式并将其传递给数据库。问题是在时区打印结果时。由于我的报告有三个日期时间字段,因此应根据用户时区显示结果。

我的问题是,我可以通过任何方式在报告顶部修复时区,而不是单独转换所有三个日期时间字段。如果您需要对此进行更多说明,请与我们联系。感谢

0 个答案:

没有答案