Make Selenium Easy

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.

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

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

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

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.

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

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

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
%d bloggers like this: