【发布时间】:2013-04-10 14:30:35
【问题描述】:
我的脚本从文件中读取一个字符串并对其进行评估以作为 TimeCategory 中的 TimeDuration。如果字符串以“-”开头,则会引发异常。
我的简化脚本:
import groovy.time.TimeCategory;
String string = "-1.day"
Date now = new Date()
use(TimeCategory)
{
def x = new Date() + evaluate(string)
println x
}
投掷:
groovy.lang.MissingMethodException: No signature of method: groovy.time.Duration.negative() is applicable for argument types: () values: []
Possible solutions: notify()
目前我的解决方案是这样的:
import groovy.time.TimeCategory;
String string = "-1.day"
Date now = new Date()
use(TimeCategory)
{
def x
if(string.startsWith("-"))
{
string = string.substring(1)
x = now - evaluate(string)
}
else
{
x = now + evaluate(string)
}
println x
}
它可以执行以下操作。但是……我不喜欢。我不确定后果(它是否适用于不同的时间单位?“-1.day+2.hours-3.minutes”):
String prestring = "-1.day"
String string = "0.day$prestring"
有什么建议吗?
【问题讨论】:
标签: string date groovy subtraction evaluate