当前位置:网站首页>@Differences between jsonformat and @datetimeformat

@Differences between jsonformat and @datetimeformat

2022-06-09 05:27:00 Classmate Tao Ran.

background

When getting time from database to front end for display , Sometimes we may not get a satisfactory time format of time date , The correct time format is displayed in the database , It turns out to be an ugly time stamp ,@JsonFormat Annotation solves this problem very well , We use @JsonFormat It can be solved very well : Back to front time format consistency problem , secondly , Another problem is , We are using WEB Time of service , May need to use , Pass in time to the backstage , For example, to register a new user, you need to fill in the date of birth , At this time, the time format passed from the foreground to the background is also inconsistent , And our counterpart has another annotation ,@DataTimeFormat So we can solve this problem very well , Next, record the specific @JsonFormat And DateTimeFormat Process of use .

Statement : About @JsonFormat Use , Be sure to import the correct complete package .

@JsonFormat

1. Use maven introduce @JsonFormat The required jar package , I'll post my pom File dependency

<!--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... To the attribute of the entity class corresponding to the database field of the time you need to query @JsonFormat

import java.util.Date;
  
import com.fasterxml.jackson.annotation.JsonFormat;
  
public class TestClass {
  
    // Set the time zone to Shanghai time zone , The time format depends on your needs .
    @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's an explanation :@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")

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

   timezone: It's time set to East 8 , Avoid time errors in conversion

  Tips :@JsonFormat Annotations can be above properties , It can also be found in the property corresponding to get On the way , There is no difference between the two ways

3. After finishing the above two steps , We use the corresponding entity class to receive the results of the database query and complete the time format conversion , When returning to the front end, it will be in accordance with the time format we set

@DateTimeFormat

[email protected] The use of and @jsonFormat almost , The first thing to introduce is spring also jodatime,spring I won't

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

2. stay controller Layer we use spring mvc When the form automatically encapsulates the mapped object , We add... To the corresponding property of the object receiving the foreground data @@DateTimeFormat

@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 just post these two attributes here , Here I use both annotations at the same time , Because I need to get the data to the front desk , It also needs the data from the front desk to the back desk , We need to change the time format , Can be used at the same time

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

summary

           annotation @JsonFormat Mainly from the background to the front of the time format conversion

           annotation @DataFormAT It mainly refers to the transformation of time format from front to back

原网站

版权声明
本文为[Classmate Tao Ran.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203021428016306.html