Adam Adam - 3 months ago 40
Java Question

How do I do date manipulation in SpEL?

How can I do date manipulation in the Spring Expression language?

<si:service-activator id="entryReader" expression="@blogEntryReader.getEntriesBetweenDates(payload.startDate, payload.startDate **PLUS 30 DAYS**)" input-channel="blogEntryReaderChannel"/>

Answer

Unfortunately, the java.util.Calendar doesn't have a builder API so it's not SpEL-friendly. One solution would be to use a helper class...

public static class CalendarManip {

    public static Date addDays(Date date, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_YEAR, 30);
        return cal.getTime();
    }
}

Then, in SpEL...

T(foo.CalendarManip).addDays(payload.startDate, 30)

You could also use a <int-groovy:script/> if you don't want a helper class.

Comments