Adam Adam - 2 months ago 23x
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"/>


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.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.