Search code examples
javacalendarspring-el

Create Calendar object using SpEL


Given a java.util.Date(), how do you create a Calendar object using the Spring Expression Language?

This one works: <property name="calendarObject" value="#{new java.util.GregorianCalendar()}"/>

but I need to feed the day, month, and year to its constructor from the java.util.Date() date that I have. I would like to use the java.util.Date().getDay() method but apparently it's already deprecated.

I tried using the Calendar.setTime() method but it doesn't work since its return type is void.


Solution

  • Calendar is not expression-friendly.

    You can use apache commons lang3 DateUtils

    "#{T(org.apache.commons.lang3.time.DateUtils).toCalendar(myDate)}"
    

    (where myDate is a Date bean) or write your own helper class.