Spring in date format conversion

Source: Internet
Author: User

I. BACKGROUND

Building Projects with Ssm+maven

Where the date field in MySQL is a datetime type

The JSON data that the controller returns to the front end is a long type "Date": "1528324651545"

What I need is the YYYY-MM-DD format.

Second, the settlement

1. In the mapper file corresponding to the entity, modify the data type of the column in the Resultmap

1 <Mappernamespace= "Com.ssm.mapper.PeopleMapper">2 3     <Resultmaptype= "Com.ssm.model.people"ID= "Peoplerm">4 ... ..5 6         <result Property= "B_date"column= "Bir_date" javatype= "Java.sql.Date" />7 8 ... ..9 Ten </Resultmap>

2. Add annotations to the Getter method on the corresponding field of the entity class

1 Private Date b_date; 2 3 @DateTimeFormat (pattern= "Yyyy-mm-dd")  5  Public Date getb_date () {7return  b_date; 9 }   

2.1 Of course it can be written as follows:

1 Private Date b_date; 2 3 @DateTimeFormat (iso=ISO. Date)5 public Date getb_date () {7return  b_date; 9 }   

2.2 When the field in your entity class is of type time, the type of column in Resultmap should be java.sql.Time

Annotations are @datetimeformat (pattern= "HH:mm:ss")

3. Introducing Joda in Maven's Pom.xml

1     <!--Date Time Library -2     <Dependency>3         <groupId>Joda-time</groupId>4         <Artifactid>Joda-time</Artifactid>5         <version>2.3</version>6     </Dependency>

4. In the SPRINGMVC configuration file, add the note driver

1 <mvc:annotation-driven/>

Third, the above related knowledge, please check your own

Spring in date format conversion

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.