Time processing (Backend)

Keywords: Java Back-end

Reference connection
@Use of JsonFormat and @ DateTimeFormat annotations: https://www.cnblogs.com/mracale/p/9828346.html
com.fasterxml.jackson tool class: https://www.cnblogs.com/loong-hon/p/10315000.html
Detailed summary and usage of Jackson API: https://blog.csdn.net/wangmx1993328/article/details/88598625

com.fasterxml.jackson introduction

The old version of Jackson uses a package named org.codehaus.jackson, while the new version uses com.fasterxml.jackson.
When using gson and fastjson, you only need to import a jar package (or a dependency), but jackson is not completely integrated into a jar (an application), but is divided into different function modules. For which functions need to be used, you can import the corresponding jar package (or dependency).
Jackson mainly includes three modules:

jackson-core
jackson-annotations
jackson-databind
 Among them, jackson-annotations Depend on jackson-core,jackson-databind And rely on jackson-annotations. 

Jackson databind internally relies on Jackson annotations and Jackson core, so when Maven applies, as long as one databind is imported, annotations and core dependencies are also imported.

Jackson has three ways to deal with Jason:

Use underlying based Stream The way is right Json Each small component of is controlled
 use Tree Model,adopt JsonNode Process single Json node
 use databind Module, direct to Java Object to serialize and deserialize

spring-boot-starter-json

The Spring Boot Web component uses jackson by default, so it is very necessary to master Jackjson.

When the time obtained from the database is transferred to the front end for display, sometimes we may not be able to obtain a time date in a satisfactory time format. The correct time format is displayed in the database, but it becomes an ugly time stamp,@JsonFormat Annotation solves this problem well by using@JsonFormat It can solve the problem that the time format from the background to the foreground is consistent.
Second, another problem is that we are using WEB It may be necessary to transfer the time to the background. 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 corresponding annotation has another annotation,@DataTimeFormat This problem is well solved. Next, record the specific problems@JsonFormat And DateTimeFormat Use process of.

Declaration: for the use of @ JsonFormat, you must import the correct and complete package.

1. Annotation @ JsonFormat - consistency of time format from background to foreground

1.1maven coordinates

If you use spring boot, the spring boot starter web already contains Jackson databind

<!--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>

1.2 application method

 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")

pattern: is the format of the time and date you need to convert
timezone: the time is set to the East eighth zone to avoid errors in time conversion
Tip: @ JsonFormat annotation can be above the attribute, and can also be on the get method corresponding to the attribute. There is no difference between the two methods

Note: after introducing the fasterxml maven jar package, you can use @ JsonFormat annotation on the entity class attribute. Note that it will only return the formatted yyyy MM DD HH: mm: SS time when @ ResponseBody returns json data. If you directly use System.out.println() to output, it is still similar to "Fri Dec 01 21:05:20 CST 2017" Such a time style.

2. Annotation @ DateTimeFormat -- conversion of time format from before and after to background

maven coordinates

@The use of DateTimeFormat is similar to @ jsonFormat. First, spring and jodatime need to be introduced. I won't post spring

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

use

2. In the controller layer, when we use the spring mvc form to automatically encapsulate the mapping object, we add @ @ DateTimeFormat to the properties of 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 paste these two attributes here. I use both annotations at the same time, because I need to get the data to the foreground and transfer the foreground data to the background. Both need to convert the time format and can be used at the same time

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

Problems and Solutions

Posted by Vik on Mon, 08 Nov 2021 22:48:33 -0800