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

相关文章:

  • 2022-12-23
  • 2021-12-07
  • 2021-11-18
  • 2022-12-23
  • 2022-12-23
  • 2021-07-30
  • 2022-12-23
  • 2022-01-26
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-21
  • 2021-10-16
  • 2022-01-06
相关资源
相似解决方案