Jan
Jan

Reputation: 3401

Date parsing unparseable

i tried this snippet from the answer with my previous question Grails input error: property must be a valid date

class YourController {

static SimpleDateFormat sdf = new SimpleDateFormat( 'MM dd yyyy' )

def saveUser() {
    def userInstance = new User(params)

    Date dateOfBirth = sdf.parse( params.dateOfBirth ) <-------- this

    if (!userInstance.save(flush: true)) {
        render(view: "createUser", model: [userInstance: userInstance])
        return
    }

    flash.message = message(code: 'default.created.message', args: [message(code: 'user.label', default: 'User'), userInstance.id])
    redirect(action: "showUser", id: userInstance.id)
}

My string outputs are January dd yyyy, February dd yyyy, March dd yyyy and so on. Basically the it gives the full month name

Now the error is Unparseable date: "January 01 2000"when i tried something like January 01 2000 on my input

Upvotes: 0

Views: 1225

Answers (2)

AlexBatya
AlexBatya

Reputation: 1

I had same issue. I recommend you to see how it works in <g:datePicker> - default grails tag. http://grails.github.io/grails-doc/3.0.x/ref/Tags/datePicker.html

<g:datePicker name="my_date" value="${new Date()}" precision="day"/>

It sends date to server in separated fields like:

my_date="date.struct"
my_date_day="12"
my_date_month="7"
my_date_year="2015"

That means you have to split your input field.

It is how i resolved that issue.

Upvotes: 0

rantunes
rantunes

Reputation: 376

This should do it:

 static SimpleDateFormat sdf = new SimpleDateFormat('MMM dd yyyy')

Explained here

Month: If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number.

Upvotes: 1

Related Questions