SoFunction
Updated on 2025-03-02

Analysis of JsonFormat and @DateTimeFormat annotation instance

This article mainly introduces the analysis of JsonFormat and @DateTimeFormat annotation examples. The example code is introduced in this article in detail, which has certain reference value for everyone's learning or work. Friends who need it can refer to it.

Background: When we get the time from the database and pass it to the front-end for display, we may sometimes not be able to get a satisfactory time format time date. The correct time format is displayed in the database, but it becomes an ugly time stamp. The @JsonFormat annotation solves this problem very well. We can solve this problem by using @JsonFormat: the background to the foreground time format remains consistent. Secondly, another problem is that when we use WEB service, we may need to use it to pass in time to the back-end, such as registering a new user, etc. The time format passed to the back-end is also inconsistent, and our corresponding one has another annotation. @DataTimeFormat solves this problem well. Next, record the specific usage process of @JsonFormat and DateTimeFormat.

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

1. Annotation @JsonFormat

1. Use maven to introduce the jar package required by @JsonFormat, and I will post the dependencies of my pom file here

<!--JsonFormat-->
 
    <dependency>
      <groupId></groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.8.8</version>
    </dependency>
 
    <dependency>
      <groupId></groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.8.8</version>
    </dependency>
 
    <dependency>
      <groupId></groupId>
      <artifactId>jackson-mapper-asl</artifactId>
      <version>1.9.13</version>
    </dependency>

2. Add @JsonFormat to the attributes of the entity class corresponding to the database field of the time you need to query

import ;
 
import ;
 
public class TestClass {
 
  //Set the time zone to Shanghai time zone, and the time format is determined by your own needs.  @JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
  private Date testTime;
 
   
  public Date gettestTime() {
    return testTime;
  }
 
  public void settestTime(Date testTimee) {
    = testTime;
  }
}

Here is an explanation: @JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")

pattern: It is the format of the time and date you need to convert

timezone: The time is set to East Eighth District to avoid errors in the conversion

Tip: @JsonFormat annotation can be above the attribute, and it can also be on the get method corresponding to the attribute. 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 of the database query, we complete the conversion of the time format. When we return it to the front end, it is a time format that meets our settings.

2. Annotation @DateTimeFormat

1. The use of @DateTimeFormat is similar to @jsonFormat. First of all, I need to introduce it is spring and jodatime. I won't post spring.

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

2. When we use spring mvc form to automatically encapsulate the mapping object in the controller layer, we add @@DateTimeFormat to the corresponding object receiving 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;

I only post these two attributes here. Here I use both annotations at the same time, because I need to retrieve data to the front desk and also need to transmit the front desk data to the back desk. Both need to convert the time format and can be used at the same time.

3. After passing the above two steps, we can obtain a time format that conforms to the custom format and store it in the database.

Summarize:

Annotation @JsonFormat mainly converts the time format from the background to the front desk

Annotation @DataFormAT is mainly the conversion of the time format from front to back to back

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.