How to get current system date and time in Java/ Selenium

Hello Guys, Welcome back to Selenium tutorial today we will see some new topic which might help you in some cases where you have to handle with  dates. Today I will share How to get current system date and time in Java/ Selenium so that you can verify the same in your test cases.

 

How to get current system date and time in JavaSelenium2

How to get current system date and time in Java/ Selenium

Today I was automating some of my test cases and I got one requirement where I have to validate some record with date and time. In this case I captured date and time and verified respectively.

 

Step which I have taken

1- Decide which date format you want, then we can use SimpleDateFormat class to do the same. I took below format

SimpleDateFormat(“MM/dd/yyyy”)

2- Create Date class object which will return Date and time

3- Then using format method get the date and time.

Program 1- Write a program which will capture System Date

OUTPUT

How to get current system date and time in Java/ Selenium

 

Program 2- Write a program which will capture System Date/ Time

In this example I am taking time as well so I have added HH:MM:SS also in format

OUTPUT

How to get current system date and time in JavaSelenium

Hope you are comfortable with Dates now.Let me know if you are facing any issue.

For More updates Learn Automation page

For any query join Selenium group- Selenium Group

13 Comments

  1. rajesh
  2. minnu
  3. Mukesh
  4. Mallika
  5. Manoj
  6. mahesh