Announcement Announcement Module
Collapse
No announcement yet.
CustomEditor and java.sql.Date Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • CustomEditor and java.sql.Date

    Is there a way to define a CustomEditor for a java.sql.Date? When trying the CustomDateEditor provided by Spring, it gives me an error message since it obviously only works with java.util.Date types.

  • #2
    You can write your own property editor based on the source for CustomDateEditor. It only requires a small change to adapt if for java.sql.Date.

    Comment


    • #3
      And how can I do that? Can you give me a hint?

      Comment


      • #4
        Originally posted by Metellus
        And how can I do that? Can you give me a hint?
        Here is the source from Spring's CustomDateEditor: http://cvs.sourceforge.net/viewcvs.p...-1-2&view=auto

        You should only have to change java.util.Date to java.sql.Date and save the file in your own project source directory.

        Comment


        • #5
          A basic property editor for java.util.Dates can be wired in like
          Code:
             protected void initBinder(HttpServletRequest request,
                   ServletRequestDataBinder binder) throws Exception
             {
                String dateFormat = getMessageSourceAccessor().getMessage("format.date",
                      "dd.MM.yyyy");
                SimpleDateFormat df = new SimpleDateFormat(dateFormat);
                df.setLenient(true);
                binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(
                      df, true));
             }
          I suspect you might need to create a new property editor. You might be able to write a simple CustomSqlDateEditor although I have not done that before. Custom editors are pretty simple once you get your head around the idea. (took me a while tho :-)

          Something as basic as the following might work.
          Code:
          public class CustomSqlDateEditor extends PropertyEditorSupport
          {
             DateFormat format = null;
          
             public CustomSqlDateEditor(DateFormat format)
             {
                super();
                this.format = format;
             }
          
             public String getAsText()
             {
                java.sql.Date value = (java.sql.Date) getValue();
                return (value != null ? this.format.format(new java.util.Date(value.getTime())) : "");
             }
          
             public void setAsText(String text) throws IllegalArgumentException
             {
                try {
                   setValue(new java.sql.Date(this.format.parse(text).getTime()));
                }
                catch (ParseException e) {
                   throw new IllegalArgumentException("Could not parse date: "
                         + e.getMessage());
                }
             }
          
          }
          FWIW: I would not expose java.sql.Date in my domain/form code anyway, I would expose java.util.Date which has better support (IMHO).
          Last edited by dhainlin; Jan 9th, 2006, 04:39 AM.

          Comment


          • #6
            Thanks! That was exactly what I'm looking for. The only problem is, to load java.sql.Date field in the proper date format, since the standard sql.Date format (yyyy-MM-dd) is not the desired one to be loaded in the form field.

            Comment

            Working...
            X