When it comes to web development, the page passes in a string type, and SPRINGMVC can convert some of the basic types, but the conversion to the date class may require us to configure.
1, if the query class lets us write ourselves, then adds the @DateTimeFormat (pattern = "Yyyy-mm-dd") before the property, can convert the string to the date type, as follows
@DateTimeFormat (pattern = "YYYY-MM-DD") private Date createtime;
2. If we are only responsible for Web layer development, we need to include data binding in the controller:
1 @InitBinder 2 Public void Initbinder (Webdatabinder binder) { 3new SimpleDateFormat ("Yyyy-mm-dd"); 4 dateformat.setlenient (false); 5 binder.registercustomeditor (Date. Classnewtrue)); //
3. A global type converter can be added to the system
Implementing converters
1 Public classDateConverterImplementsConverter<string, date> { 2 @Override3 PublicDate Convert (String source) {4SimpleDateFormat DateFormat =NewSimpleDateFormat ("Yyyy-mm-dd"); 5Dateformat.setlenient (false); 6 Try { 7 returndateformat.parse (source); 8}Catch(ParseException e) {9 E.printstacktrace (); Ten } One return NULL; A}
To configure:
<BeanID= "Conversionservice"class= "Org.springframework.format.support.FormattingConversionServiceFactoryBean"> < Propertyname= "Converters"> <List> <Beanclass= "Com.doje.XXX.web.DateConverter" /> </List> </ Property> </Bean>
<conversion-service= "Conversionservice"/>
4. If you convert the date type to string on the page, you need to work with some tips from the front end.
5. SPRINGMVC when using @responsebody to return JSON, the date format is displayed by default as a timestamp.
1@Component ("Customobjectmapper") 2 Public classCustomobjectmapperextendsObjectmapper {3 4 PublicCustomobjectmapper () {5Customserializerfactory factory =Newcustomserializerfactory (); 6Factory.addgenericmapping (Date.class,NewJsonserializer<date>() { 7 @Override8 Public voidSerialize (Date value, Jsongenerator jsongenerator,9Serializerprovider provider)throwsIOException, jsonprocessingexception {TenSimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); One jsongenerator.writestring (Sdf.format (value)); A } - }); - This. Setserializerfactory (Factory); the } -}
The configuration is as follows:
<mvc:annotation-driven> <mvc:message-converters> class= " Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter "> <property name=" Objectmapper "ref=" Customobjectmapper "></property> </bean> </mvc:message-converters > </mvc:annotation-driven>
6. When the date type is converted to a JSON string, a long time value is returned, and the @jsonformat (pattern= "Yyyy-mm-dd HH:mm:ss") is written on the Get method that returns the type of the specified date, timezone = " Gmt+8 ") to return the JSON object to the specified type.
@DateTimeFormat (pattern= "Yyyy-mm-dd HH:mm:ss") @JsonFormat (Pattern= "Yyyy-mm-dd HH:mm:ss", timezone = " Gmt+8 ") public Date getcreatetime () { returnthis. createtime; }
SPRINGMVC Conversion of a date type