Java 在 Thymeleaf 中格式化日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39860643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:30:29  来源:igfitidea点击:

Formatting date in Thymeleaf

javaspring-bootthymeleafdatetime-format

提问by adam.shaleen

I'm brand new to Java/Spring/Thymeleaf so please bear with my current level of understanding. I did review this similar question, but wasn't able to solve my problem.

我是 Java/Spring/Thymeleaf 的新手,所以请忍受我目前的理解水平。我确实查看了这个类似的问题,但无法解决我的问题。

I'm trying to get a simplified date instead of the long date format.

我试图获得一个简化的日期而不是长日期格式。

// DateTimeFormat annotation on the method that's calling the DB to get date.
@DateTimeFormat(pattern="dd-MMM-YYYY")
public Date getReleaseDate() {
    return releaseDate;
}

? html:

? html:

<table>
    <tr th:each="sprint : ${sprints}">
        <td th:text="${sprint.name}"></td>
        <td th:text="${sprint.releaseDate}"></td>
    </tr>
</table>

Current output

电流输出

sprint1 2016-10-04 14:10:42.183

采纳答案by DimaSan

Bean validation doesn't matter, you should use Thymeleaf formatting:

Bean 验证无关紧要,您应该使用 Thymeleaf 格式:

<td th:text="${#dates.format(sprint.releaseDate, 'dd-MMM-yyyy')}"></td>

Also make sure your releaseDateproperty is java.util.Date.

还要确保您的releaseDate财产是java.util.Date.

Output will be like: 04-Oct-2016

输出将类似于: 04-Oct-2016

回答by Metroids

If you want to use converters in th:text attributes, you have to use double-bracket syntax.

如果要在 th:text 属性中使用转换器,则必须使用双括号语法。

<td th:text="${{sprint.releaseDate}}"></td>

(They are automatically applied to th:field attributes)

(它们会自动应用于 th:field 属性)

http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#double-bracket-syntax

http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#double-bracket-syntax

回答by Amit

you should use Thymeleaf formatting milliseconds

你应该使用 Thymeleaf 格式化毫秒

<td th:text="${#dates.format(new java.util.Date(transaction.documentDate), 'dd-MMM-yy')}"></td>

回答by David Gonzalez

If you want show por example = 20-11-2017

如果你想显示 por 示例 = 20-11-2017

You can use:

您可以使用:

 th:text="${#temporals.format(notice.date,'dd-MM-yyyy')}

回答by Adam111p

th:text="${#calendars.format(store.someDate(),'dd MMMM yyyy')}"

th:text="${#calendars.format(store.someDate(),'dd MMMM yyyy')}"

API : https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#calendars

API : https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#calendars