Thymeleaf comes with several utility objects that you can use for common
tasks such as working with dates and times, formatting numbers,
and string operations. There are several objects, but I've only
included the ones you might find useful. Feel free to explore the others
in the Thymeleaf documentation and tutorials.
<div th:text="${#dates.format(myDate, 'dd/MMM/yyy HH:mm')}">format myDate using standard
date/time format characters</div>
<div th:text="|${#dates.hour(apptTime)}: ${#dates.minute(apptTime)}|">you can retrieve
pieces of a date/time</div>
<div th:text="${#numbers.formatCurrency(inv.price)}">always formats as $xx.xx</div>
<div th:text="${#numbers.formatDecimal(eval.weight,2,1)}">2nd arg = # digits on the left;
3rd arg = # digits on the right</div>
<div th:text="${#arrays.length(bookArray)}"># elements in bookArray</div>
<div th:text="${#lists.size(bookList)}"># elements in bookList</div>
<li th:each="b : ${#lists.sort(bookList)}">iterate through a sorted list of books
(Book must implement Comparable interface)</li>
<div th:if="${#maps.containsKey(courseMap, 'prog10082')}">courseMap is a Map (like HashMap),
renders this DIV if the map contains an element with the key 'prog10082'</div>
<div th:if="${#maps.containsValue(courseMap, java3)}">courseMap is a Map (like HashMap),
renders this DIV if the map contains an element whose value is the object
referenced by the variable called 'java3'</div>
Exercises
Write the code for a paragraph element that displays the
year of a date stored in the model attribute that has the
key "today".
Write the code for any block element that only renders if
a string stored in the model attribute "itemId" contains
the value "xx". The elment should display the value
in the itemId model attribute in all upper-case letters.
Write the code that displays a table only if an array stored
in the model attribute "bookList" contains elements.
Write the code that displays an unordered list of Inventory
objects with a list item for each inventory object in a
list stored as the model attribute "inventory". The
inventory items should be displayed in sorted order (assume
the Inventory bean implements Comparable)
<p th:text="${#dates.year(today)}">year</p>
<div th:text="${#strings.toUpperCase(itemId)}"
th:if="${#strings.contains(itemId, 'xx')}"></p>
(will also work with #strings.indexOf(..) gt 0)