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 @attacomsian on Twitter. You can also follow me on LinkedIn and DEV. Subscribe to RSS Feed.

👋 If you enjoy reading my articles and want to support me to continue creating free tutorials, Buy me a coffee (cost $5) .

Need help to launch a new product? I am available for contract work. Hire me to accomplish your business goals with engineering and design.