Workaround: Receive date data issues in SPRINGMVC

Source: Internet
Author: User
Tags dateformat

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

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.