Groovy Date解析错误

时间:2014-07-29 20:58:47

标签: grails groovy

我的程序中有一段代码,我试图解析一个日期,然后将它设置为对象上的属性,如下所示:

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

我似乎无法弄清楚这个错误意味着什么。任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:3)

它试图使用一个属性'' (空字符串)日期。该属性不存在,您收到错误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
        }
    }()