thymeleaf java8_Thymeleaf 如何支持java8的时间LocalDate和LocalDatetime

本文介绍了如何在Thymeleaf中支持Java8的时间类型LocalDate和LocalDateTime。首先需要添加thymeleaf-extras-java8time依赖,然后通过模型将Java8时间对象传递到视图。接着展示了如何使用ISO8601格式和自定义日期格式化。最后,文中列举了如何获取日期的特定字段,包括天数、月份、星期等,并对比了java.util.Date和java.time包中的不同用法。
摘要由CSDN通过智能技术生成

一、添加依赖

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

输出:

f50c922aa9d04936168f8968785dfb8e.png

四、手动指定日期格式

Format manually

输出:

d60fb32d7966bdb1983381b2e9eb23ca.png

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

在java.util.Dateclass中使用:

${#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值