Thymeleaf utility methods for Calendar

January 04, 2020 No comments Thymeleaf Utility Calendar

1. Introduction

Thymeleaf is a widely-used template engine designed for Java applications. The engine comes with awesome helpers that could be helpful in data manipulation. In this article, we will present utility methods for java.util.Calendar object. The implementation of Calendar utility methods can be found in the official Thymeleaf GitHub Repository.

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

Thymeleaf provides several methods to manipulate java.util.Calendar object directly on the template. Calendar method are very similar to those available for java.util.Date object. You can go and check Thymeleaf utility methods for Dates.

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

3. Conclusion

In this article, we presented Thymeleaf utility methods for java.util.Calendar objects. Methods can be used to format dates and creating a new instance of a Date object directly on the template. Utility methods for Calendar are very similar to methods for Date objects. Use them accordingly to your needs.

{{ message }}

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