Google Gson 格式化日期时间

示例 1

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

运行结果: 
这里写图片描述

示例 2

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

运行结果: 
这里写图片描述

示例 3

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

运行结果: 
这里写图片描述

猜你喜欢

转载自blog.csdn.net/suyimin2010/article/details/81230320