How to change date format (month name) in iReport?

前端 未结 2 1593
醉话见心
醉话见心 2020-12-18 14:15

I\'ve a requirement to change the date format in iReport. Currently I used an SQL query to collect the data. Here\'s my query in iReport

SELECT DATE_F

相关标签:
2条回答
  • 2020-12-18 14:39

    To display dates in different formats, one option is to format dates based on locales. For example, the following expression uses the current user's locale:

    DateFormat.getDateInstance(DateFormat.LONG, $P{REPORT_LOCALE}).format($F{currentDate})
    
    0 讨论(0)
  • 2020-12-18 14:51

    You can create and use scriptlet or you can use expressions like in this samples:

    • Using national locale (ms_MY, malaysia):
    <field name="currentDate" class="java.sql.Timestamp"/>
    ...
    <textField>
        <reportElement x="300" y="0" width="100" height="20"/>
        <textElement/>
        <textFieldExpression><![CDATA[(new DateFormatSymbols(new Locale("ms", "MY")).getMonths())[$F{currentDate}.getMonth()]]]></textFieldExpression>
    </textField>
    
    • Using conditional ? : operator
    <field name="currentDate" class="java.sql.Timestamp"/>
    ...
    <textField>
        <reportElement x="200" y="0" width="100" height="20"/>
        <textElement/>
        <textFieldExpression><![CDATA[$F{currentDate}.getMonth() == 0 ?
        "Januari" : $F{currentDate}.getMonth() == 1 ?
        "Februari" : $F{currentDate}.getMonth() == 2 ?
        "Mac" : $F{currentDate}.getMonth() == 3 ?
        "April" : $F{currentDate}.getMonth() == 4 ?
        "Mei" : $F{currentDate}.getMonth() == 5 ?
        "Jun" : $F{currentDate}.getMonth() == 6 ?
        "Julai" : $F{currentDate}.getMonth() == 7 ?
        "Ogos" : $F{currentDate}.getMonth() == 8 ?
        "September" : $F{currentDate}.getMonth() == 9 ?
        "Oktober"  : $F{currentDate}.getMonth() == 10 ?
        "November"  : $F{currentDate}.getMonth() == 11 ?
        "Disember" : "Unknown"
        ]]></textFieldExpression>
    </textField>
    
    • You can read this article how to use scriptlet.
    0 讨论(0)
提交回复
热议问题