How to Perform Accurate Validation of Date and Time Related Scenarios


While automating web applications at many places, we encounter scenarios where we need to manipulate the date time format.

To overcome this problem, date and time manipulation can be easily handled through Java programming.

Scenario -We need to verify whether the date and time shown in the web application while a user is logging in is the same as the actual date and time of the system. 

Approach – 
Store the date-time value shown on the application in a string and use the split() method in Java to split the string into 2 parts. 

For example, if the date and time of the user logging in is 6 Jun 2022  7:34:10 PM

This date has the format: dd mm yyyy HH:MM:ss a

Where – 
D – date
M – month
Y – year
H- hours
M – minutes
S – seconds
a – AM/  PM

Step 1 – Splitting the date accordingly using the split() method 

String DateTime = “ 6 Jun 2022  7:34:10 PM”;

arrOfStr is used mainly for splitting the string into arrays.

So when arrOfStr is used in the string , it splits the string into different parts.

The + operator is used to concatenate or combine the strings together after splitting.

Now,
Where  a = arrOfStr1[0]; = 6
b = arrOfStr1[1]; = June
c = arrOfStr1[2];  = 2022
d = arrOfStr1[3]; = 7
e = arrOfStr1[4]; = 34

dt is a variable used to store the value of the date after splitting .

The output will look like this:
================Splitting date ===================
6 Jun 2022
Now we just need to do the same for the time as well.

Now let’s assign the value of d2 = arrOfStr1[3];

Output
================Splitting time ===================
7:34 PM
Now convert the time to a 24-hour format as the system time that you will need to compare it with will also be in the 24-hour format.

We use a DateFormat class to parse the date to any format as per our wish.

Here we use the SimpleDateFormat class to convert any date-time into another format. 

HH – denotes hours in the 24-hour format

The variable output is used to store the new date-time in the 24-hour format.

As we need to return a string, we declare a function as a public string and return.

The output looks something like this:
==============Converting into the 24-hr time format================
19:34 PM

Finally, we need a method to join the newly-formatted date and time together.

Output:
==============Join Date and time ================
6 Jun 2022  19:34 PM

Now we can get the system time of the device.

The LocalDateTime class is used to represent a date-time without showing the time zone.
A method of LocalDatetime class is now () . It is mainly used to get the present date-time of the system.

We use the DateTimeFormatter class here again as we want the system time format to be similar to the format that is used in the web application.

The output looks something like this after execution:
6 Jun 2022  19:34 PM

We can observe that the date-time formats of the device in the web application and the system are the same.

Validation Step :

We add one final method to compare the 2 strings.

All that is left to do is to check whether both strings are equal to each other using Java’s built in equals() method

And if they are equal – the case has passed successfully
If they are not equal – then the case has failed

After all the methods are grouped together in the test class and after the case has been executed:

After successful completion of the execution,

the final console output would look something like this:

Thus, in this manner,you can use the concepts of DateTimeFormatter, SimpleDateFormat, and strings in Java to properly validate your test cases that require effective validation of data while being able to completely eliminate manual intervention.

Leave A Comment

Your email address will not be published. Required fields are marked *