In the following snipped the property $F
is of class java.time.LocalDateTime
or java.time.LocalDate
.
<textField pattern="EE. dd.MM.yyyy">
<reportElement...>
</reportElement>
<textFieldExpression><![CDATA[$F{theLocalDateTime}]]></textFieldExpression>
</textField>
How can I format this property with textField pattern
in jasper reports?
To use the pattern attribute in current version of jasper-report for Date/Time object you need a java.util.Date
class or one of it's subclasses.
The solution is to convert java.time.LocalDate
and java.time.LocalDateTime
java.util.Date
from java.time.LocalDate
<textField pattern="EE. dd.MM.yyyy">
<reportElement...>
</reportElement>
<textFieldExpression><![CDATA[java.util.Date.from($F{theLocalDate}.atStartOfDay(java.time.ZoneId.systemDefault()).toInstant())]]></textFieldExpression>
</textField>
from java.time.LocalDateTime
<textField pattern="EE. dd.MM.yyyy">
<reportElement...>
</reportElement>
<textFieldExpression><![CDATA[java.util.Date.from($F{theLocalDateTime}.atZone(java.time.ZoneId.systemDefault()).toInstant())]]></textFieldExpression>
</textField>
java.sql.Timestamp
from java.time.LocalDate
<textField pattern="EE. dd.MM.yyyy">
<reportElement...>
</reportElement>
<textFieldExpression><![CDATA[java.sql.Timestamp.valueOf($F{theLocalDate}.atStartOfDay())]]></textFieldExpression>
</textField>
from java.time.LocalDateTime
<textField pattern="EE. dd.MM.yyyy">
<reportElement...>
</reportElement>
<textFieldExpression><![CDATA[java.sql.Timestamp.valueOf($F{theLocalDateTime})]]></textFieldExpression>
</textField>
Note: Applying pattern is always preferable solution, specially when exporting to excel since correct class will be passed to poi (hence excel will recognize column as a date and apply same formatting as in pattern)
As java.time
module is kind of complex and verbose, I usually create some variables which holds the compiled DateTimeFormatter for further use.
I want my reports to adapt to any Locale, so I don't use string literals for formatting.
I am using the report Locale, but you can choose your Locale with java.util.Locale.forLanguageTag("en-US")
, for example.
You can also change the java.time.format.FormatStyle
if needed
LocalDate formatter:
<variable name="dateFormatter" class="java.time.format.DateTimeFormatter">
<variableExpression><![CDATA[java.time.format.DateTimeFormatter
.ofLocalizedDate(java.time.format.FormatStyle.SHORT)
.withLocale($P{REPORT_LOCALE})
.withChronology(java.time.chrono.Chronology.ofLocale($P{REPORT_LOCALE}))]]></variableExpression>
</variable>
LocalDateTime formatter:
<variable name="dateTimeFormatter" class="java.time.format.DateTimeFormatter">
<variableExpression><![CDATA[java.time.format.DateTimeFormatter
.ofLocalizedDateTime(java.time.format.FormatStyle.SHORT)
.withLocale($P{REPORT_LOCALE})
.withChronology(java.time.chrono.Chronology.ofLocale($P{REPORT_LOCALE}))]]></variableExpression>
</variable>
Now I can format a LocalDate field this way:
$F{localDateField}.format($V{dateFormatter})
And a LocalDateTime field this way:
$F{localDateTimeField}.format($V{dateTimeFormatter})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With