首先整体环境使用 Springboot 2.7.1

添加额外依赖:

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-java8time</artifactId>
    <version>3.0.4.RELEASE</version>
</dependency>

确保代码中的时间戳是使用long数据类型,使用string是无法转换的。

使用如下代码转换

# Java 获取系统当前时间
Long standardDate = System.currentTimeMillis()
# Thymeleaf 转换时间
<span th:text="${#dates.format(standardDate, 'dd-MM-yyyy HH:mm:ss')}">

本文参考:
Formatting date in Thymeleaf
spring boot 2.0 + thymeleaf unix epoch to date
How to Work with Dates in Thymeleaf

标签: none

评论已关闭