iReport:如何根据法语区域设置格式化日期

时间:2012-09-28 08:43:09

标签: localization jasper-reports ireport date-formatting

我在 iReport

中格式化日期时遇到问题

我的电脑我将语言环境语言配置为法语但是当 iReport 生成报告时,我发现使用英语语言环境格式化日期。

以下是我的 jrxml 文件中的一些代码:

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

以下是我的显示方式: Fri Sep 28 09:59:00

我的目标格式为: vendredi 28 septembre 2012 09:59 法语

你有什么想法吗?

1 个答案:

答案 0 :(得分:3)

您的问题会重复How to change date format (month name) in iReport?Setting REPORT_LOCALE in IReport?个帖子。


  • 要在 iReport 中设置区域设置,您应该调用对话框选项 - 编译和执行(通过 iReport - &gt; 工具 - &gt; 选项菜单)。

Dialog for setting language

对于此 textField

<textField pattern="EEEEE dd MMMMM yyyy">
    <reportElement x="0" y="0" width="100" height="20"/>
    <textElement/>
    <textFieldExpression><![CDATA[$F{date}]]></textFieldExpression>
</textField>

结果将是:

Result via preview in iReport

注意:它仅适用于 iReport 中的预览。

Map<String, Object> params = new HashMap<String, Object>(); 
params.put(JRParameter.REPORT_LOCALE, Locale.FRENCH); 
JasperFillManager.fillReportToFile(compiledReportName, params); 

对于使用此类代码生成的报告,结果将相同。


工作样本 jrxml 文件:

<?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代码:

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 generated report in Adobe Reader