使用java进行JasperReports生成的mysql datetime格式更改 [英] mysql datetime format change using java for JasperReports generation

查看:137
本文介绍了使用java进行JasperReports生成的mysql datetime格式更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JasperReport的 iReport 4.5.0 设计器来构建我的报告。我遇到时间戳字段 startDate endDate 的问题。在我的设计师中,我使用模式更改了 startDate endDate 的格式。

I am using JasperReport's iReport 4.5.0 designer to build my report. I am having one problem with the timestamp fields startDate and endDate. In my designer I changed the format of startDate and endDate using pattern.

但是,问题是,当我将报告导出为.excel或.pdf时,它仍以时间戳格式提供日期,不是格式 YYYY-MM -dd 我已完成模式。

But, the problem is that when I export the report as .excel or .pdf it still gives me date in timestamp format, not the format YYYY-MM-ddI had done in pattern.

我的报告使用starDate生成 2012-03-01 00:00:00.0 但我希望我的Excel报告在 2012-03-01 中有starDate,例如 YYYY-MM-dd

my report generated with starDate as 2012-03-01 00:00:00.0 but I want my excel report to be have starDate in 2012-03-01 like YYYY-MM-dd.

你有任何解决方案,我不想将我的数据库类型从datetime更改为日期。

我的projectmain.jrxml文件代码如下:

My projectmain.jrxml file code is below mentioned:

<?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="projectmain" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <import value="java.util.Date"/>
    <parameter name="pagelimit" class="java.lang.Integer">
        <defaultValueExpression><![CDATA[]]></defaultValueExpression>
    </parameter>
    <parameter name="pagestart" class="java.lang.Integer">
        <defaultValueExpression><![CDATA[]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[select * from project limit $P{pagestart},$P{pagelimit}]]>
    </queryString>
    <field name="projectid" class="java.lang.Integer"/>
    <field name="enddate" class="java.sql.Timestamp"/>
    <field name="projectdesc" class="java.lang.String"/>
    <field name="projectname" class="java.lang.String"/>
    <field name="projecttitle" class="java.lang.String"/>
    <field name="startdate" class="java.sql.Timestamp"/>
    <background>
        <band/>
    </background>
    <title>
        <band height="56">
            <staticText>
                <reportElement x="0" y="0" width="410" height="56"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="Century" size="24"/>
                </textElement>
                <text><![CDATA[Project Management Report Generation]]></text>
            </staticText>
        </band>
    </title>
    <pageHeader>
        <band height="28">
            <staticText>
                <reportElement mode="Opaque" x="0" y="0" width="555" height="28" forecolor="#FFFFFF" backcolor="#666666"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="Century" size="16" isBold="true"/>
                </textElement>
                <text><![CDATA[Project Management Report Generation]]></text>
            </staticText>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="29">
            <staticText>
                <reportElement mode="Opaque" x="0" y="0" width="144" height="29" backcolor="#CCCCCC"/>
                <box>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement verticalAlignment="Middle">
                    <font fontName="Century" size="16"/>
                </textElement>
                <text><![CDATA[Project Title]]></text>
            </staticText>
            <staticText>
                <reportElement mode="Opaque" x="144" y="0" width="158" height="29" backcolor="#CCCCCC"/>
                <box>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement verticalAlignment="Middle">
                    <font fontName="Century" size="16"/>
                </textElement>
                <text><![CDATA[Project Name]]></text>
            </staticText>
            <staticText>
                <reportElement mode="Opaque" x="302" y="0" width="151" height="29" backcolor="#CCCCCC"/>
                <box>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement verticalAlignment="Middle">
                    <font fontName="Century" size="16" isUnderline="false"/>
                </textElement>
                <text><![CDATA[Start Date]]></text>
            </staticText>
            <staticText>
                <reportElement mode="Opaque" x="453" y="0" width="102" height="29" backcolor="#CCCCCC"/>
                <box>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement verticalAlignment="Middle">
                    <font fontName="Century" size="16"/>
                </textElement>
                <text><![CDATA[End Date]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="29">
            <textField>
                <reportElement x="0" y="0" width="144" height="29"/>
                <box>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement verticalAlignment="Bottom">
                    <font fontName="Century" size="12" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{projecttitle}]]></textFieldExpression>
            </textField>
            <textField pattern="d/M/yyyy">
                <reportElement x="453" y="0" width="102" height="29"/>
                <box>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement verticalAlignment="Bottom">
                    <font fontName="Century" size="12" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{enddate}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="144" y="0" width="158" height="29"/>
                <box>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement verticalAlignment="Bottom">
                    <font fontName="Century" size="12" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{projectname}]]></textFieldExpression>
            </textField>
            <textField pattern="d/M/yyyy">
                <reportElement x="302" y="0" width="151" height="29"/>
                <box>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement verticalAlignment="Bottom">
                    <font fontName="Century" size="12" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{startdate}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="60">
            <textField pattern="MMMMM dd, yyyy">
                <reportElement mode="Opaque" x="0" y="20" width="555" height="20" forecolor="#000000" backcolor="#CCCCCC"/>
                <textElement verticalAlignment="Middle">
                    <font fontName="Calibri" size="14" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
            </textField>
        </band>
    </columnFooter>
    <pageFooter>
        <band height="31">
            <staticText>
                <reportElement x="0" y="1" width="541" height="30"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="Century" size="10" isBold="false"/>
                </textElement>
                <text><![CDATA[Copyright © Project Management 2012 | Design by Kintudesigns.com]]></text>
            </staticText>
        </band>
    </pageFooter>
    <summary>
        <band height="42"/>
    </summary>
</jasperReport>


推荐答案

您可以尝试设置 net.sf.jasperreports.export .xls.detect.cell.type 属性,以便在导出为Excel格式时正确格式化。

You can try to set net.sf.jasperreports.export.xls.detect.cell.type property for correct formatting in case export to Excel format.

此问题非常奇怪。您可以添加 java.text.DateFormat 类型的变量,并在此示例中使用它:

This issue is very strange. You can add the variable of java.text.DateFormat type and use it like in this sample:

<?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="datetime_format" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <property name="net.sf.jasperreports.export.xls.detect.cell.type" value="true"/>
    <queryString>
        <![CDATA[SELECT date1, date2 FROM table]]>
    </queryString>
    <field name="date1" class="java.sql.Timestamp"/>
    <field name="date2" class="java.sql.Timestamp"/>
    <variable name="dateFormat" class="java.text.DateFormat" resetType="None">
        <variableExpression><![CDATA[new SimpleDateFormat("yyyy-MM-dd")]]></variableExpression>
    </variable>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement x="11" y="0" width="194" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$V{dateFormat}.format($F{date1})]]></textFieldExpression>
            </textField>
            <textField pattern="yyyy-MM-dd">
                <reportElement x="205" y="0" width="272" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{date2}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

这对我有用。对于PDF和XLS,我有两个textFields的正确数据(使用 DateFormat 而不使用它)。

It is works for me. I have the correct data for both textFields (with using DateFormat and without it) for PDF and XLS.

这篇关于使用java进行JasperReports生成的mysql datetime格式更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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