Announcement Announcement Module
Collapse
No announcement yet.
Parse dates using custom dateformat Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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));
        }

    Comment


    • #3
      Thank you so much: your solution worked perfectly.

      Alessandro

      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));
          }

      Comment

      Working...
      X