2 回答
TA贡献1828条经验 获得超3个赞
是的,你可以在 thymeleaf 中设置它,但它非常冗长......这对我有用:
<th:block th:with="clazz=${T(java.time.format.DateTimeFormatter)},
style=${T(java.time.format.FormatStyle).SHORT},
zone=${T(java.time.ZoneId).systemDefault()},
formatter=${clazz.ofLocalizedDateTime(style).withLocale(#locale).withZone(zone)}">
<span th:text="${formatter.format(work.indexTime)}" />
</th:block>
您还可以添加一个从 Instant 到 String 的默认转换器,并在输出 Instant 时使用双括号语法:
语境:
public class Context extends WebMvcConfigurerAdapter {
@Override
public void addFormatters(FormatterRegistry r) {
DateTimeFormatter dateFormatter = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.SHORT)
.withLocale(LocaleContextHolder.getLocale())
.withZone(ZoneId.systemDefault());
r.addConverter(new Converter<Instant, String>() {
@Override
public String convert(Instant s) {
return s != null ? dateFormatter.format(s) : "";
}
});
}
}
HTML:
<div th:text="${{work.indexTime}}" />
TA贡献1851条经验 获得超4个赞
您可以简单地指定SHORT
为格式。
<div th:text="${#temporals.format(work.indexTime, 'SHORT')}"></div>
从自述文件:
/* * Format date with the specified pattern * SHORT, MEDIUM, LONG and FULL can also be specified to used the default java.time.format.FormatStyle patterns * Also works with arrays, lists or sets */
添加回答
举报