Spring MVC Custom Converters

Source: Internet
Author: User
Tags dateformat

    <!--Register Converters -    <Mvc:annotation-drivenConversion-service= "Conversionservice" />    <!--Custom Converters -    <BeanID= "Conversionservice"class= "Org.springframework.context.support.ConversionServiceFactoryBean">        < Propertyname= "Converters">            <List>                <Beanclass= "Com.vrv.linkdood.app.workreport.conversion.DateConverter"></Bean>            </List>        </ Property>    </Bean>
ImportJava.text.DateFormat;ImportJava.text.SimpleDateFormat;Importjava.util.ArrayList;Importjava.util.Date;Importjava.util.List;ImportOrg.springframework.core.convert.converter.Converter;/*** @Description Custom time Converters *@authorLJ * @Date June 7, 2016 PM 3:13:35 * @Version v1.0*/ Public classDateConverterImplementsConverter<string, date> {    Private Static FinalList<string> Formarts =NewArraylist<string> (4); Static{Formarts.add ("YYYY-MM"); Formarts.add ("Yyyy-mm-dd"); Formarts.add ("Yyyy-mm-dd hh:mm"); Formarts.add ("Yyyy-mm-dd Hh:mm:ss"); }     PublicDate Convert (String source) {String value=Source.trim (); if("". Equals (value)) {            return NULL; }        if(Source.matches ("^\\d{4}-\\d{1,2}$")) {            returnParsedate (source, formarts.get (0)); } Else if(Source.matches ("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {            returnParsedate (source, Formarts.get (1)); } Else if(Source.matches ("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")) {            returnParsedate (Source, Formarts.get (2)); } Else if(Source.matches ("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {            returnParsedate (Source, Formarts.get (3)); } Else {            Throw NewIllegalArgumentException ("Invalid boolean value '" + Source + "'"); }    }    /*** Function Description: Formatted date * *@paramDatestr * String character date *@paramformat * String formatting *@returnDate Dates*/     PublicDate Parsedate (string datestr, string format) {Date Date=NULL; Try{DateFormat DateFormat=NewSimpleDateFormat (format); Date=(Date) dateformat.parse (DATESTR); } Catch(Exception e) {}returndate; }}

Spring MVC Custom 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.