Parse JSON String Using Date Format in Java

Parsing JSON in java is quite straight using lib like Gson and Jackson. Gson is light weight lib and very handy to use. We can parse json string containing dates by specifying format using Gson.

Example for parsing json string with date format


public class GsonParserWithDateFormatExample {
    class Info{
        private String name;
        private Date dob;
        public String getName() {
            return name;
        public void setName(String name) {
   = name;
        public Date getDob() {
            return dob;
        public void setDob(Date dob) {
            this.dob = dob;
    public static void main(String[] args) throws Exception {
        String date ="1980-01-15 16:45";
        String json = "{"name":"ssp","dob":"1980-01-15 16:45"}";
        Info obj = parseJson(json, Info.class);

    public static <T> T parseJson(String json, Class<T> type) throws Exception {
        T result = null;
        try {
            // Gson gson = new Gson();
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm")
            result = gson.fromJson(json, type);
        } catch (Exception e) {
            throw new Exception("Invalid data");
        return result;

Here are maven and gradle entries for Gson lib