How do I do date manipulation in SpEL?
-
25-05-2021 - |
Question
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"/>
Solution
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.
OTHER TIPS
T(org.apache.commons.lang.time.DateUtils).addDays(payload.startDate, 30)
If you have the access a tidier way to do this would be by writing the date manipulation functions you need and injecting them into the SpelEvaluationContext:
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow