Jackson2.1.4シーケンス化フォーマット時間
5453 ワード
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-01 T 00:00:00.000+0000」と同様の出力が得られる.@JsonFormat(pattern=「yyyy-MM-dd HH:mm:ss」、timezone=「GMT+8」)は、クラスの属性でカスタマイズするシーケンス化timezone=「GMT+8」を必要とする属性をカスタマイズすることもできる.ここでは中国のタイムゾーン東8区カスタムフォーマットクラスJsonSerializer
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.