编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

Google Gson 格式化日期时间

wxchong 2024-06-13 03:22:12 开源技术 14 ℃ 0 评论

示例 1


  1. @Test
  2. public void test() throws IOException {
  3. Gson gson = new GsonBuilder()
  4. .setDateFormat("yyyy-MM-dd")
  5. .create();
  6. System.out.println(gson.toJson(new Date()));
  7. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

运行结果:

示例 2


  1. @Test
  2. public void test() throws IOException {
  3. Gson gson = new GsonBuilder()
  4. .setDateFormat("yyyy-MM-dd hh:mm:ss")
  5. .create();
  6. System.out.println(gson.toJson(new Date()));
  7. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

运行结果:

示例 3


  1. @Test
  2. public void test() throws IOException {
  3. Gson gson = new GsonBuilder()
  4. .setDateFormat("MM-dd-yyyy hh:mm")
  5. .create();
  6. System.out.println(gson.toJson(new Date()));
  7. }

运行结果:

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表