java - Struts2 Datetime picker displayformat issue -


a issue facing struts2.0.14's date time picker tag

the problem struts2 datetimepicker displayformat attribute must set format of tomcat server date time format else submitted values null.

change date time setting in win 7:

  1. rightclick bottom right corner date.
  2. click on change date & time settings
  3. change calender settings
  4. change regional settings
  5. set format english(india)

repro steps

  1. change regional settings mentioned above & restart tomcat server.
  2. now not use displayformat or use display format other "dd/mm/yyyy" in date time picker
  3. submit struts2 form date 21/12/2012
  4. in action submitted date set null
  5. now change regional setting english(us) , not use displayformat , restart server.
  6. values in action set submitted through form.

expected result

  1. whatever system date time format date must parsed accordingly , made available in action.

envi:

java 6, struts2.0.14, firefix 12, tomcat 6.

any workarounds or fixes through properties or something?

*note: not answer if answer use jquery or other js lib or upgrade.

you need use custom datetime converter.

in xwork-conversion.properties (create if don't have one) file put line:

java.util.date = org.yourproject.common.stringtodatetimeconverter

and in stringtodatetimeconverter, have code this

import java.util.* import java.text.dateformat.*;      public class stringtodatetimeconverter extends strutstypeconverter{          public object convertfromstring(map context, string[] strings, class toclass) {                   dateformat datetime_format = getdateinstance(short, locale.getdefault());              if (strings == null || strings.length == 0 || strings[0].trim().length() == 0) {                 return null;             }              try             {                 datetime_format.setlenient(false);                 return datetime_format.parseobject(strings[0]);              } catch (parseexception e) {                 //throw new typeconversionexception(); <-- if want catch conversion error                 return null;             }         }          public string converttostring(map context, object date) {             dateformat datetime_format = getdateinstance(short, locale.getdefault());             if (date != null && date instanceof date) {                 return datetime_format.format(date);             } else {                 return null;             }         }     } 

references:

java date format locale

http://www.roseindia.net/java/java-get-example/java-get-default-locale.shtml


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -