Deserialize Custom Date Format using Jackson in Spring Boot Java Application

[8757 views]




Dates are often written in different formats in different applications. So there is always a need to create custom parser which will parse that date format and convert it into proper Date object for our application. In this article, we will learn how to create such Custom Date Deserializer for Spring Boot Application using Jackson.

Deserialize Custom Date Format using Jackson in Spring Boot Application

Let's first create our Custom Date Deserializer Class which will help us convert Dates received in "dd-MM-yyyy HH:mm" format to our Date Object. For Creating, such Deserializer class, you need to extend your Class with JsonDeserializer where T will be the Class type you want to Deserialize. Once you extend your Class with JsonDeserializer, you need to override deserialize method and write your Deserialization logic inside this method. Check below our example for Custom Date Deserializer.

CustomJsonDateDeserializer.java:

package com.testing.myproject.util; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.JsonProcessingException; import org.codehaus.jackson.map.DeserializationContext; import org.codehaus.jackson.map.JsonDeserializer; public class CustomJsonDateDeserializer extends JsonDeserializer<Date> { @Override public Date deserialize(JsonParser jsonparser, DeserializationContext context) throws IOException, JsonProcessingException { SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm"); String date = jsonparser.getText(); try { return format.parse(date); } catch (ParseException e) { throw new RuntimeException(e); } } }

Now wherever you want to deserialize your Date which comes in above format. You just have to annotate that Date field or Date field's setter method with @JsonDeserialize(using = CustomJsonDateDeserializer.class). Check below our POJO Class example for reference (private Date insertionDate):


Podcast.java

package com.testing.myproject.entities; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import org.codehaus.jackson.map.annotate.JsonDeserialize; import org.codehaus.jackson.map.annotate.JsonSerialize; import org.codingpedia.demo.rest.service.helper.CustomJsonDateDeserializer; import org.codingpedia.demo.rest.service.helper.CustomJsonDateSerializer; @Entity @Table(name="podcasts") public class Podcast implements Serializable { private static final long serialVersionUID = -8039686696076337053L; /** id of the podcast */ @Id @GeneratedValue private Long id; /** title of the podcast */ private String title; private String linkOnPodcastpedia; /** url of the feed */ private String feed; /** description of the podcast */ private String description; /** insertion date in the database */ @JsonDeserialize(using = CustomJsonDateDeserializer.class) private Date insertionDate; public Podcast(){} public Podcast(String title, String linkOnPodcastpedia, String feed, String description) { this.title = title; this.linkOnPodcastpedia = linkOnPodcastpedia; this.feed = feed; this.description = description; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getLinkOnPodcastpedia() { return linkOnPodcastpedia; } public void setLinkOnPodcastpedia(String linkOnPodcastpedia) { this.linkOnPodcastpedia = linkOnPodcastpedia; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getFeed() { return feed; } public void setFeed(String feed) { this.feed = feed; } public Date getInsertionDate() { return insertionDate; } public void setInsertionDate(Date insertionDate) { this.insertionDate = insertionDate; } }
                 



Clear Any Java Interview by Reading our Ebook Once.


Can you clear Java Interview?



Comments










Search

Hot Deals ends in













Technical Quizzes Specially For You:

Games:
     



Search Tags

    Custom Date Format Deserializer using Jackson in Java

    Java Application Custom Date Deserializer

    how to use Custom class in JsonDeserialize annotation