Results 1 to 6 of 6

Thread: CustomEditor and java.sql.Date

  1. #1
    Join Date
    Jan 2006
    Posts
    3

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    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.

  3. #3
    Join Date
    Jan 2006
    Posts
    3

    Default

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

  4. #4
    Join Date
    Jul 2005
    Location
    Munich, Germany
    Posts
    153

    Default

    Quote 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.

  5. #5
    Join Date
    Nov 2004
    Location
    Dallas, TX (USA)
    Posts
    58

    Default

    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 at 03:39 AM.
    Sleep is for the weak

  6. #6
    Join Date
    Jan 2006
    Posts
    3

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •