例えば、Controllerの実装が以下のとき、
java
1 import java . util . Date ;
2
3 import org . springframework . stereotype . Controller ;
4 import org . springframework . web . bind . annotation . GetMapping ;
5 import org . springframework . web . bind . annotation . RequestMapping ;
6 import org . springframework . web . servlet . ModelAndView ;
7
8 @Controller
9 @RequestMapping ( "date" )
10 public class DateController {
11
12 @GetMapping
13 public ModelAndView display ( ModelAndView mnv ) {
14 mnv . addObject ( "nowDate" , new Date ( ) ) ;
15 mnv . setViewName ( "date" ) ;
16 return mnv ;
17 }
18
19 }
表示する画面がThymeleafのHTMLテンプレートであれば、noeDateをそのまま表示すると
html
1 <! DOCTYPE html >
2 < html xmlns: th = " http://www.thymeleaf.org " >
3 < head >
4 < meta charset = " UTF-8 " >
5 < title > Insert title here </ title >
6 </ head >
7 < body >
8 < div th: text = " ${nowDate} " > </ div >
9 </ body >
10 </ html >
こうなってしまう、ということでしょうか?
Tue Jul 14 20:21:41 GMT+09:00 2020
その場合は、HTMLテンプレート内で日付フォーマットを指定すれば解決します。
html
1 < div th: text = " ${#dates.format(nowDate, ' yyyy/MM/dd ' )} " > </ div >
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf_ja.html#%E6%97%A5%E4%BB%98