Use fastjson2's @JSONField annotation to solve date format records

I was doing a three-party docking recently. The date format data of the other party required the time and date format: yyyyMMddHHmmss or
yyyyMMdd. I suddenly thought of the fastjson2 toolkit, so I solved the problem happily. question.
The dependent jars are as follows:

    <dependency>
        <groupId>com.alibaba.fastjson2</groupId>
        <artifactId>fastjson2</artifactId>
        <version>2.0.16</version>
    </dependency>

The test code is as follows:

@Data
public class TestDTO {
    
    

    private String name;

    @JSONField(format="yyyyMMddHHmmss")
    private Date createTime;

    @JSONField(format="yyyy-MM-dd HH:mm:ss")
    private Date updateTime;
}
public class DateTest {
    
    

    public static void main(String[] args) {
    
    
        TestDTO dto = new TestDTO();
        dto.setCreateTime(new Date());
        dto.setUpdateTime(new Date());
        dto.setName("test");

        System.out.println(dto);

        System.out.println(JSON.toJSONString(dto));
    }
}

The test results are as follows:
Insert image description here

Guess you like

Origin blog.csdn.net/lzx5290/article/details/132475369