Jackson2.1.4 序列化格式化时间
2013-08-23 16:17
190 查看
public class User { private int id; private Date birthday; private double money; private String name; public User() { } public User(int id, String name, Date birthday) { super(); this.id = id; this.name = name; this.birthday = birthday; } public User(int id, String name, double money, Date birthday) { super(); this.id = id; this.name = name; this.money = money; this.birthday = birthday; } public Date getBirthday() { return birthday; } public int getId() { return id; } public double getMoney() { return money; } public String getName() { return name; } public void setBirthday(Date birthday) { this.birthday = birthday; } public void setId(int id) { this.id = id; } public void setMoney(double money) { this.money = money; } public void setName(String name) { this.name = name; } }
结果{"id":1,"name":"JACK","money":0.0,"birthday":"2013-04-01"}
objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS,FALSE);
禁止使用时间戳(数字),而使用[ISO-8601标准的符号,它得到类似的输出:“1970-01-01T00:00:00.000 +0000”。
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
也可以再类的属性上自定义哪些属性需要自定义序列化
timezone="GMT+8" 这里是中国的时区 东8区
自定义格式化类
JsonSerializer<T>
@JsonSerialize(user=xxx.class)
ObjectMapper mapper = new ObjectMapper(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); mapper.setDateFormat(format); User user = new User(1,"JACK",new Date()); String outJson = mapper.writeValueAsString(user); System.out.println(outJson);
Notes on java.sql.Date (官方不建议使用此类型)
(aka "Please do NOT use java.sql.Date, ever!")
Although Jackson supports java.sql.Date, there are known issues with respect to timezone handling, partly due to design of this class. It is recommended that this type is avoided, if possible, and regular java.util.Date (orjava.util.Calendar) used instead. If this is not possible, it may be necessary for applications to convert these dates using java.util.Calendar and explicit timezone definition.
相关文章推荐
- 解决jackson序列化时间对象需要格式化的问题
- jackson2.8.4java对象序列化成json字符串格式化时间
- 基于jackson注释@JsonFormat 格式化时间少8小时
- Jackson 时间格式化输入输出
- JSON——Jackson 时间格式化输入输出
- Newtonsoft.Json序列化 对时间格式化处理
- Jackson格式化时间
- 表单序列化json字符串和js时间格式化
- 在SpringMVC中使用Jackson并格式化时间
- Jackson 时间格式化,时间注解 @JsonFormat 用法、时差问题说明
- Jackson 时间格式化,时间注解 @JsonFormat 用法、时差问题说明
- 在SpringMVC中使用Jackson并格式化时间
- Jackson 时间格式化,时间注解 @JsonFormat 用法、时差问题说明
- Jackson2.1.4 序列化对象时对属性的过滤
- 基于jackson注释@JsonFormat 格式化时间少8小时
- jackson中JsonFormat格式化时间少8小时
- jackson2.1.4 序列化 通过给定Class映射 与抽象类的映射
- Jackson2.1.4 序列化对象时,过滤null的属性 empty的属性 default的属性
- jackson序列化接口将Date转为格式化好的String类型
- Java 时间格式化