如何在 Thymeleaf 中使用带有语言环境的 Dates.Format [英] How to use Dates.Format with locale in Thymeleaf

查看:50
本文介绍了如何在 Thymeleaf 中使用带有语言环境的 Dates.Format的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Thymeleaf 中使用语言环境格式化日期,我已经使用了dates.format

I'm trying to format a date with locale in Thymeleaf, I already used the dates.format

<td th:text="${#dates.format(embargo.fecha, 'dd-MMMM-yyyy', new Locale('es'))}"></td>

<td th:text="${#dates.format(embargo.fecha, 'dd-MMMM-yyyy',${ new Locale('es')})}"></td>

但以上都不起作用.

我基于这个已经解决的问题https://github.com/thymeleaf/thymeleaf-extras-java8time/pull/6

I was based in this issue that is already solved https://github.com/thymeleaf/thymeleaf-extras-java8time/pull/6

推荐答案

我遇到了和你一样的问题.

I stumble upon the same problem as you.

不工作的原因是因为你需要使用 #temporals 而不是 #dates.

The reason is not working is because you need to use #temporals instead of #dates.

为此,您需要将 thymeleaf-extras-java8time 依赖项添加到您的项目中:

In order to do that, you need to add to your project the thymeleaf-extras-java8time dependency:

compile("org.thymeleaf.extras:thymeleaf-extras-java8time:3.0.4.RELEASE")

请记住,Locale 功能是在 2.1.0 版发布后添加的,因此您必须使用 Thymeleaf 3.添加:

Keep in mind that the Locale feature was added after the release of version 2.1.0 so you MUST be using Thymeleaf 3. Add:

compile("org.thymeleaf:thymeleaf-spring4:3.0.6.RELEASE")
compile("org.thymeleaf:thymeleaf:3.0.6.RELEASE")
compile("nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:2.2.2")

正如@Andreas 指出的,您需要指定整个包名,例如:

And as @Andreas pointed out, you need to specify the whole package name, for example:

<td th:text="${#temporals.format(embargo.fecha, 'dd-MMMM-yyyy', new java.util.Locale('es', 'ES'))}"></td>

另请注意,#temporals 不适用于 java.util.Dates,而它们适用于 java.time.LocalDatejava.time.LocalDateTime

Also notice that the #temporals does not work with java.util.Dates and they do with java.time.LocalDate and java.time.LocalDateTime

如果您无法更改后端以使用 java.time.LocalDate,那么解决方案是使用静态方法从您 java.util.Date 创建它of(year, month, day) 来自 LocalDate 类.

If you can not change your backend to use java.time.LocalDate a solution would be to create it from you java.util.Date, using the static method of(year, month, day) from the LocalDate class.

例如:

T(java.time.LocalDate).of(#dates.year(embargo.fecha), #dates.month(embargo.fecha), #dates.day(embargo.fecha))

把它放在你的例子中,它会变成:

Putting that inside your example, it would become:

<td th:text="${#temporals.format(T(java.time.LocalDate).of(#dates.year(embargo.fecha), #dates.month(embargo.fecha), #dates.day(embargo.fecha)), 'dd-MMMM-yyyy', new java.util.Locale('es', 'ES'))}"></td>

希望有帮助!

这篇关于如何在 Thymeleaf 中使用带有语言环境的 Dates.Format的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆