java - Can't separately input date in thymeleaf -


i've been trying add dateofbirth fields registration form. want separate date 3 fields: day, month , year.

when enter date fields, in controller current date , strange year - "3890".

my html code:

<div class="form-group row">     <label for="confirm" class="col-sm-2 col-form-label">date of birth</label>     <div class="col-sm-10">         <input id="day" type="text" th:field="*{dateofbirth.day}"/>     </div>     </div>     <div class="form-group row">         <label for="confirm" class="col-sm-2 col-form-label"></label>     <div class="col-sm-10">         <input id="month" type="text" th:field="*{dateofbirth.month}"/>     </div>         </div>     <div class="form-group row">         <label for="confirm" class="col-sm-2 col-form-label"></label>     <div class="col-sm-10">         <input id="year" type="text" th:field="*{dateofbirth.year}"/>     </div>     </div> 

my controllers code:

@getmapping(value = "/register") public string showregistrationform(webrequest request, model model) {     channeldto channeldata = new channeldto();     model.addattribute("channel", channeldata);     return "registration"; }  @postmapping(value = "/register")     public modelandview registeruseraccount(@modelattribute("channel") @valid channeldto channeldata,                                             bindingresult result, httpservletrequest request, errors errors, redirectattributes redirectattributes) {         channel channel = null;         if (!result.haserrors()) {             channel = createuseraccount(channeldata, result);         }          //logic...          return new modelandview("redirect:/register/select-interests");     } 

my channeldto:

@passwordmatches public class channeldto {      @notnull     @notempty     @email     private string email;      @notnull     @notempty     private string name;      //@notnull     //@notempty     @datetimeformat(pattern="yyyy-mm-dd")     private date dateofbirth;      @notnull     @notempty     private string password;      @notnull     @notempty     private string matchingpassword; } 

i appreciate help, thank you.

you're using original date class (it's not related java 8's datetime api): java.util.date

the documentation states setyear:

sets year of date object specified value plus 1900.

so need subtract year value 1900. can th:value, or in controller can subtract dateofbirth 1900 before logic part.

or can use thymeleaf java8 datetime api module, one: thymeleaf - module java 8 time api compatibility

if want subtract date 1900, try out:

@postmapping(value = "/register") public modelandview registeruseraccount(@modelattribute("channel") @valid channeldto channeldata, bindingresult result, httpservletrequest request, errors errors, redirectattributes redirectattributes) {     calendar cal = calendar.getinstance();     cal.settime(channeldata.getdateofbirth());     cal.set(calendar.year, cal.get(calendar.year) - 1900);     channeldata.setdateofbirth(cal.gettime());      channel channel = null;     if (!result.haserrors()) {         channel = createuseraccount(channeldata, result);     }      //logic...      return new modelandview("redirect:/register/select-interests"); } 

also modify line:

<input id="day" type="text" th:field="*{dateofbirth.day}"/> 

to this:

<input id="day" type="text" th:field="*{dateofbirth.date}"/> 

you can try use not date type dateofbirth, calendar.

edit

there 2 options can try:

option 1:

leave channeldto , modify controller so:

@postmapping(value = "/register") public modelandview registeruseraccount(@modelattribute("channel") @valid channeldto channeldata,                                         bindingresult result, httpservletrequest request, errors errors, redirectattributes redirectattributes) {     calendar cal = calendar.getinstance();     cal.settime(channeldata.getdateofbirth());     cal.set(calendar.year, cal.get(calendar.year) - 1900);     cal.set(calendar.month, cal.get(calendar.month) - 1);     channeldata.setdateofbirth(cal.gettime());      channel channel = null;     if (!result.haserrors()) {         channel = createuseraccount(channeldata, result);     }      //logic...      return new modelandview("redirect:/register/select-interests"); } 

modify html this:

<div class="form-group row">     <label for="confirm" class="col-sm-2 col-form-label">date of birth</label>     <div class="col-sm-10">         <input id="day" type="text" th:field="*{dateofbirth.date}" />     </div> </div> <div class="form-group row">     <label for="confirm" class="col-sm-2 col-form-label"></label>     <div class="col-sm-10">         <input id="month" type="text" th:field="*{dateofbirth.month}" />     </div> </div> <div class="form-group row">     <label for="confirm" class="col-sm-2 col-form-label"></label>     <div class="col-sm-10">         <input id="year" type="text" th:field="*{dateofbirth.year}" />     </div> </div> 

option 2:

create new class, used hold year, month , day values:

public class customdate {     public customdate() {     }      private int year;     private int month;     private int day;      public int getyear() {         return year;     }     public void setyear(int year) {         this.year = year;     }     public int getmonth() {         return month;     }     public void setmonth(int month) {         this.month = month -1;     }     public int getday() {         return day;     }     public void setday(int day) {         this.day = day;     } } 

modify channeldto class so:

public class channeldto {      public channeldto() {     }      @notnull     @notempty     @email     private string email;      @notnull     @notempty     private string name;      //@notnull     //@notempty     @datetimeformat(pattern="yyyy-mm-dd")     private date dateofbirth;      private customdate customdateofbirth;      @notnull     @notempty     private string password;      @notnull     @notempty     private string matchingpassword;      public string getemail() {         return email;     }      public void setemail(string email) {         this.email = email;     }      public string getname() {         return name;     }      public void setname(string name) {         this.name = name;     }      public date getdateofbirth() {         calendar cal = calendar.getinstance();         cal.set(customdateofbirth.getyear(), customdateofbirth.getmonth(), customdateofbirth.getday());         return cal.gettime();     }      public void setdateofbirth(date dateofbirth) {         this.dateofbirth = dateofbirth;     }      public string getpassword() {         return password;     }      public void setpassword(string password) {         this.password = password;     }      public string getmatchingpassword() {         return matchingpassword;     }      public void setmatchingpassword(string matchingpassword) {         this.matchingpassword = matchingpassword;     }      @transient     public customdate getcustomdateofbirth() {         return customdateofbirth;     }      public void setcustomdateofbirth(customdate customdateofbirth) {         this.customdateofbirth = customdateofbirth;     }  } 

please note there new field called customdateofbirth, , getter method of customdateofbirth has @transient annotation prevents jpa insert database. if want skip being serialized via jackson, put @jsonignore annotation on getter method, too.

the getdateofbirth method has custom logic in it, transforms customdateofbirth date type.

modify html this:

<div class="form-group row">     <label for="confirm" class="col-sm-2 col-form-label">date of         birth</label>     <div class="col-sm-10">         <input id="day" type="text" th:field="*{customdateofbirth.day}" />     </div> </div> <div class="form-group row">     <label for="confirm" class="col-sm-2 col-form-label"></label>     <div class="col-sm-10">         <input id="month" type="text" th:field="*{customdateofbirth.month}" />     </div> </div> <div class="form-group row">     <label for="confirm" class="col-sm-2 col-form-label"></label>     <div class="col-sm-10">         <input id="year" type="text" th:field="*{customdateofbirth.year}" />     </div> </div> 

please note customdateofbirth used in html template.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -