如何使用模式格式化java.time.LocalDateTime和java.time.LocalDate? [英] How to format java.time.LocalDateTime and java.time.LocalDate with pattern?
问题描述
在下面的剪辑中,属性 $ F
属于类 java.time.LocalDateTime
或 java.time.LocalDate
。
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>
如何使用textField pattern
在jasper报告中?
How can I format this property with textField pattern
in jasper reports?
推荐答案
在当前版本的jasper-report中使用模式属性您需要的日期/时间对象 java.util.Date
类或其中一个子类。
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.
解决方案是转换 java.time.LocalDate
和 java.time.LocalDateTime
<textField pattern="EE. dd.MM.yyyy">
<reportElement...>
</reportElement>
<textFieldExpression><![CDATA[java.util.Date.from($F{theLocalDate}.atStartOfDay(java.time.ZoneId.systemDefault()).toInstant())]]></textFieldExpression>
</textField>
<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
来自 java.time.LocalDate
Converting to 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>
来自 java.time.LocalDateTime
from java.time.LocalDateTime
<textField pattern="EE. dd.MM.yyyy">
<reportElement...>
</reportElement>
<textFieldExpression><![CDATA[java.sql.Timestamp.valueOf($F{theLocalDateTime})]]></textFieldExpression>
</textField>
注意:应用模式总是更可取解决方案,特别是当
导出到excel时,因为正确的类将被传递给poi(因此
excel会将列识别为日期并应用与
模式相同的格式)
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)
这篇关于如何使用模式格式化java.time.LocalDateTime和java.time.LocalDate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!