Grails“Unparseable Date”错误

时间:2015-08-21 21:17:37

标签: date grails groovy type-conversion date-formatting

我之前在这里看到过一些Unparseable Date错误,但我的看似没有任何意义。我正在上传一个CSV文件,该文件将显示在网页上的一个表格中(我知道并且很容易)我正试图找到一种方法来获取单元格中的日期并将其转换为原生字符串格式到日期。

以下是代码:

        def line = f.inputStream.toCsvReader(['skipLines':1]).eachLine{fields ->
        List list = new List()
        list.item = fields[0].trim()
        String checkedOut = fields[1].trim()
        String returned = fields[2].trim()
        Date c = Date.parse('E MM/dd/yy', checkedOut)
        Date r = Date.parse('E MM/dd/yy', returned)
        list.lastCheckedOut = c
        list.lastReturned = r
        list.checkedOutBy = fields[4].trim()
        list.save flush: true
        return
    }

这是stacktrace

Error 2015-08-21 16:13:38,936 [http-bio-8080-exec-7] ERROR errors.GrailsExceptionResolver  - ParseException occurred when processing request: [POST] /inventory/list/upload - parameters:
upload: Upload
Unparseable date: "9/22/94". Stacktrace follows:
Message: Unparseable date: "9/22/94"
    Line | Method
->>  357 | parse     in java.text.DateFormat
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     27 | doCall    in org.ListController$_upload_closure1$$EPM22klU
|     34 | eachLine  in org.grails.plugins.csv.CSVReaderUtils
|    126 | doCall    in CsvGrailsPlugin$_closure4$_closure8
|     22 | upload .  in org.ListController$$EPM22klU
|    198 | doFilter  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter  in grails.plugin.cache.web.filter.AbstractFilter
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread

“字段[1]”中的日期是9/22/94,我不完全确定这里的问题是什么,我读过的所有内容似乎都表明这应该有效。

2 个答案:

答案 0 :(得分:3)

尝试将此添加到您的域中:

@BindingFormat('yyyy-MM-dd')
 Date lastCheckedOut

以这种方式设置属性:

list.lastCheckedOut = Date.parse('E MM/dd/yy', checkedOut).format('yyyy-MM-dd')

编辑:您需要在域类中执行以下导入:

import org.grails.databinding.BindingFormat

答案 1 :(得分:0)

如果您删除了“E'它会解析。字符串日期没有提供。

Date c = Date.parse('MM/dd/yy', checkedOut)
Date r = Date.parse('MM/dd/yy', returned)

实施例

assert Date.parse('MM/dd/yy', "9/22/94").toString() == 'Thu Sep 22 00:00:00 EDT 1994'