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

断了今生、忘了曾经 提交于 2019-11-30 08:52:57

问题


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_FORMAT(CURRENT_DATE,'%d-%m-%Y') AS currentDate, Upper(e.name) AS name , e.address, Upper(ea.comName) AS comName blablablabla.....

and currentDate field will show 28-12-2011

What the requirement is change the month (12) to "Disember" not "December". Or if the month is 1 so in the report should be "Januari".

The Month names are [Januari, Februari, Mac, April, Mei, Jun, Julai, Ogos, September, Oktober, November, Disember].

Can I do these condition in iReport?

Thanks in advance.


回答1:


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.



回答2:


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


来源:https://stackoverflow.com/questions/8653424/how-to-change-date-format-month-name-in-ireport

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!