Posted In: GSON, JSON

How to map multiple date formats using Gson?

Use Case – I want to map multiple date formats to Gson while converting date string to POJO

Use Jackson if possible for this requirement. It has much easier to use annotations to achieve this how-to-map-multiple-date-formats-using-jackson-mapper

Maven – Gson

<<dependency>
	<groupId>com.google.code.gson</groupId>
	<artifactId>gson</artifactId>
	<version>2.8.5</version>
</dependency>

Solution – Use JsonDeserializer

JsonDeserializer, gsonBuilder.registerTypeAdapter(Date.class, new DateDeserializer()) and SimpleDateFormat

package com.example.json;

import java.io.Reader;
import java.io.Serializable;
import java.io.StringReader;
import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

public class GsonDateformat2 {
	GsonBuilder gsonBuilder = new GsonBuilder();

	public static void main(String[] args) throws Exception {
		GsonDateformat2 jacksonDateformat = new GsonDateformat2();
		jacksonDateformat.dateformat();
	}

	public void dateformat() throws Exception {
		String json = "{\"createDate\":\"1976/09/08\"," + "\"createDateGmt\":\"2015-10-1 3:00 PM GMT+1:00\"}";
		Reader reader = new StringReader(json);
		gsonBuilder.registerTypeAdapter(Date.class, new DateDeserializer());
		Gson gson = gsonBuilder.create();
		Employee5 employee = gson.fromJson(reader, Employee5.class);
		System.out.println(employee);
	}
}

class DateDeserializer implements JsonDeserializer<Date> {
	public Date deserialize(JsonElement element, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
		String date = element.getAsString();
		SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy/MM/dd");
		formatter1.setTimeZone(TimeZone.getTimeZone("UTC"));
		try {
			return formatter1.parse(date);
		} catch (ParseException e) {
			System.out.println("Failed to parse Date due to:" + e);
		}
		SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm a z");
		formatter2.setTimeZone(TimeZone.getTimeZone("UTC"));
		try {
			return formatter2.parse(date);
		} catch (ParseException e) {
			System.out.println("Failed to parse Date due to:" + e);
		}
		return null;
	}
}

class Employee5 implements Serializable {
	private static final long serialVersionUID = 5590115870809984678L;
	private Date createDate;
	private Date createDateGmt;

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Date getCreateDateGmt() {
		return createDateGmt;
	}

	public void setCreateDateGmt(Date createDateGmt) {
		this.createDateGmt = createDateGmt;
	}

	@Override
	public String toString() {
		return "Employee5 [createDate=" + createDate + ", createDateGmt=" + createDateGmt + "]";
	}
}

Tags:

by , on December 5th, 2018