Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 63 – How to create JSON with date fields using POJO

Posted on 03/21/2025 By admin

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

Uncategorized

Post navigation

Previous Post: Ways of Handling StaleElementReferenceException Without PageFactory
Next Post: TestNG Tutorials 23: @Test Annotation – Don’t Confuse TestNG With Duplicate Priorities

Related Posts

December 13, 2018 – Make Selenium Easy Uncategorized
postman tutorials Uncategorized
Log4j2 Tutorial 1 – Introduction To Apache Log4j2 Uncategorized
TestNG Tutorials 49: Need of DataProvider Method in TestNG Uncategorized
Part 7: Usages Of Javascripts In Selenium: Difference Among ScrollBy, ScrollTo and Scroll Methods Of Javascript Uncategorized
Make Selenium Easy – Page 48 of 49 – Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark