Gson – Date format Java example shows how to format a date in the JSON using the Gson library in Java. This example also shows how to format a date in JSON using the GsonBuilder class.
How to format a date in JSON?
When we serialize an object to a JSON representation using the Gson library, any date fields that the object may have will have the date in the default format. Consider the below given example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
package com.javacodeexamples.gsonexamples; import java.util.Calendar; import java.util.Date; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class GsonDateFormatExample { public static void main(String[] args) { Employee emp = new Employee("EMP001", "Bob", Calendar.getInstance().getTime()); Gson gson = new GsonBuilder() .setPrettyPrinting() .create(); System.out.println(gson.toJson(emp)); } } class Employee{ private String id; private String name; private Date dateOfJoining; public Employee(String id, String name, Date dateofJoining) { this.id = id; this.name = name; this.dateOfJoining = dateofJoining; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getDateOfJoining() { return dateOfJoining; } public void setDateOfJoining(Date dateOfJoining) { this.dateOfJoining = dateOfJoining; } } |
Output
1 2 3 4 5 |
{ "id": "EMP001", "name": "Bob", "dateOfJoining": "Mar 28, 2023 3:39:39 PM" } |
As we can see from the output, the “dateOfJoining” field contains the date in the default format.
However, it is fairly easy to format the date field in JSON using the setDateFormat
method of the Gson class.
1 |
public GsonBuilder setDateFormat(java.lang.String pattern) |
This method sets the Gson object to serialize any Date objects in the format of the provided pattern. The pattern will be applied to any objects of the class Date and Timestamp.
The pattern needs to be as defined by the SimpleDateFormat class.
Here is an example that sets the date format to dd-mm-yyyy using the above method.
1 2 3 4 5 6 7 8 9 10 11 |
Employee emp = new Employee("EMP001", "Bob", Calendar.getInstance().getTime()); /* * sets the date format to dd-mm-yyyy */ Gson gson = new GsonBuilder() .setPrettyPrinting() .setDateFormat("dd-MM-yyyy") .create(); System.out.println(gson.toJson(emp)); |
Output
1 2 3 4 5 |
{ "id": "EMP001", "name": "Bob", "dateOfJoining": "28-03-2023" } |
We can also format the date to include hours, minutes, seconds, and even milliseconds as given below.
1 2 3 4 |
Gson gson = new GsonBuilder() .setPrettyPrinting() .setDateFormat("dd MM yyyy HH:mm:ss:SSS") .create(); |
Output
1 2 3 4 5 |
{ "id": "EMP001", "name": "Bob", "dateOfJoining": "28 03 2023 15:56:12:001" } |
The list of all supported formats can be found in this SimpleDateFormat class documentation.
Please let me know your views in the comments section below.