Results 1 to 3 of 3

Thread: Parse dates using custom dateformat

  1. #1
    Join Date
    May 2006
    Posts
    2

    Default Parse dates using custom dateformat

    Dear Spring forum users, this is my question. I live in Italy and italian date format id dd/mm/yyyy instead of yyyy/dd/mm.

    I'm writing a
    Code:
    AbstractCommandController
    and in its constructor I've passed
    Code:
    setCommandClass(ParametriEstrazione.class);
    .

    This is the code of my command class:

    Code:
    public class ParametriEstrazione implements Serializable {
    
        private Date daFineValidita = null;
        private boolean appartenenze = false;
        private boolean fornitoriNonPreferenziali = false;
    
        public ParametriEstrazione() {
            /* Intenzionalmente lasciato bianco... */
        }
    
        public Date getDaFineValidita() {
            return daFineValidita;
        }
    
        public void setDaFineValidita(Date daFineValidita) {
            this.daFineValidita = daFineValidita;
        }
    
        public boolean isAppartenenze() {
            return appartenenze;
        }
    
        public void setAppartenenze(boolean appartenenze) {
            this.appartenenze = appartenenze;
        }
    
        public boolean isFornitoriNonPreferenziali() {
            return fornitoriNonPreferenziali;
        }
    
        public void setFornitoriNonPreferenziali(boolean fornitoriNonPreferenziali) {
            this.fornitoriNonPreferenziali = fornitoriNonPreferenziali;
        }
    
        public String toString() {
            return "ParametriEstrazione{" +
                    "daFineValidita=" + daFineValidita +
                    ", appartenenze=" + appartenenze +
                    ", fornitoriNonPreferenziali=" + fornitoriNonPreferenziali +
                    '}';
        }
    }
    If I use this code the
    Code:
    private Date daFineValidita = null;
    property is never populated since, in my web form, the user wrote the date using italian format. How can I write a custom date editor to parse the date the way I want?

    Best regards.

    Alessandro Beneventi

  2. #2

    Default

    You could extend the initBinder method for example like this. Check out the Javadoc for CustomDateEditor and initBinder for more information

    Code:
       protected void initBinder(HttpServletRequest request, 
                ServletRequestDataBinder binder) throws Exception {
            binder.registerCustomEditor(Date.class, 
                new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), false));
        }
    \christof

  3. #3
    Join Date
    May 2006
    Posts
    2

    Default

    Thank you so much: your solution worked perfectly.

    Alessandro

    Quote Originally Posted by laenzlinger
    You could extend the initBinder method for example like this. Check out the Javadoc for CustomDateEditor and initBinder for more information

    Code:
       protected void initBinder(HttpServletRequest request, 
                ServletRequestDataBinder binder) throws Exception {
            binder.registerCustomEditor(Date.class, 
                new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), false));
        }

Posting Permissions

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