问题
I have a problem with formatting date in iReport
An my pc I configured the locale language as French but when the iReport generate the reports I find the date formated with English locale.
Here is a few code from my jrxml file :
<band height="41" splitType="Stretch">
<textField pattern="dd/MM/yyyy h.mm a">
<reportElement uuid="fb711e77-c949-4a99-9b52-109aae00c8ed" x="87" y="19" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$P{datenow}]]></textFieldExpression>
</textField>
<staticText>
<reportElement uuid="51fb76a0-829e-4c36-b474-3ff9c7d4c239" x="41" y="19" width="48" height="20"/>
<textElement>
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Fes Le : ]]></text>
</staticText>
</band>
and here is how it is displayed for me: Fri Sep 28 09:59:00
My target format is: vendredi 28 septembre 2012 09:59 (in French)
Do you have any idea?
回答1:
Your question duplicates How to change date format (month name) in iReport? and Setting REPORT_LOCALE in IReport? posts.
- For setting locale in iReport you should call dialog Options - Compilation and exectution (via iReport -> Tools -> Options menu).
For this textField:
<textField pattern="EEEEE dd MMMMM yyyy">
<reportElement x="0" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{date}]]></textFieldExpression>
</textField>
The result will be:
Note: It's works only for preview in iReport.
- For Java application you can set JRParameter.REPORT_LOCALE parameter.
Map<String, Object> params = new HashMap<String, Object>();
params.put(JRParameter.REPORT_LOCALE, Locale.FRENCH);
JasperFillManager.fillReportToFile(compiledReportName, params);
The result will be the same for report generated with code like this.
The working sample, jrxml file:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ... whenNoDataType="AllSectionsNoDetail" ...>
<parameter name="date" class="java.util.Date" isForPrompting="false">
<defaultValueExpression><![CDATA[new Date()]]></defaultValueExpression>
</parameter>
<title>
<band height="50">
<textField pattern="EEEEE dd MMMMM yyyy">
<reportElement x="200" y="11" width="228" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$P{date}]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>
Java code:
Map<String, Object> params = new HashMap<String, Object>();
params.put("date", new Date());
params.put(JRParameter.REPORT_LOCALE, Locale.FRENCH);
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, connection);
JasperExportManager.exportReportToPdfFile(jasperPrint, outputFile);
The result be:
来源:https://stackoverflow.com/questions/12636568/ireport-how-to-format-date-based-on-french-locale