The following code returns the current year in Thymeleaf:

${#dates.year(#dates.createNow())}

How to use it?

Here is an example to create a dropdown list for selecting credit card expiration year:

<select>
    <option th:each="year : ${#numbers.sequence(#dates.year(#dates.createNow()), #dates.year(#dates.createNow()) + 7)}"
            th:text="${year}"></option>
</select>

Another example to create a dropdown list for date of birth selection starting from 1970:

<select>
    <option th:each="year : ${#numbers.sequence(1970, #dates.year(#dates.createNow()))}"
            th:value="${year}" th:text="${year}">
    </option>
</select>

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also Subscribe to RSS Feed.

Last Updated: