【问题标题】:iReport: How to format date based on French localeiReport:如何根据法语区域设置日期格式
【发布时间】:2012-09-28 19:35:19
【问题描述】:

我在 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法语

你有什么想法吗?

【问题讨论】:

标签: localization jasper-reports ireport date-formatting


【解决方案1】:

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


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

对于这个textField

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

结果将是:

注意:它仅适用于 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);

结果是:

【讨论】:

  • 我测试了两种方式,但结果相同,总是:Fri Sep 28 11:29:37
  • 现在可以了,非常感谢,问题是:而不是:&lt;parameter name="datenow" class="java.util.Date"/&gt; 我有:&lt;parameter name="datenow" class="java.lang.String"/&gt;
  • @Alex K ,它适用于日期但不适用于数字。你能帮助我吗?例如,我的数据源是 csv,我在 CSV 中的值为 85.6,现在我想将其用作德国方式 85,6,但每当我应用此配置时,它就不起作用。 (它给出 8560.0)。我也试过ds.setNumberFormatds.setLocale(Locale.German)。 (当我删除 ds.setLocal 和 REPORT_LOCALE 参数时,它显示 85.6)
猜你喜欢
  • 1970-01-01
  • 2015-09-16
  • 1970-01-01
  • 2012-06-27
  • 1970-01-01
  • 2011-01-22
  • 2012-10-14
  • 1970-01-01
  • 2021-12-12
相关资源
最近更新 更多