Date formatting part of text in Jasper / ireports
Asked Answered
J

4

19

how do I format a date field (database field) displayed part of a text in Jasper / iReports (4.5.1)

Displayed via a text field in the report... (using Groovy please)

"Sub total for this date: " + $F(DEPOSIT_DATE)

I have tried (new SimpleDateFormat("MM/dd/yyyy")).parse($F{DEPOSIT_DATE})and I am getting error message:

net.sf.jasperreports.engine.fill.JRExpressionEvalException: 
Error evaluating expression : Source text : (new SimpleDateFormat("MM/dd/yyyy")).parse($F{BANK_DATE}) 

What I want to display in my report is as follows...

Sub total for this date: MM/DD/YYYY - format...

Junko answered 4/4, 2013 at 16:37 Comment(0)
J
44

Try this:

new SimpleDateFormat("MM/dd/yyyy").format($F{BANK_DATE})
Junko answered 4/4, 2013 at 17:7 Comment(1)
Still works in the latest version of Jasper Studio as of this comment.Moonraker
T
9

I agree with Mateusz, textField with pattern perfrormes faster than new SimpleDateFormat("someFormat").format("jasperField"). It's important when you have deal with huge reports. This is my example

<textField pattern="MM/dd/yyyy" isBlankWhenNull="true">
...
    <textFieldExpression class="java.util.Date"><![CDATA[$F{certIssueDate}]]></textFieldExpression>
</textField>
Tobar answered 1/6, 2015 at 10:37 Comment(1)
This is okay if you only have 1 text field to be formatted. If you need to format a part of the text field how you would use this?Play
A
5

It seems, that you try to parse instead of formatting (as stated above).

You could also use Pattern in the textfield properties tab to pretty-print the date, or manually change the pattern in the jrxml:

<textField pattern="MM/dd/yyyy">
  <!-- here comes other generated data-->
  <textFieldExpression><![CDATA[$F{BANK_DATE}]]></textFieldExpression>
</textField>
Allay answered 11/12, 2014 at 15:32 Comment(0)
E
3

if the Date field is a String value, say : "2014-11-20"

<field name="dateField" class="java.lang.String"/>

then you can do this

<variable name="THE_DATE" class="java.util.Date">
<variableExpression>
<![CDATA[new java.text.SimpleDateFormat("yyyy-mm-dd").parse($F{dateField})]]>
</variableExpression>
</variable>

<textField pattern="dd/MM/yyyy" isBlankWhenNull="true">
    <reportElement x="0" y="0" width="88" height="20" uuid="47b41787-a8fd-44ea-bf96-7e5484e477fb"/>
    <textElement textAlignment="Left" verticalAlignment="Middle"/>
    <textFieldExpression><![CDATA[ $V{THE_DATE} ]]></textFieldExpression>
</textField>

You can set the pattern by right clicking the field -> click field pattern -> Select Date -> Choose a Date Pattern

you may also do this

<textField isBlankWhenNull="true">
    <reportElement x="0" y="0" width="88" height="20" uuid="47b41787-a8fd-44ea-bf96-7e5484e477fb"/>
    <textElement textAlignment="Left" verticalAlignment="Middle"/>
    <textFieldExpression class="java.util.Date"><![CDATA[ new java.text.SimpleDateFormat("dd/MM/yyyy").format(new java.text.SimpleDateFormat("yyyy-mm-dd").parse($F{dateField})) ]]></textFieldExpression>
</textField>

However,If the DateField is of type Date then doing the below is just fine.

<field name="dateField" class="java.util.Date"/>
<textField pattern="dd/MM/yyyy" isBlankWhenNull="true">
        <reportElement x="0" y="0" width="88" height="20" uuid="47b41787-a8fd-44ea-bf96-7e5484e477fb"/>
        <textElement textAlignment="Left" verticalAlignment="Middle"/>
        <textFieldExpression><![CDATA[ $F{dateField} ]]></textFieldExpression>
</textField>
Easter answered 14/12, 2015 at 20:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.