GSON轉換日期資料為特定的JSON資料

來源:互聯網
上載者:User

標籤:pre   ssr   lxde   src   dsa   color   通過   wim   sep   

  通過JSON傳遞資料的時候經常需要傳遞日期,Java中可以通過GSON將日期轉換為特定格式的JSON資料。

1.普通的GSON轉換日期

    public void query(HttpServletRequest request, HttpServletResponse response, RegisterService service)            throws Exception {        Integer id = Integer.parseInt(request.getParameter("id"));        Register register = service.getRegisterById(id);        Gson gson = new Gson();        String json = gson.toJson(register);        System.out.println(json);        response.getWriter().write(json);    }

 轉換後的日期格式:

前台介面顯示:

 

2.如果需要將日期格式轉換為特定日期格式的資料:

    // 查詢    public void query(HttpServletRequest request, HttpServletResponse response, RegisterService service)            throws Exception {        Integer id = Integer.parseInt(request.getParameter("id"));        Register register = service.getRegisterById(id);        //轉換日期格式,也可以new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();         Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();        String json = gson.toJson(register);        System.out.println(json);        response.getWriter().write(json);    }

 

 轉換後的日期JSON:

前台介面顯示的日期格式:

 

GSON轉換日期資料為特定的JSON資料

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.