问题
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