Introduction to @JsonFormat and @DateTimeFormat annotations

@JsonFormat The conversion of the time format from the background to the foreground
@JsonFormat annotation is not an annotation that comes with Spring, so you need to add jackson-related dependency packages before using this annotation. Of course, if it is a SpringBoot project, you don't need to manually add dependencies yourself, because jackson-related dependencies are already included under spring-boot-start-web.
insert image description here

insert image description here

@DataFormat The conversion of the time format from front to back to the background** (input parameter formatting) The
date and time format specified by the pattern attribute value of the @DateTimeFormat annotation should correspond to the incoming parameters. If the annotation is:
@DateTimeFormat(pattern="yyyy/ MM/dd HH:mm:ss")
, the incoming parameter should be like this:
2018/08/02 22:05:55
Otherwise, an exception will be thrown.

Use of annotations:
Statement: Regarding the use of @JsonFormat, be sure to import the correct and complete package.

1. Annotate @JsonFormat

1. Use maven to introduce the jar package required by @JsonFormat

		<!--JsonFormat-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.8.8</version>
        </dependency>
  
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.8</version>
        </dependency>
  
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

2. Add @JsonFormat import java.util.Date to the attribute of the entity class corresponding to the database field of the time you need to query
;

import com.fasterxml.jackson.annotation.JsonFormat;
  
public class TestClass {
    
    
  
    //设置时区为上海时区,时间格式自己据需求定。
    @JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
    private Date testTime;
  
     
    public Date gettestTime() {
    
    
        return testTime;
    }
  
    public void settestTime(Date testTimee) {
    
    
        this.testTime= testTime;
    }
}

Here is an explanation: @JsonFormat(pattern="yyyy-MM-dd", timezone="GMT+8")
pattern: is the format of the time and date you need to convert
timezone: is the time set to East Eighth District, to avoid time conversion There is an error in
jackson, which formats the time according to the international standard time GMT when serializing the time, while the default time zone in China uses the CST time zone, and the difference between the two is 8 hours.
Tip: The @JsonFormat annotation can be placed above the property, as well as on the get method corresponding to the property. There is no difference between the two methods

3. After completing the above two steps, when we use the corresponding entity class to receive the results from the database query, the conversion of the time format is completed, and when it is returned to the front end, it will be a time format that conforms to our settings.

2. Annotate @DateTimeFormat

1. The use of @DateTimeFormat is similar to that of @jsonFormat. The first thing to introduce is spring and jodatime

<!-- joda-time -->
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.3</version>
</dependency>

2. In the controller layer, when we use the spring mvc form to automatically encapsulate the mapping object, we add @DateTimeFormat to the corresponding attribute of the object receiving the foreground data

@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date symstarttime;
 
@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date symendtime;

Note: The two annotations can be used at the same time, because both the data needs to be fetched to the foreground, and the foreground data needs to be transmitted to the background, and time format conversion is required

Guess you like

Origin blog.csdn.net/weixin_45334970/article/details/123728571