Announcement Announcement Module
Collapse
No announcement yet.
string to Date in form Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • string to Date in form

    Hi all,

    Can anyone help me in this issue.
    I have a form, which i need to bind to a member of DO class which is a java.util.Date type( String-->java.util.Date)

    any clue/code snippet will be appreciated.

    Thanks in advance.

    Best Regards,
    Kamal

  • #2
    in initBinder, register a CustomDateEditor (with a SimpleDateFormat and the required format). In the JSP use <spring:bind path="PROPERTY"> and ${status.value} (or <c:out value="${status.value}, depening on whether or not you're using a 2.3 or 2.4 servlet container). This should do the trick.

    regards,
    Alef Arendsen

    Comment


    • #3
      Thanks very much Alef.

      Your message has really helped me. i used the following code:

      protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
      SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
      dateFormat.setLenient(false);
      binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
      }

      I found that, the date format is restricted to(dd-MM-yyyy). Is there any other way to allow (dd-MM-yyyy) or (dd/MM/yyyy) or (dd-MM-yy)......using a single function.


      Thanks in advance.

      Best Regards,
      Kamal

      Comment


      • #4
        Not with the CustomDateEditor, but if you look at the code for the CustomDateEditor, you should be able to write your own without too much effort. You could have it take in a set of acceptable formats then then rewrite the getAsText() and setAsText() methods to check against each format. You'd also have to pick a default format when going from Date to String as well.


        Keller

        Comment

        Working...
        X