<!--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