2017年9月4日月曜日

Spring BootのthymeleafでDate and Time api

Spring BootのThymeleafでDate and Time apiを使う手順

thymeleaf-extras-java8timeを依存に追加

gradleであれば下のように依存を追加する。
Thymeleaf 3.0以上の場合は、3系のバージョンを指定する。
compile 'org.thymeleaf.extras:thymeleaf-extras-java8time:2.1.0.RELEASE'

thymeleafでfomatなどを行う

temporals.formatを使ってフォーマットを行う。
<span th:text="${#temporals.format(nowDateTime, 'yyyy/MM/dd HH:mm:ss')}"></span>

その他

昔は、Java8TimeDialectをBeanとして登録しないとダメだったようですが、下のコミットの対応によりthymeleaf-extras-java8timeを依存に追加するだけでviewで利用できるようになったようです。

https://github.com/spring-projects/spring-boot/commit/c68ffa5ba059781ab2c0fb21eed7a541036e837d