Gson 处理 Date LocalDate LocalDateTime 日期类型JSON格式字符串
要在使用Gson库进行属性为
Date
、LocalDate
和LocalDateTime
的对象的序列化和反序列化时,
可以使用注解来指定日期的格式化方式。Gson库支持@SerializedName
和@JsonAdapter
注解。
@SerializedName注解:用于指定JSON属性的名称。可以将@SerializedName注解应用在对象的属性上,指定对应的JSON属性名称。
@JsonAdapter注解:用于指定自定义的JsonAdapter类。可以将@JsonAdapter注解应用在对象的属性上,指定对应的JsonAdapter类来进行日期的格式化和反格式化。
下面是一个示例代码,演示如何使用注解来格式化输出属性为Date、LocalDate和LocalDateTime的对象:
Gson 注解方式处理 Date LocalDate LocalDateTime
准备 DateAdapter
package com.lihaozhe.util.json.gson;
import com.google.gson.*;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author 李昊哲
* @version 1.0
* @create 2023/10/19
*/
public class DateAdapter implements JsonDeserializer<Date>, JsonSerializer<Date> {
private final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(dateFormat.format(src));
}
@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
try {
return dateFormat.parse(json.getAsString());
} catch (Exception e) {
throw new JsonParseException(e);
}
}
}
准备 LocalDateAdapter
package com.lihaozhe.util.json.gson;
/**
* @author 李昊哲
* @version 1.0
* @create 2023/10/19
*/
import com.google.gson.*;
import java.lang.reflect.Type;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateAdapter implements JsonDeserializer<LocalDate>, JsonSerializer<LocalDate> {
private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
@Override
public JsonElement serialize(LocalDate src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(dateTimeFormatter.format(src));
}
@Override
public LocalDate deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
try {
return LocalDate.parse(json.getAsString(), dateTimeFormatter);
} catch (Exception e) {
throw new JsonParseException(e);
}
}
}
准备 LocalDateTimeAdapter
package com.lihaozhe.util.json.gson;
import com.google.gson.*;
import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @author 李昊哲
* @version 1.0
* @create 2023/10/19
*/
public class LocalDateTimeAdapter implements JsonDeserializer<LocalDateTime>, JsonSerializer<LocalDateTime> {
private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
@Override
public JsonElement serialize(LocalDateTime src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(dateTimeFormatter.format(src));
}
@Override
public LocalDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
try {
return LocalDateTime.parse(json.getAsString(), dateTimeFormatter);
} catch (Exception e) {
throw new JsonParseException(e);
}
}
}
准备javabean
package com.lihaozhe.json.gson;
import com.google.gson.annotations.JsonAdapter;
import com.lihaozhe.util.date.DateUtils;
import com.lihaozhe.util.json.gson.DateAdapter;
import com.lihaozhe.util.json.gson.LocalDateAdapter;
import com.lihaozhe.util.json.gson.LocalDateTimeAdapter;
import lombok.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
/**
* @author 李昊哲
* @version 1.0
* @create 2023/10/19
*/
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Emp {
/**
* 账号
*/
private String account;
/**
* 密码
*/
private String password;
/**
* 姓名
*/
private String name;
/**
* 性别 1 代表男性 0 代表女性
*/
private int gender;
/**
* 入职时间
*/
@JsonAdapter(DateAdapter.class)
private Date hiredate;
/**
* 离职日期
*/
@JsonAdapter(DateAdapter.class)
private Date turnoverDate;
/**
* 账号注册日期
*/
@JsonAdapter(LocalDateAdapter.class)
private LocalDate createDate;
/**
* 账号注册日期时间
*/
@JsonAdapter(LocalDateTimeAdapter.class)
private LocalDateTime createDateTime;
public Emp(String account, String password, String name, int gender) {
this.account = account;
this.password = password;
this.name = name;
this.gender = gender;
this.hiredate = new Date();
this.createDate = LocalDate.now();
this.createDateTime = LocalDateTime.now();
}
}
javabean转json格式字符串
Emp emp = new Emp("admin", "e10adc3949ba59abbe56e057f20f883e", "李昊哲", 1);
System.out.println(emp);
Gson gson = new Gson();
String json = gson.toJson(emp);
System.out.println(json);
结果如下:
Emp(account=admin, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:20:29 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:20:29.118)
{"account":"admin","password":"e10adc3949ba59abbe56e057f20f883e","name":"李昊哲","gender":1,"hiredate":"2023-10-19 19:20:29","createDate":"2023-10-19","createDateTime":"2023-10-19 19:20:29"}
json格式字符串转javabean
Emp emp = new Emp("admin", "e10adc3949ba59abbe56e057f20f883e", "李昊哲", 1);
System.out.println(emp);
Gson gson = new Gson();
String json = gson.toJson(emp);
System.out.println(json);
Emp fromJson = gson.fromJson(json, Emp.class);
System.out.println(fromJson);
结果如下:
Emp(account=admin, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:20:29 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:20:29.118)
{"account":"admin","password":"e10adc3949ba59abbe56e057f20f883e","name":"李昊哲","gender":1,"hiredate":"2023-10-19 19:20:29","createDate":"2023-10-19","createDateTime":"2023-10-19 19:20:29"}
Emp(account=admin, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:20:29 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:20:29.118)
转自:Gson FastJson Jackson 出来 Date LocalDate LocalDateTime 日期类型JSON格式字符串_jackson处理日期-CSDN博客