Gson日期格式化
Student类:
package ccc.json; import java.sql.Timestamp; import java.util.Date; public class Student { int id; String name; Date birthDate; Timestamp updateTime; public Student() { super(); } public Student(int id, String name, Date birthDate, Timestamp updateTime) { super(); this.id = id; this.name = name; this.birthDate = birthDate; this.updateTime = updateTime; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } public Timestamp getUpdateTime() { return updateTime; } public void setUpdateTime(Timestamp updateTime) { this.updateTime = updateTime; } }
主程序入口:
package ccc.json; import java.sql.Timestamp; import java.util.Date; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class Gson_demo2 { public static void main(String[] args) { Student stu = new Student(1, "bobo", new Date(), new Timestamp(System.currentTimeMillis())); Gson gson1 = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create(); String s = gson1.toJson(stu); System.out.println(s); // {"id":1,"name":"bobo","birthDate":"2015-05-22 14:09:33","updateTime":"2015-05-22 14:09:33"} } }
感觉空虚寂寞,只是因为你无所关注,无处付出。