简体   繁体   English

Groovy Date解析错误

[英]Groovy Date parsing error

I have a piece of code in my program where I am trying to parse a date and then set it equal to a property on an object like this: 我在我的程序中有一段代码,我试图解析一个日期,然后将它设置为对象上的属性,如下所示:

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
}

I am getting this error regarding the line which reads as ​def 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

I can't seem to figure out what this error means. 我似乎无法弄清楚这个错误意味着什么。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Thank you. 谢谢。

It tries to use a property '' (empty string) on date. 它试图在日期使用属性''(空字符串)。 The property does not exist and you get the error No such property: ​ for class: java.util.Date . 该属性不存在,您将收到错误No such property: ​ for class: java.util.Date

I can't reproduce the error. 我无法重现错误。

This works (with grails 2.4.2), it also works in all cases without the if/else stuff. 这有效(使用grails 2.4.2),它也适用于没有if / else东西的所有情况。 If the source value is null or "-" it will throw and return null or a Date if the parse succeeded. 如果源值为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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM