问题
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...
回答1:
Try this:
new SimpleDateFormat("MM/dd/yyyy").format($F{BANK_DATE})
回答2:
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>
回答3:
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>
回答4:
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>
来源:https://stackoverflow.com/questions/15816874/date-formatting-part-of-text-in-jasper-ireports