Google Gson 格式化日期时间

x33g5p2x  于2021-12-25 转载在 Go  
字(0.5k)|赞(0)|评价(0)|浏览(376)

本文内容大多基于官方文档和网上前辈经验总结,经过个人实践加以整理积累,仅供参考。

示例 1

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

运行结果:

示例 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()));
}

运行结果:

示例 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()));
}

运行结果:

相关文章