The following code returns the current year in Thymeleaf:


How to use it?

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

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

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

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

✌️ 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: