REST Assured Tutorial 63 – How to create JSON with date fields using POJO
As a part of the End to End REST Assured Tutorial, in this post, we will learn to create a POJO class for JSON consisting of date fields.
{ "firstname" : "Jim", "lastname" : "Brown", "checkin" : "2018-01-01", "checkout" : "2019-01-01" }
If you observe the above JSON carefully you will notice that we have “checkin” and “checkout” fields which consist of date values. If we follow the approach explained in this post we can define both fields as String itself and provide required date values like checkin 10 days from today etc.
Let’s create a POJO class for the required JSON object as below:-
package PojoWithDateField; public class Booking1 { private String firstName; private String lastName; private String checkin; private String checkout; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getCheckin() { return checkin; } public void setCheckin(String checkin) { this.checkin = checkin; } public String getCheckout() { return checkout; } public void setCheckout(String checkout) { this.checkout = checkout; } }
We will use a LocalDate class to get the desired date. The default format of LocalDate is yyyy-MM-dd i.e. a date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03.
It obtains the current date from the system clock in the default time-zone. We can easily get past and future dates using the LocalDate class.
In this example program, I will not change the default format used by the LocalDate class i.e. yyyy-MM-dd.
package PojoWithDateField; import java.time.LocalDate; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; public class PojoToJson1 { public static void main(String[] args) throws JsonProcessingException { Booking1 booking1 = new Booking1(); booking1.setFirstName("Amod"); booking1.setLastName("Mahajan"); // default format is yyyy-MM-dd LocalDate currentDate = LocalDate.now(); // Get 10 days from current date booking1.setCheckin(currentDate.plusDays(10).toString()); // Get 20 days from current date booking1.setCheckout(currentDate.plusDays(20).toString()); // POJO to JSON ObjectMapper objectMapper = new ObjectMapper(); String s = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(booking1); System.out.println(s); // JSON to POJO Booking1 b2 = objectMapper.readValue(s, Booking1.class); System.out.println("Passed checkin date is "+ b2.getCheckin()); System.out.println("Passed checkout date is "+ b2.getCheckout()); } }
{ "firstName" : "Amod", "lastName" : "Mahajan", "checkin" : "2021-01-23", "checkout" : "2021-02-02" } Passed checkin date is 2021-01-23 Passed checkout date is 2021-02-02
It is highly possible that you need to pass the date in a specific format and the default format of LocalDate will not serve your purpose. There is no need to worry as LocalDate allows you to format a date output as you wish.
package PojoWithDateField; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; public class PojoToJson2 { public static void main(String[] args) throws JsonProcessingException { Booking1 booking1 = new Booking1(); booking1.setFirstName("Amod"); booking1.setLastName("Mahajan"); // default format is yyyy-MM-dd LocalDate currentDate = LocalDate.now(); // Get 10 days from current date booking1.setCheckin(currentDate.plusDays(10).format(DateTimeFormatter.ofPattern("dd-MMM-yyyy"))); // Get 20 days from current date booking1.setCheckout(currentDate.plusDays(20).format(DateTimeFormatter.ofPattern("dd-MMM-yyyy"))); // POJO to JSON ObjectMapper objectMapper = new ObjectMapper(); String s = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(booking1); System.out.println(s); // JSON to POJO Booking1 b2 = objectMapper.readValue(s, Booking1.class); System.out.println("Passed checkin date is "+ b2.getCheckin()); System.out.println("Passed checkout date is "+ b2.getCheckout()); } }
{ "firstName" : "Amod", "lastName" : "Mahajan", "checkin" : "23-Jan-2021", "checkout" : "02-Feb-2021" } Passed checkin date is 23-Jan-2021 Passed checkout date is 02-Feb-2021
If you want to include a timestamp as well we can use LocalDateTime class instead of LocalDate class as shown below:-
package PojoWithDateField; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; public class PojoToJson3 { public static void main(String[] args) throws JsonProcessingException { Booking1 booking1 = new Booking1(); booking1.setFirstName("Amod"); booking1.setLastName("Mahajan"); // default format is yyyy-MM-dd LocalDateTime currentDate = LocalDateTime.now(); // Get 10 days from current date booking1.setCheckin(currentDate.plusDays(10).format(DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss"))); // Get 20 days from current date booking1.setCheckout(currentDate.plusDays(20).format(DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss"))); // POJO to JSON ObjectMapper objectMapper = new ObjectMapper(); String s = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(booking1); System.out.println(s); // JSON to POJO Booking1 b2 = objectMapper.readValue(s, Booking1.class); System.out.println("Passed checkin date is "+ b2.getCheckin()); System.out.println("Passed checkout date is "+ b2.getCheckout()); } }
{ "firstName" : "Amod", "lastName" : "Mahajan", "checkin" : "24-Jan-202120:41:48", "checkout" : "03-Feb-2021 20:41:48" } Passed checkin date is 24-Jan-202120:41:48 Passed checkout date is 03-Feb-2021 20:41:48
You can download/clone the above sample project from here.
You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.
If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading
#HappyLearning