Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
783 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...