问题
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"/>
回答1:
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.
回答2:
T(org.apache.commons.lang.time.DateUtils).addDays(payload.startDate, 30)
回答3:
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:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html#expressions-ref-functions
来源:https://stackoverflow.com/questions/9832090/how-do-i-do-date-manipulation-in-spel