Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Grails Version >= 2.3</h2> <p>A setting in <code>Config.groovy</code> defines the date formats which will be used application-wide when binding params to a <code>Date</code></p> <pre><code>grails.databinding.dateFormats = [ 'MMddyyyy', 'yyyy-MM-dd HH:mm:ss.S', "yyyy-MM-dd'T'hh:mm:ss'Z'" ] </code></pre> <p>The formats specified in <code>grails.databinding.dateFormats</code> will be attempted in the order in which they are included in the List.</p> <p>You can override these application-wide formats for an individual command object using <code>@BindingFormat</code></p> <pre><code>import org.grails.databinding.BindingFormat class Person { @BindingFormat('MMddyyyy') Date birthDate } </code></pre> <h2>Grails Version &lt; 2.3</h2> <blockquote> <p>i can't and will not belief that extracting the date by hand is nessesary!</p> </blockquote> <p>Your stubbornness is rewarded, it has been possible to bind a date directly since long before Grails 1.3. The steps are:</p> <p><strong>(1)</strong> Create a class that registers an editor for your date format</p> <pre><code>import org.springframework.beans.PropertyEditorRegistrar import org.springframework.beans.PropertyEditorRegistry import org.springframework.beans.propertyeditors.CustomDateEditor import java.text.SimpleDateFormat public class CustomDateEditorRegistrar implements PropertyEditorRegistrar { public void registerCustomEditors(PropertyEditorRegistry registry) { String dateFormat = 'yyyy/MM/dd' registry.registerCustomEditor(Date, new CustomDateEditor(new SimpleDateFormat(dateFormat), true)) } } </code></pre> <p><strong>(2)</strong> Make Grails aware of this date editor by registering the following bean in <code>grails-app/conf/spring/resources.groovy</code></p> <pre><code>beans = { customPropertyEditorRegistrar(CustomDateEditorRegistrar) } </code></pre> <p><strong>(3)</strong> Now when you send a date in a parameter named <code>foo</code> in the format <code>yyyy/MM/dd</code> it will automatically be bound to a property named <code>foo</code> using either:</p> <pre><code>myDomainObject.properties = params </code></pre> <p>or </p> <pre><code>new MyDomainClass(params) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload