Announcement Announcement Module
No announcement yet.
Date Property Editors Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Date Property Editors

    I have a form with two date fields: one has a date and time, and the other is just a date. What is the best way to create a PropertyEditorSupport class so that I can handle both situations as they both are represented as java.util.Date?

    I started by creating such a class and to override the setAsText method, which works perfectly. However, if I don't override getAsText, then I get the full timestamp. And this is where my dilemma begins ... if one is a date and time, and the other is just a date, how can I output them to be displayed differently?

    I do have a kludge which does work, but now I'm testing logic inside a view instead of keeping it in my business class. On overriding getAsText, I check of the date has an hour greater than zero (which works because I'm only dealing with regular office hours).


  • #2
    While registering the property editor using registerCustomEditor on the ServletRequestDataBinder you can also specify a field name. Use that in combination with a somewhat configurable property editor (for example mention a boolean in the constructor indicating whether or not to also output timestamps).

    binder.registerCustomEditor(Date.class, "timestamp", new MyDateEditor(true));
    binder.registerCustomEditor(Date.class, "noTimestamp", new MyDateEditor(false));

    Hope this helps,



    • #3
      Thank you, that works perfectly! I wasn't aware that I could pass a field name along with the registering the property editor. So, I'm now using the built-in CustomDateEditor instead of rolling my own editor.