Gson 生成和解析带Date类型的json

来源:互联网 发布:淘宝运营助理工作职责 编辑:程序博客网 时间:2024/06/08 03:53

前后端交互数据的时候有一些包含Date类型的数据,普通转换会将时间转乱,需要特殊处理,这个问题困扰我很长时间,啊啊啊啊


/** * gson解析和生成带时间格式的数据 * @author Administrator */public class gsondate {static class User{Date brith;String name;}public static void main(String[] args) {Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create(); User user=new User();user.name="lmw";user.brith=new Date();System.out.println(gson.toJson(user));//Gson gson=new Gson();String jsonStr="{'brith':'2013-11-20','name':'lmw'}";User uu=gson.fromJson(jsonStr, User.class);System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(uu.brith));}}


原创粉丝点击