new Gson().toJson日期转特定格式日期实体

Google的Gson功能非常强大!

格式化日期我们只需要这样创建就好了

Student stu = new Student();

 stu.setName("张三");

stu.setBirthDay(new Date());

如果张三的生日是日期型的话,直接实体转json的话,

new Gson().toJson(stu);

日期会附带星期几 等奇怪的格式,如果想转常用的yyyy-MM-dd hh:mm:ss 字符串格式怎么办呢?

Gson  gson = new GsonBuilder().setDateFormat("yyyy-MM-dd hh:mm:ss).create();

System.out.print(gson.toJson(stu));

常见日期格式:

yyyy-MM-dd

yyyy-MM-dd hh:mm:ss

MM-dd-yyyy hh:mm

用Gson格式化之后,还用Gson解析。

猜你喜欢

转载自blog.csdn.net/qq_43842093/article/details/124956192
new