SPRINGMVC Conversion of a date type

Source: Internet
Author: User
Tags dateformat

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) {  3 SimpleDateFormat dateformat = new Simpledateform at ("Yyyy-mm-dd");  4 dateformat.setlenient (false);  5 Binder.registercustomeditor (Date.class, New Customdateeditor (DateFormat, true));   

3. A global type converter can be added to the system

Implementing converters

1 public class DateConverter implements Converter<string, date> {     2 @Override     3 public Date convert (String s Ource) {     4     simpledateformat dateformat = new SimpleDateFormat ("Yyyy-mm-dd");     5     Dateformat.setlenient (false);     6     try {     7         return dateformat.parse (source);     8     } catch (ParseException e) {     9         e.printstacktrace ();    Ten     }     return null;    12}    

To configure:

<bean id= "Conversionservice" class= "Org.springframework.format.support.FormattingConversionServiceFactoryBean ">            <property name=" Converters ">                <list>                    <bean class=" Com.doje.XXX.web.DateConverter "/>                </list>            </property>    
<mvc:annotation-driven 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 class Customobjectmapper extends Objectmapper {   3    4     Public Customobjectmapper () {   5         customserializerfactory factory = new Customserializerfactory ();   6         factory.addgenericmapping (Date.class, New jsonserializer<date> () {   7             @Override   8             public void serialize (Date value, Jsongenerator jsongenerator,   9                     Serializerprovider provider) throws IOException, jsonprocessingexception {                 simpledateformat sdf = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss" );                 jsongenerator.writestring (Sdf.format (value));         );         this.setserializerfactory (factory);  16}  

The configuration is as follows:

<mvc:annotation-driven>      <mvc:message-converters>          <bean 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 () {  return this.createtime;  

SPRINGMVC Conversion of a date type

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.