Noa
Noa

Reputation: 355

Thymeleaf parse preprocessing String to date and format it

I have Model Attribute that 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 keeps failing. Then I thought I should convert String to date using custom dialect but it is complicated. Is there any simpler solution?

I tried to convert the string to date based on this question but it failed:

<p th:text="${#temporals.format(new java.util.Date(${{myData.mdProperties.get('completionDate')}}), 'dd-MM-yyyy')}"></p>

Upvotes: 2

Views: 8386

Answers (2)

Frah
Frah

Reputation: 1

this works for me, I needed to set min attribute for a input type="datetime-local", maybe it could be useful

th:attr="min=${#strings.substring(#dates.formatISO(#dates.createNow()),0,16)}"

that set: min="2022-01-06T19:47"

Upvotes: 0

Metroids
Metroids

Reputation: 20487

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>

Upvotes: 5

Related Questions