How do I do date manipulation in SpEL?

亡梦爱人 提交于 2021-01-28 09:31:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!