Thymeleaf将预处理字符串解析为日期并将其格式化 [英] Thymeleaf parse preprocessing String to date and format it
问题描述
我的模型属性包含属性列表
,我要格式化的值类似于此字符串2012-07-16T00:00:00
我尝试使用
I have Model Attribute contains a list of properties the value which I want to format is similar to this String 2012-07-16T00:00:00 I try to use
<p th:text="${#temporals.format(${myData.mdProperties.get('completionDate')}, 'dd-MM-yyyy')}"></p>
解析一直失败,然后我想我应该使用自定义方言将String转换为日期,但是那里很复杂任何更简单的解决方案
the parsing keep failing then I thought I should convert String to date using custom dialect but it complicated is there any simpler solution
即使我尝试根据此问题,但失败了
even I try to convert the string to date is based on this question but it failed
<p th:text="${#temporals.format(new java.util.Date(${{myData.mdProperties.get('completionDate')}}), 'dd-MM-yyyy')}"></p>
这里有什么建议吗?
感谢进阶
推荐答案
每个方括号都太多你的表情通常,您永远都不应嵌套 $ {...}
表达式(在进行预处理时例外)。
You have too many brackets in each of your expressions. In general, you should never have nested ${ ... }
expressions (excpept when doing preprocessing).
另外,您需要创建一种简单的日期格式才能首先解析您的日期。这适用于我的测试用例:
Also, you'll need to create a simple date format to first parse your dates. This worked for my test case:
<th:block th:with="sdf = ${new java.text.SimpleDateFormat('yyyy-MM-dd''T''HH:mm:ss')}">
<p th:text="${#dates.format(sdf.parse(myData.mdProperties.get('completionDate')), 'dd-MM-yyyy')}" />
</th:block>
这篇关于Thymeleaf将预处理字符串解析为日期并将其格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!