JSFのconvertDateTimeでCannot format given Object as a Dateとなった時の対応方法


- 環境
    - 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を使ったらエラーになった
```bash
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)
```

```xhtml
  <h:outputText value="#{bean.myTimestamp}" >
     <f:convertDateTime pattern="yyyy/MM/dd HH:mm" timeZone="Japan" />
  </h:outputText>
```

# 原因 : 変換対象の日付がLocalDateTimeだから

```java
    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.)](https://javaee.github.io/glassfish/doc/5.0/vdldoc/f/convertDateTime.html)

```xhtml:こうするとうまくいく
  <h:outputText value="#{bean.myTimestamp}" >
     <f:convertDateTime pattern="yyyy/MM/dd HH:mm" timeZone="Japan" type="localDateTime" />
  </h:outputText>
```

なんか注意されるのは気になる・・・
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/159761/82666f01-3951-4a62-6f98-d438f9f81bf6.png)


## 他の対応
やってないけどいつかのために

- 独自のConverterを作成する
    - [java - f:convertDateTime support for Java8 LocalDate / LocalDateTime? - Stack Overflow](https://stackoverflow.com/questions/30395398/fconvertdatetime-support-for-java8-localdate-localdatetime)
- LocalDateTimeをDateに変換して使う
    - [Java8 LocalDateTimeと旧Dateの相互変換 - 気軽に楽しくプログラムと遊ぶ](http://tamata78.hatenablog.com/entry/2017/06/21/081512)
- LocalDateTimeを使うのをやめる(古い世界に閉じこもる)