Spring configuration Converter, formatter date converters

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.