17

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...

Alex K
  • 22,315
  • 19
  • 108
  • 236
Meeza S
  • 720
  • 2
  • 10
  • 22

4 Answers4

43

Try this:

new SimpleDateFormat("MM/dd/yyyy").format($F{BANK_DATE})
Troy Alford
  • 26,660
  • 10
  • 64
  • 82
Meeza S
  • 720
  • 2
  • 10
  • 22
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>
Valeriy Gorbatikov
  • 3,459
  • 1
  • 15
  • 9
  • 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? – Cem U May 04 '21 at 22:36
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>
MrMeszaros
  • 610
  • 11
  • 15
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>
codereal
  • 150
  • 1
  • 11