[英]Groovy Date parsing error
我在我的程序中有一段代码,我试图解析一个日期,然后将它设置为对象上的属性,如下所示:
if (newIndividualRecord.dateOfBirth == null || newIndividualRecord.dateOfBirth == "-")
newIndividualRecord.dateOfBirth = null
else {
def date = { ->
try {
Date.parse('dd-MM-yyyy', newIndividualRecord.dateOfBirth)
}
catch(e) {
newIndividualRecord.dateOfBirth = null
/* Should an incorrectly formatted date be reported as an error here? */
}
}()
if (date.getClass() == java.util.Date)
newIndividualRecord.dateOfBirth = date
}
我收到关于读取为def date = { ->
的行的错误:
| Error 2014-07-29 20:46:15,892 [http-bio-8080-exec-7] ERROR errors.GrailsExceptionResolver - MissingPropertyException occurred when processing request: [POST] /FatcaOne_0/customer/upload - parameters:
dataTypegrp: 1
fileTypegrp: 1
No such property: for class: java.util.Date
Possible solutions: day. Stacktrace follows:
Message: No such property: for class: java.util.Date
Possible solutions: day
Line | Method
->> 788 | $tt__processNewIndividualRecordFlags in com.twc.fatcaone.FileImportService$$EOlRGIsK
我似乎无法弄清楚这个错误意味着什么。 任何帮助将不胜感激。 谢谢。
它试图在日期使用属性''(空字符串)。 该属性不存在,您将收到错误No such property: for class: java.util.Date
。
我无法重现错误。
这有效(使用grails 2.4.2),它也适用于没有if / else东西的所有情况。 如果源值为null或“ - ”,则抛出并返回null或者如果解析成功则返回Date
。
def record = [:]
//record.dateOfBirth = "-"
record.dateOfBirth = {
try {
Date.parse ('dd-MM-yyyy', record.dateOfBirth)
}
catch (Exception ignore) {
null
}
}()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.