2

I have a problem within a grails 2.3 application, when it comes to data binding and correct date formats.

I use a datepicker (jQuery ui) that provides a <input type="hidden" /> that holds the selected date in ISO_8601 format. It will post a value like this: 2015-08-14 to the controller. The form itself and the post result is correct.

I use this simplified model:

class Thing {

    DateTime lastUpdated

    static constraints = {
        lastUpdated nullable: true
    }
}


when I try to create an entity, I will face this error message:

Invalid format: "2015-08-14" is malformed at "15-08-14"


A first research lead me to this change in the Config.groovy:

jodatime.format.html5 = true

(Link 3 in the list below)

Appying this leads to change. Now the error message is:

Invalid format: "2015-08-14" is too short (flip table)


Another try was to change the databinding.dateFormats to this (also in the Config.groovy):

grails.databinding.dateFormats = [ "yyyy-MM-dd HH:mm:ss.S","yyyy-MM-dd'T'hh:mm:ss'Z'", "yyyy-MM-dd"]

Which has no effect what so ever.


For my understanding a given date format should automatically be marshaled in a dateTime object. What configuration did I miss?


Here are relative question, that sadly did not help me:

Community
  • 1
  • 1
Nico O
  • 13,762
  • 9
  • 54
  • 69
  • 1
    http://stackoverflow.com/questions/11180431/how-change-joda-time-default-datetime-format-in-grails – Ilya Aug 25 '15 at 11:05
  • @Ilya thank you for your reply. I'am quite new to grails and do not fully understand how this can help. Should I put this: `jodatime { format.org.joda.time.DateTime = "yyyy-MM-dd" }`? - this seems so wrong. Is there a connection between input and display style? **Update:** This do work, but is it a good idea to switch the dateTime format to a date only format? – Nico O Aug 25 '15 at 11:10
  • @Ilya may you want to post this as an answer? This seems to solve the problem. I still have to understand the impact of this change. – Nico O Aug 25 '15 at 11:28

1 Answers1

2

You should add next line in config.groovy

jodatime { format.org.joda.time.DateTime = "yyyy-MM-dd" }  

But if you don't need time in this field, it's better to use LocalDate instead of DateTime here.

class Thing {  
   LocalDate lastUpdated;

...

jodatime {   
   format.org.joda.time.DateTime = "yyyy-MM-dd HH:mm:ss" 
   format.org.joda.time.LocalDate = "yyyy-MM-dd" 
}  

So you will use DateTime where you need date with time and LocalDate where date is enough

Ilya
  • 29,135
  • 19
  • 110
  • 158
  • Thank you for your time. This was driving me insane ;) Now everything seems to work like expected. – Nico O Aug 25 '15 at 12:06