There are three solutions available.
A. Local conversion: Only valid for the current controller class
Added in Springmvc.xml:
<bean class = " Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter "> <property name=" Messageconverters "> <list> <ref bean=" Stringhttpmessageconverter "/> & Lt;/list> </property> </bean> <!--string type parser, allowing direct return of a string type of message--<bean id= "str Inghttpmessageconverter "class =" Org.springframework.http.converter.StringHttpMessageConverter "/> <!--date conversion-<bean class = "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > <property Name= "Webbindinginitializer" > <bean class = "Com.rw.tools.ConvertDate" /> </property> </bean>
The Controller class file is added:
@Controller @requestmapping ("/image") Public classImagecontroller {@AutowiredPrivateImageService ImageService; @org. Springframework.web.bind.annotation.InitBinder Public voidInitbinder (Webdatabinder binder) {DateFormat df=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Customdateeditor Dateeditor=NewCustomdateeditor (DF,true); Binder.registercustomeditor (Date.class, Dateeditor); }
Two. Global conversions
1. Create the Convertdate class to implement the Webbindinginitializer interface
Public classConvertdateImplementswebbindinginitializer{@Override Public voidInitbinder (Webdatabinder binder, WebRequest request) {//TODO auto-generated Method Stub//Conversion DateDateFormat dateformat=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Binder.registercustomeditor (Date.class,NewCustomdateeditor (DateFormat,true)); }}
2. Configure the date conversion in Spring-mvc.xml
<!--date conversion-- class= " Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "> <property name=" Webbindinginitializer "> class=" Com.wx.web.convertDate "/> </property> </ Bean>
Three. Entity class attribute method configuration
@DateTimeFormat (pattern= "Yyyy-mm-dd HH:mm:ss")// The format of the reception's time format to the background @JsonFormat (pattern= " Yyyy-mm-dd HH:mm:ss ", timezone =" gmt+8 ")// function: The background time format is sent to the foreground private date date;
@JsonFormat default is the time of the standard Time zone, Beijing time East eight district timezone= "Gmt+8"
Function: Background time format sent to foreground
Format of the @DateTimeFormat to receive the foreground time format into the background
Workaround: Receive date data issues in SPRINGMVC