我需要在包含日期的MVEL中计算一个表达式。基本上,--我需要向给定的日期添加一定的天数并得到值.--当我试图在MVEL中计算表达式时,得到一些异常。
这是我的代码:
package Mvel;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.mvel2.MVEL;
import org.mvel2.integration.VariableResolverFactory;
import org.mvel2.integration.impl.MapVariableResolverFactory;
public class Mveldatetest {
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub
Map<String, Object> m1 = new HashMap<String, Object>();
m1.put("name", "xyz");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date d1 = sdf.parse("02/10/2014");
m1.put("doj", d1);
//Date d2=sdf.parse("05/10/2014");
System.out.println("Given Date"+" "+d1);
final Calendar c = Calendar.getInstance();
c.setTime(d1);
System.out.println(c.getTime());
Date finaldate=(Date) MVEL.eval("c.add(Calendar.DAY_OF_MONTH, 4)",m1);
System.out.println(finaldate);
}
}我得到了以下例外:
Exception in thread "main" [Error: unresolvable property or identifier: c]
[Near : {... c.add(Calendar.DAY_OF_MONTH, 4 ....}]
^
[Line: 1, Column: 1]
at org.mvel2.PropertyAccessor.getBeanProperty(PropertyAccessor.java:677)
at org.mvel2.PropertyAccessor.getNormal(PropertyAccessor.java:179)
at org.mvel2.PropertyAccessor.get(PropertyAccessor.java:146)
at org.mvel2.PropertyAccessor.get(PropertyAccessor.java:126)
at org.mvel2.ast.ASTNode.getReducedValue(ASTNode.java:187)
at org.mvel2.MVELInterpretedRuntime.parseAndExecuteInterpreted(MVELInterpretedRuntime.java:106)
at org.mvel2.MVELInterpretedRuntime.parse(MVELInterpretedRuntime.java:49)
at org.mvel2.MVEL.eval(MVEL.java:165)
at Mvel.Mveldatetest.main(Mveldatetest.java:31)发布于 2014-10-13 12:52:55
必须将c添加到上下文m1中。而且,Calender也是未知的,但是您可以只使用c (丑陋,但很有用)。最后,请注意,add返回void,即它在原地修改c。试试这个:
System.out.println(c.getTime());
m1.put("c", c);
MVEL.eval("c.add(c.DAY_OF_MONTH, 4)", m1);
System.out.println(c.getTime());输出:
Thu Oct 02 00:00:00 CEST 2014
Mon Oct 06 00:00:00 CEST 2014https://stackoverflow.com/questions/26340300
复制相似问题