Thymeleaf parse preprocessing String to date and format it

泄露秘密 提交于 2020-02-07 02:25:30

问题


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>

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>

any suggestion here?

thanks in advanced


回答1:


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>


来源:https://stackoverflow.com/questions/52149703/thymeleaf-parse-preprocessing-string-to-date-and-format-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!