- 環境
- Windows10 64bit バージョン1909
- openjdk 11 2018-09-25
- Eclipse IDE for Enterprise Java Developers Version: 2020-03 (4.15.0)
- JSF 2.3.9
事象 : f:convertDateTimeを使ったらエラーになった
2020-07-20 09:49:54:387 X-TrackingId:6079d04e-6554-492e-b2be-e01eed06 ERROR MyExceptionHandler.handle:63 Cannot format given Object as a Date
java.lang.IllegalArgumentException: Cannot format given Object as a Date
at java.base/java.text.DateFormat.format(DateFormat.java:338)
at java.base/java.text.Format.format(Format.java:158)
at javax.faces.convert.DateTimeConverter$FormatWrapper.format(DateTimeConverter.java:495)
at javax.faces.convert.DateTimeConverter$FormatWrapper.access$300(DateTimeConverter.java:470)
at javax.faces.convert.DateTimeConverter.getAsString(DateTimeConverter.java:544)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getFormattedValue(HtmlBasicRenderer.java:491)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getFormattedValue(HtmlBasicRenderer.java:509)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:330)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:143)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:595)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1654)
at com.sun.faces.facelets.component.RepeatRenderer.encodeChildren(RepeatRenderer.java:64)
at com.sun.faces.facelets.component.UIRepeat.process(UIRepeat.java:559)
at com.sun.faces.facelets.component.UIRepeat.encodeChildren(UIRepeat.java:1068)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:278)
at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:90)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:566)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1647)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:152)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:566)
<h:outputText value="#{bean.myTimestamp}" >
<f:convertDateTime pattern="yyyy/MM/dd HH:mm" timeZone="Japan" />
</h:outputText>
原因 : 変換対象の日付がLocalDateTimeだから
bean.setMyTimestamp(LocalDateTime.now());
対応 : typeに「localDateTime」を指定する
Specifies what contents the string value will be formatted to include, or parsed expecting. Valid values are "date", "time", "both", "localDate", "localDateTime", "localTime", "offsetTime", "offsetDateTime", and "zonedDateTime".
convertDateTime (JSF 2.3 View Declaration Language: Facelets Variant, generated with VDLDoc.)
こうするとうまくいく
<h:outputText value="#{bean.myTimestamp}" >
<f:convertDateTime pattern="yyyy/MM/dd HH:mm" timeZone="Japan" type="localDateTime" />
</h:outputText>
他の対応
やってないけどいつかのために
- 独自のConverterを作成する
- LocalDateTimeをDateに変換して使う
- LocalDateTimeを使うのをやめる(古い世界に閉じこもる)