简体   繁体   中英

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 = { -> :

| 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 .

I can't reproduce the error.

This works (with grails 2.4.2), it also works in all cases without the if/else stuff. If the source value is null or "-" it will throw and return null or a Date if the parse succeeded.

    def record = [:]
    //record.dateOfBirth = "-"

    record.dateOfBirth = {
        try {
            Date.parse ('dd-MM-yyyy', record.dateOfBirth)
        }
        catch (Exception ignore) {
            null
        }
    }()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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