Thymeleaf utility methods for Dates

August 16, 2019 No comments Thymeleaf Utility Dates

1. Introduction

Thymeleaf is an engine designed to create dynamic templates for Java-based applications. The engine provides many utility methods that can be used to build awesome websites. In this article, we will focus on Thymeleaf utility methods for java.util.Date objects used to present formatted dates and print specific date properties. The implementation of Date utility methods can be found in the official Thymeleaf GitHub Repository.

2. Available utility methods for java.util.Date objects

Thymeleaf provides several methods to format java.util.Date objects into a human-readable form. We can also create a new instance by providing date components.

Method Purpose Description
${#dates.format(date)}
${#dates.arrayFormat(datesArray)}
${#dates.listFormat(datesList)}
${#dates.setFormat(datesSet)}
Format date with the standard locale format Also works with arrays, lists or sets
${#dates.formatISO(date)}
${#dates.arrayFormatISO(datesArray)}
${#dates.listFormatISO(datesList)}
${#dates.setFormatISO(datesSet)}
Format date with the ISO8601 format Also works with arrays, lists or sets
${#dates.format(date, 'dd/MMM/yyyy HH:mm')} ${#dates.arrayFormat(datesArray, 'dd/MMM/yyyy HH:mm')} ${#dates.listFormat(datesList, 'dd/MMM/yyyy HH:mm')} ${#dates.setFormat(datesSet, 'dd/MMM/yyyy HH:mm')} Format date with the specified pattern Also works with arrays, lists or sets
${#dates.day(date)}
${#dates.month(date)}
${#dates.monthName(date)}
${#dates.monthNameShort(date)}
${#dates.year(date)}
${#dates.dayOfWeek(date)}
${#dates.dayOfWeekName(date)}
${#dates.dayOfWeekNameShort(date)}
${#dates.hour(date)}
${#dates.minute(date)}
${#dates.second(date)}
${#dates.millisecond(date)}
Obtain date properties
${#dates.create(year,month,day)}
${#dates.create(year,month,day,hour,minute)}
${#dates.create(year,month,day,hour,minute,second)}
${#dates.create(year,month,day,hour,minute,second,millisecond)}
Create date (java.util.Date) objects from its components
${#dates.createNow()}
${#dates.createNowForTimeZone()}
Create a date (java.util.Date) object for the current date and time
${#dates.createToday()}
${#dates.createTodayForTimeZone()}
Create a date (java.util.Date) object for the current date (time set to 00:00)

3. Conclusion

In this article, we presented Thymeleaf utility methods for java.util.Date objects. Formatting dates is one of the main use cases that web developers have to struggle with. This utility can ease the pain. You can use it to format date objects, get specific attributes, display the current date, and create date object using its components.

{{ message }}

{{ 'Comments are closed.' | trans }}