Recently, a little bit of the suspicion of spring, and then learned a little bit of knowledge of the record here.
Often in the project we will encounter front page input a date type string passed to the back end we need to do the conversion. Even in the process of delivery will be an error.
Spring has a once and for all approach, configured to follow its own rules after the conversion will not need to continue the Java Backend conversion
The spring jar can be downloaded to the spring official website.
1. First type of Convertor converter
PackageCom.converter;Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;ImportOrg.springframework.core.convert.converter.Converter; Public classStringDateImplementsConverter<string, date>{ //Date Conversion Format PrivateString pattern; //constructor Function Publicstringdate (String pattern) {Super(); This. Pattern =pattern; } @Override PublicDate Convert (String arg0) {//YYYY-MM-DDSimpleDateFormat SD =NewSimpleDateFormat (pattern); Try { returnSd.parse (arg0); } Catch(ParseException e) {//e.printstacktrace (); Throw NewIllegalaccesserror ("Date conversion Error!! "); } }}
Need to configure the content in the Springmvc-servlet.xml configuration file (springmvc-servlet file specific can view my previous article Springmvc+springjdbc+mysql annotation mode)
<!--Date Conversion Factory - <BeanID= "Conversionservice"class= "Org.springframework.context.support.ConversionServiceFactoryBean"> < Propertyname= "Converters"> <List> <Beanclass= "Com.converter.StringDate"> <Constructor-argtype= "Java.lang.String"value= "Yyyy-mm-dd"></Constructor-arg> </Bean> </List> </ Property> </Bean>
2. Second Way Formatter converter
PackageCom.converter2;Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;ImportJava.util.Locale;ImportOrg.springframework.core.convert.converter.Converter;ImportOrg.springframework.format.Formatter;/*** Date type converter *@authorAdministrator **/ Public classStringDate2ImplementsFormatter<date>{ //Date Conversion Format PrivateString pattern; PrivateSimpleDateFormat SD; //constructor Function PublicStringDate2 (String pattern) {Super(); This. Pattern =pattern; SD=NewSimpleDateFormat (pattern); } @Override PublicString Print (Date object, locale locale) {//In addition to this sentence, we need to follow the date specification to make an error like 1999-15-15.Sd.setlenient (false); returnSd.format (object); } @Override PublicDate Parse (String text, locale locale)throwsparseexception {sd.setlenient (false); returnsd.parse (text); }}
Need to configure the content in the Springmvc-servlet.xml configuration file
<BeanID= "Conversionservice"class= "Org.springframework.format.support.FormattingConversionServiceFactoryBean"> < Propertyname= "Formatters"> <List> <Beanclass= "Com.converter2.StringDate2"> <Constructor-argtype= "Java.lang.String"value= "Yyyy-mm-dd"></Constructor-arg> </Bean> </List> </ Property> </Bean>
The above method is as long as the page input specified format value= "Yyyy-mm-dd" string and then send the request and take the parameters to the controller spring will automatically convert to a date type
Spring configuration Converter, formatter date converters