0

I came accross an interesting problem in my code today. I am using Grails 2.2.0.

Here is the code

def user = lookupUserClass().get(params.id)
log.info "[update]user.subscriptionExpiryDate1: " + user.subscriptionExpiryDate
user.subscriptionExpiryDate = Calendar.getInstance();
log.info "[update]user.subscriptionExpiryDate2: " + user.subscriptionExpiryDate
if (user.subscriptionExpiryDate instanceof Calendar ) {
    log.error "***** Is A Calendar Instance ***"
} else if (user.subscriptionExpiryDate instanceof String ) {
    log.error "***** Is A String Instance ***"
}  else {
    log.error "***** Is Something else ***"
}

if (!user.save()) {
    log.error "[update]Error occured saving user. Errors are: "
    user.errors.each { err -> log.error err; }
    render view: 'edit', model: buildUserModel(user)
    return
} else {
    log.info "[update]Successfully saved user"
}

subscriptionExpiryDate is a calendar property in my User object.

When I perform the save I get the following error

Failed to convert property value of type 'java.lang.String' to required type 'java.util.Calendar' for property 'subscriptionExpiryDate'; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: 05/03/2013

Could anyone please explain why I would be seeing this error for the above code as nothing is standing out

doelleri
  • 19,232
  • 5
  • 61
  • 65
Damien
  • 4,081
  • 12
  • 75
  • 126
  • so which error message from the above is logged? If there is nothing after `Unparsable date:` the date is empty and therfore unparsable/invalid – moeTi Mar 05 '13 at 16:21
  • I suggest you to use Joda instaead of Calendar. See [the plugin](http://grails.org/plugin/joda-time) –  Mar 05 '13 at 16:33
  • @moeti apologies, I didnt paste the full date, the date is printed out and is a valid date Sergio - thanks Sergio I will keep that in mind but I would be curious as to why my initial code is not working first – Damien Mar 05 '13 at 16:39
  • 2
    Your snippet seems to run fine in isolation. Are you doing any data binding on the `user` object before the code you pasted? If you are binding to the `subscriptionExpiryDate` property, you will need a `PropertyEditor` to do the `String` -> `Calendar` conversion – Andrew Mar 05 '13 at 17:49
  • Hi Andrew, yes there is some binding going on before that point. Thanks for mentioning the PropertyEditor, I will have a look at that – Damien Mar 05 '13 at 20:52
  • @Andrew - I followed your suggestion and it worked a treat - many thanks for this – Damien Mar 06 '13 at 12:09

1 Answers1

0

This question has been resolved as per Andrew's suggestion:

Are you doing any data binding on the user object before the code you pasted? If you are binding to the subscriptionExpiryDate property, you will need a PropertyEditor to do the String -> Calendar conversion

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Damien
  • 4,081
  • 12
  • 75
  • 126