• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Java Concept / How to get current system date and time in Java/ Selenium

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

June 17, 2020 by Mukesh Otwani 19 Comments

How to get current system date and time in JavaSelenium2

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

package demo;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class GetDateinJava {

 public static void main(String[] args) {
 
 // Create object of SimpleDateFormat class and decide the format
 DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy ");
 
 //get current date time with Date()
 Date date = new Date();
 
 // Now format the date
 String date1= dateFormat.format(date);
 
 // Print the Date
 System.out.println(date1);
 
 }

}

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

package demo;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class GetDateinJava {

 public static void main(String[] args) {
 
 // Create object of SimpleDateFormat class and decide the format
 DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
 
 //get current date time with Date()
 Date date = new Date();
 
 // Now format the date
 String date1= dateFormat.format(date);

 // Print the Date
 System.out.println("Current date and time is " +date1);
 
 }

}

OUTPUT

How to get current system date and time in JavaSelenium

Program 3- Write a program which will capture Only Current date

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateExample {

	public static void main(String[] args) {
		
                // here we are only capturing current date, which you can use for your automated scripts.
		DateFormat dateFormat = new SimpleDateFormat("dd");
		 
		 //get current date time with Date()
		 Date date = new Date();
		 
		 // Now format the date
		 String date1= dateFormat.format(date);
		 
		 System.out.println(date1);

	}

}

Output

Current Date

 

I hope now you are comfortable with Dates now. Let me know if you are facing any issues.

For More updates Learn Automation page

For any query join Selenium group- Selenium Group

Filed Under: Java Concept Tagged With: Java Date and Time

Reader Interactions

Comments

  1. Devi says

    October 19, 2022 at 2:09 PM

    Hi Mukesh,

    I need your help with the date and time assertion. My assertion is passing in local but in Travis, it’s failing, and I am seeing the difference of 5 and half hours
    [2022-10-17 15:13:29] [+0000] [INFO] [org.junit.ComparisonFailure: expected: but was:]
    In Travis, the timezone is – export TZ=America/New_York. So I tried in many ways setting lang=en-us but did not work. Could you please let me know if there is any solution for this time comparision

    Reply
    • Mukesh Otwani says

      October 20, 2022 at 12:42 PM

      Hi Devi, I have not worked on travis CI but hope below thread will work https://stackoverflow.com/questions/23371542/how-do-i-configure-travis-ci-to-use-the-correct-time-zone-for-a-rails-app

      Reply
  2. QA says

    January 21, 2021 at 2:54 PM

    How to capture current date and time of a country of City?

    Reply
    • Mukesh Otwani says

      January 22, 2021 at 6:21 PM

      Hi Priya,

      For this, you need to use some APIs that provide data. As of now, I don’t have ready code for this. Please check this link https://stackoverflow.com/questions/55901/web-service-current-time-zone-for-a-city

      Reply
  3. Beula says

    June 12, 2019 at 12:04 PM

    Hello sir, I am taking multiple inputs from excel sheet and running a test case . If 2 or more input tests fail then also this will create single screenshot returns the same name on every failure so this will create a single screenshot every time due to the same name. Please tell me how to set a different name to every failure test screenshot?

    Reply
    • Mukesh Otwani says

      June 13, 2019 at 6:51 PM

      Hi Beula,

      While naming screenshot file name, use some dynamic name over there like current System date time(including minutes and seconds).
      Example: methodWhichReturnsCurrentDateTime+”.png”

      Reply
  4. rajesh says

    October 29, 2016 at 1:24 PM

    Hi,
    How to select date when it is dynamically changing every day
    for any sites when we use for transportation booking
    how to select current date from calendar
    and another date for return booking

    Reply
    • Mukesh Otwani says

      October 31, 2016 at 3:38 AM

      Hi Rajesh,

      Below post will guide you for the same.

      http://learn-automation.com/selenium-advance-activity/

      Reply
  5. minnu says

    August 8, 2016 at 4:00 PM

    how to print current date and time in the emailable-report.html….can u give any suggestions???

    Reply
    • Mukesh Otwani says

      August 18, 2016 at 10:11 AM

      Hi Minnnu,

      Kindly use below report learn-automation.com/advance-selenium-reporting-with-screenshots-2/

      Reply
  6. Mukesh says

    June 27, 2016 at 2:45 PM

    Hi mukesh, I am using the same method to check the current date in my application. however the test cases are failed if they are executed by my onsite folks(USA).

    Reply
    • Mukesh Otwani says

      July 2, 2016 at 3:10 PM

      Hey Mukesh,

      What error or exception it is throwing?

      Reply
  7. Mallika says

    May 31, 2016 at 11:03 AM

    Thank you very much it worked for me.

    Reply
  8. Manoj says

    April 7, 2016 at 10:36 AM

    hello sir, using selenium can we run a script at a particular time of a day or on a regular basis. Suppose I have to send an email or a message to one of my friend at 2pm, and I had already written the message in a selenium script and i want that message to be send at that time of day only

    Reply
    • Mukesh Otwani says

      April 10, 2016 at 12:21 PM

      Yes manoj using jenkins we can do that http://learn-automation.com/selenium-integration-with-jenkins/

      Reply
  9. Algirdas says

    April 1, 2016 at 4:06 PM

    Hi, make sure you are using correct letter pattern.
    mm is not the same as MM.
    m – Minute in hour
    M – Month in year

    ref.: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

    Hope it will helped 😉
    Al

    Reply
    • Mukesh Otwani says

      April 1, 2016 at 4:55 PM

      Thanks Algirdas

      Reply
  10. mahesh says

    March 10, 2016 at 3:35 PM

    Hi Mukesh,

    While executing my program, the month is changing repetitively for every run of the program.The output should be 2016/03/10.

    But the result is ,
    First run output1: 2016/29/10
    Second run output2: 2016/32/10

    Can u tell me which it is changing ??

    Thanks in advance.

    Reply
    • Mukesh Otwani says

      March 11, 2016 at 5:17 PM

      Hi Mahesh,

      not sure I will check and let u know.

      Reply

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • Syllabus For Playwright Online Training Program
  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?