GSON Format Date in JSON object

You can globally specify how to format dates in JSON objects using GSON. Very easy, you will only have to declare your custom date once. GSON Format Date method setDateFormat() witch takes an int a pattern or dateStyle and timeStyle which are both primitive int types.

Maven dependencies

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""




Object to be Serialised/Deserialised

package com.memorynotfound.json;

import java.util.Date;

public class Product {

    private long id;
    private String name;
    private Date created;
    private double amount;

    public Product(long id, String name, Date created, double amount) { = id; = name;
        this.created = created;
        this.amount = amount;

GSON Format Date in JSON object

You can call setDateFormat() multiple times, but only the last invocation will be used to decide the serialization format. The date format will be used to serialize and deserialize Date, Timestamp and Date.

Note that this pattern must comply with the convention provided by SimpleDateFormat class. See the documentation in SimpleDateFormat for more information on valid date and time patterns.
For the int types have a look at the constants defined in the DateFormat class. Here you will find options like: FULL, LONG, MEDIUM, SHORT and DEFAULT
package com.memorynotfound.json;


import java.util.Date;

public class FormatDate {

    public static void main(String... args){
        Product product = new Product(1, "Playstation 4", new Date(), 499.99);
        Gson gson = new GsonBuilder().setDateFormat("dd/MM/yyyy").create();
        String result = gson.toJson(product);


{"id":1,"name":"Playstation 4","created":"26/12/2014","amount":499.99}


You may also like...