<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
在使用fastjson中,需要对输出的时间进行格式化的操作,有两种方式:
使用@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss" ,timezone="GMT+8")
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Date;
public class student{
private int id;
private String name;
private int age;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss" ,timezone="GMT+8")
private Date birthday;
}
测试
student tom = new student(1, "tom", 22, new Date());
ObjectMapper mapper = new ObjectMapper(); //需要序列化
System.out.println(mapper.writeValueAsString(tom));//需要序列化
第二种方法:
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Date;
public class student{
private int id;
private String name;
private int age;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date birthday;
}
student tom = new student(1, "tom", 22, new Date());
System.out.println(JSON.toJSONString(tom));
//输出结果
{"age":22,"birthday":"2020-04-20 15:45:25","id":1,"name":"tom"}