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 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. Buy me a coffee (cost $3)

Need help to start a new Spring Boot or MEAN stack project? I am available for contract work. Hire me to accomplish your business goals with engineering and design. Let’s talk about your project: hi@attacomsian.com.