Thymeleaf 如何支持java8的时间LocalDate和LocalDatetime

一、添加依赖

    org.thymeleaf.extras

    thymeleaf-extras-java8time

    3.0.0.RELEASE

 

 

二、测试model

  对比新老时间用法,也就是java.util and java.time这两个包的时间处理差别。

model.addAttribute("standardDate", new Date()); 
model.addAttribute("localDateTime", LocalDateTime.now());
model.addAttribute("localDate", LocalDate.now());
model.addAttribute("timestamp", Instant.now());

 

三、格式化日期,使用ISO8601 format

Format ISO

 

输出:

 Thymeleaf 如何支持java8的时间LocalDate和LocalDatetime_第1张图片

四、手动指定日期格式

Format manually

 

输出:

 Thymeleaf 如何支持java8的时间LocalDate和LocalDatetime_第2张图片

 

五、获取特定日期字段和属性

java.util.Date class中使用:

${#dates.day(date)}

${#dates.month(date)}

${#dates.monthName(date)}

${#dates.monthNameShort(date)}

${#dates.year(date)}

${#dates.dayOfWeek(date)}

${#dates.dayOfWeekName(date)}

${#dates.dayOfWeekNameShort(date)}

${#dates.hour(date)}

${#dates.minute(date)}

${#dates.second(date)}

${#dates.millisecond(date)}

 

在java8的java.time中使用:

${#temporals.day(date)}

${#temporals.month(date)}

${#temporals.monthName(date)}

${#temporals.monthNameShort(date)}

${#temporals.year(date)}

${#temporals.dayOfWeek(date)}

${#temporals.dayOfWeekName(date)}

${#temporals.dayOfWeekNameShort(date)}

${#temporals.hour(date)}

${#temporals.minute(date)}

${#temporals.second(date)}

${#temporals.millisecond(date)}

 

用法:

Show only which day of a week

 

 

Show the name of the week day

 

Show the second of the day

 

 

 

 

 
  

转载于:https://www.cnblogs.com/asker009/p/9370603.html

你可能感兴趣的:(Thymeleaf 如何支持java8的时间LocalDate和LocalDatetime)