Search code examples
freemarker

freemarker sort by date; need to change date format


hope someone can help me... i'm using freemarker and need to do the following.

i got a list that i have to sort by a field called course_start_date. this is a date field of this form: dd MM yyyy (day month year). unfortunately sorting isn't correct as i need the date in this form yyyyMMdd to run it as supposed to use for sorting by date.

the big question: how can i change the date format? i tried this but i just got an error. [#list?sort_by("veranstaltung.first_course_date".pattern('YYYYMMDD')") as veranstaltung]

hope someone can help me.

best regards, daniel


Solution

  • ?sort_by won't do such thing, it's for fairly simple cases. Generally, the data should be already sorted when the template gets it (so one may argues that ?short_by shouldn't exist at all). But at the very least, dates should be in the data model as dates (like java.util.Date or such), not as text, and then ?sort_by would sort them correctly.