Make Selenium Easy

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

Introduction

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.

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.

Create POJO with date fields as strings

Let’s create a POJO class for the required JSON object as below:-

Create JSON using POJO

LocalDate Class

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.

With default format used by LocalDate

In this example program, I will not change the default format used by the LocalDate class i.e. yyyy-MM-dd.

Example program
Output

With a custom format of a date

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.

Example program
Output

with timestamp

If you want to include a timestamp as well we can use LocalDateTime class instead of LocalDate class as shown below:-

Example Program
Output

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

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

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

by Amod Mahajan time to read: 3 min
0
%d bloggers like this: