Reputation: 11
I've seen some Unparseable Date errors on here before, but mine doesn't seem to make any sense. I'm uploading a CSV file, which is going to be displayed in a table on a webpage (that much I know and is easy) and I'm trying to find a way to take the date in a cell and convert it from it's native string format to a date.
Here's the code:
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
}
Here's the 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
The date held in "fields[1]" is 9/22/94, I'm not entirely sure what the issue here is, everything I've read seems to show that this should work.
Upvotes: 1
Views: 1729
Reputation: 9895
If you remove the 'E' it will parse. It's not being provided in the date Strings.
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'
Upvotes: 0
Reputation: 11327
Try adding this to your domain:
@BindingFormat('yyyy-MM-dd')
Date lastCheckedOut
And set the property this way:
list.lastCheckedOut = Date.parse('E MM/dd/yy', checkedOut).format('yyyy-MM-dd')
Edit: you will need to do the following import into your domain class:
import org.grails.databinding.BindingFormat
Upvotes: 3