springboot3 thymeleaf日期格式化
时间: 2023-09-27 14:07:50 浏览: 228
在Spring Boot中使用Thymeleaf进行日期格式化可以通过以下步骤实现:
1. 首先,确保你的项目中已经引入了Thymeleaf和Spring Boot的相关依赖。
2. 在你的Thymeleaf模板中,使用Thymeleaf标签来显示日期。例如,如果你要显示一个日期变量`date`,可以使用`${#dates.format(date, 'yyyy-MM-dd')}`来格式化日期为"yyyy-MM-dd"的形式。
```html
<p>Date: <span th:text="${#dates.format(date, 'yyyy-MM-dd')}"></span></p>
```
3. 如果你希望在整个应用中都使用相同的日期格式,可以在Spring Boot的配置文件(如application.properties或application.yml)中设置全局日期格式化。
```yaml
spring:
thymeleaf:
mode: HTML
cache: false
servlet:
content-type: text/html
encoding: UTF-8
prefix: classpath:/templates/
suffix: .html
template-resolver-order: 1
date-format: yyyy-MM-dd
```
这样,你就可以在Thymeleaf模板中直接使用`${date}`来显示日期,它会自动按照配置的日期格式进行格式化。
请注意,以上示例中使用的日期格式仅供参考,你可以根据自己的需求自定义日期格式。还可以在Thymeleaf中使用更多的日期格式化选项,请参考Thymeleaf官方文档了解更多详情。
阅读全文
相关推荐


















