iReport: How to format date based on French locale

12,990

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

Dialog for setting language

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:

Result via preview in iReport

Note: It's works only for preview in iReport.

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:

The generated report in Adobe Reader

Share:
12,990
simonTifo
Author by

simonTifo

Updated on June 04, 2022

Comments

  • simonTifo
    simonTifo almost 2 years

    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?

  • simonTifo
    simonTifo over 11 years
    I tested the two way but the same result, always : Fri Sep 28 11:29:37
  • simonTifo
    simonTifo over 11 years
    now it works, thank you very much, the problem was : instead of having : <parameter name="datenow" class="java.util.Date"/> I had : <parameter name="datenow" class="java.lang.String"/>
  • Parth Patel
    Parth Patel almost 4 years
    @Alex K ,It works with a date but not with the number. can you help me? For example, my data source is csv and I have value 85.6 in CSV now I want to use it as the german way 85,6 but whenever I apply this config then it doesn't work. (it gives 8560.0). I tried ds.setNumberFormat and ds.setLocale(Locale.German) as well. (when I remove ds.setLocal and REPORT_LOCALE parameter it shows 85.6)