簡體   English   中英

從字符串更改日期格式

[英]To change date format from string

我在gwt框架中使用了datebox.datebox的值在字段t3中作為字符串存儲在數據庫中。 當我打印碧玉報告時,t3的值“ 11/02/2014”顯示為“ 11022014”。 而且,我使用了這種格式,但是有一些錯誤。

<?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="RosterReport" pageWidth="700" pageHeight="595" orientation="Landscape" columnWidth="660" 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"/>
    <field name="t1" class="java.lang.String"/>
    <field name="t2" class="java.lang.String"/>
    <field name="t3" class="java.lang.String"/>
    <field name="t4" class="java.lang.String"/>
    <field name="t5" class="java.lang.String"/>
    <variable name="date1" class="java.util.Date">
        <variableExpression><![CDATA[new SimpleDateFormat("MM/dd/yyyy").format($F{t3})]]></variableExpression>
    </variable>
    <variable name="date2" class="java.lang.String">
        <variableExpression><![CDATA[new SimpleDateFormat("MM/dd/yyyy").format($F{t4})]]></variableExpression>
    </variable>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="58" splitType="Stretch">
            <staticText>
                <reportElement x="247" y="17" width="153" height="39"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="12" isBold="true"/>
                </textElement>
                <text><![CDATA[Report for Roster]]></text>
            </staticText>
            <staticText>
                <reportElement x="530" y="0" width="43" height="20"/>
                <textElement/>
                <text><![CDATA[Printed;]]></text>
            </staticText>
            <textField>
                <reportElement x="573" y="0" width="87" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <pageHeader>
        <band height="6" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="22" splitType="Stretch">
            <staticText>
                <reportElement x="115" y="0" width="81" height="20"/>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Code]]></text>
            </staticText>
            <staticText>
                <reportElement x="196" y="0" width="100" height="20"/>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Description]]></text>
            </staticText>
            <staticText>
                <reportElement x="296" y="0" width="73" height="20"/>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Start Date]]></text>
            </staticText>
            <staticText>
                <reportElement x="369" y="0" width="78" height="20"/>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[End Date]]></text>
            </staticText>
            <staticText>
                <reportElement x="447" y="0" width="100" height="20"/>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Remark]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="34" splitType="Stretch">
            <textField>
                <reportElement x="196" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{t2}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="447" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{t5}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="115" y="0" width="81" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{t1}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="300" y="0" width="69" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$V{date1}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="369" y="0" width="78" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$V{date2}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="14" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="9" splitType="Stretch"/>
    </pageFooter>
</jasperReport>

錯誤是

Source text : new SimpleDateFormat("MM/dd/yyyy").format($F{t3})
Caused by: java.lang.IllegalArgumentException: Cannot format given Object as a Date

如何解決這個錯誤?

format()方法將Date作為參數,並且您正在傳遞字符串。

更改:

new SimpleDateFormat("MM/dd/yyyy").format($F{t3})

至:

new SimpleDateFormat("MM/dd/yyyy").format(new SimpleDateFormat("ddMMyyyy").parse($F{t3}))

這樣,首先將給定的字符串解析為Date。

問題出在您的$ F {t3}中,它給“ 02112014”(例如date)和SimpleDateFormat無法格式化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM