Jasper iReport custom date and custom time

2019-03-01 17:17发布

问题:

In Excel, I have a date format yyyy.MM.dd hh:mm and a time format hh:mm. I set this as a source for an iReport database.

For the date I set a custom date format in iReport, same as in Excel. Then I set class="java.util.Date".

For the time, which class should I choose? I've tried many, none working.

I think, the problem is, that I cannot set another custom date format, so the iReport does not recognise it.

Thank you for your help.

回答1:

It is quite slightly harder to solve this your new case than the old one described in Excel datasource in JasperReports/iReport: Unable to get value for field 'Date' of class 'java.sql.Date' post.

Solution

Datasource, data

The xls file (flights.xls) contains data like this:

The cells in Departure and Duration columns have Text format (without any patterns).

Datasource, configuration

The Excel datasource have configuration (in iReport, you can do the same with Java code):

The Date format is set as yyyy.MM.dd HH:mm for reading data from Departure column of Excel file.

It is impossible to declare several fields of java.util.Date for Excel datasource which contains Date data with different patterns.
This behavior is determined by implementation net.sf.jasperreports.engine.data.JRXlsDataSource.getFieldValue(JRField) method. You can see the source code of this class and you can change this behavior.

Template

The fields in report will be:

<field name="Flight" class="java.lang.String"/>
<field name="Departure" class="java.util.Date"/>
<field name="Duration" class="java.lang.String"/>

Note that the Duration field has java.lang.String type.

The jrxml file:

<?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="flights" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="827d6b81-9342-48fd-88b5-f9ea2cfd8bfb">
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="Flight" class="java.lang.String"/>
    <field name="Departure" class="java.util.Date"/>
    <field name="Duration" class="java.lang.String"/>
    <columnHeader>
        <band height="20" splitType="Stretch">
            <staticText>
                <reportElement uuid="ccfc5f9d-0b0f-4127-be62-3c3eefc16c5e" mode="Opaque" x="273" y="0" width="100" height="20" backcolor="#999999"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle" markup="none">
                    <font isBold="true" isItalic="true" isUnderline="false"/>
                </textElement>
                <text><![CDATA[Duration]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="5f7a483c-2c9b-456e-8d6e-3063534c1710" mode="Opaque" x="0" y="0" width="173" height="20" backcolor="#999999"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle" markup="none">
                    <font isBold="true" isItalic="true" isUnderline="false"/>
                </textElement>
                <text><![CDATA[Flight]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="45724e54-4a70-48b1-980e-acbba6266670" mode="Opaque" x="173" y="0" width="100" height="20" backcolor="#999999"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle" markup="none">
                    <font isBold="true" isItalic="true" isUnderline="false"/>
                </textElement>
                <text><![CDATA[Departure]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement uuid="a45c9487-3521-4440-8c4d-c016023dc799" x="0" y="0" width="173" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{Flight}]]></textFieldExpression>
            </textField>
            <textField pattern="dd/MM/yyyy HH:mm">
                <reportElement uuid="ad60f65d-72ba-48d3-bf3e-0d4275edabb4" x="173" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{Departure}]]></textFieldExpression>
            </textField>
            <textField isBlankWhenNull="false">
                <reportElement uuid="b20e0e58-feab-47f9-9ce8-63d89425e914" x="273" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{Duration}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

Output result

The result will be (via preview in iReport):

Notes:

  • I've used the iReport 5.1.0.
  • You can change the format of Duration field with help of java.text .DateFormat.format(Date date) method. You can the example in mysql datetime format change using java for JasperReports generation post.
  • The format defined in datasource is applied only for Departure field.