How to create Date Object and Compare Dates

0 votes

I am n tot able to convert/compare dates extracted from a calendar to the current date My aim is to evaluate the current date against the dates in the rows/cell and select/click the button beside the row/cell. But it is throwing an error

Below is my code:

 for (WebElement pd: payDates) {

  LocalDate currentDate = LocalDate.now();

  java.util.Date d = new SimpleDateFormat("yyyy-MM-dd").parse(currentDate.toString());

  if (pd >= (d)) {

   driver.findElement(By.xpath("//tr[starts-with(@id,'changeStartWeekGrid_row_')and not(starts-with(@id,'changeStartWeekGrid_row_column'))]/td[5]/span'" + reqIndex + "])/TBODY[@id='changeStartWeekGrid_rows_tbody']/TR[7]/TD[1]/DIV[1]/DIV[1]/DIV[1]")).click();

   PS_OBJ_CycleData.donebtn(driver).click();

   break;

  } else {

   reqIndex++;

   PS_OBJ_CycleData.Nextbtn(driver).click();

  }

 }

} while (reqIndex < 7); /// do this 7 times;

int reqIndex = 0;

 dowhileloop: do {

 List < WebElement > payDates = driver.findElements(By.xpath("//tr[starts-with(@id,'changeStartWeekGrid_row_')and not(starts-with(@id,'changeStartWeekGrid_row_column'))]/td[5]/span"));

List < String > texts = payDates.stream().map(WebElement::getText).collect(Collectors.toList());

   System.out.println("date ->" + texts);

   for (WebElement pd: payDates) { 

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

    Date payDate = dateFormat.parse(pd.getText());

    System.out.println("sample-> " + dateFormat.format(payDate));

    if (payDate.after(new Date())) {

     System.out.println("inside for loop");

     driver.findElement(By.xpath("//tr[starts-with(@id,'changeStartWeekGrid_row_')and not(starts-with(@id,'changeStartWeekGrid_row_column'))]/td[5]/span'" + reqIndex + "])/TBODY[@id='changeStartWeekGrid_rows_tbody']/TR[7]/TD[1]/DIV[1]/DIV[1]/DIV[1]")).click();

     PS_OBJ_CycleData.donebtn(driver).click();

     break dowhileloop;

    }

   } 

   reqIndex++;

   PS_OBJ_CycleData.Nextbtn(driver).click();

   Thread.sleep(5000);
May 4, 2018 in Selenium by Martin
• 4,320 points
1,135 views

1 answer to this question.

0 votes
You can convert pd to date (or better, its epoch as long (date is deprecated)) and do d.compare instead of >=. Hope this will help.
answered May 4, 2018 by Samarpit
• 5,910 points

Related Questions In Selenium

0 votes
1 answer

How to Create Object Repository in Selenium Web Driver?

Try POM Model (Page object model). Refer ...READ MORE

answered Apr 17, 2018 in Selenium by adam
1,688 views
0 votes
1 answer

How to create a executable jar file for TestNG and the runnnig point should be the Xml file

Use Eclipse Export Wizard. While exporting, select "Create ...READ MORE

answered Aug 24, 2018 in Selenium by Meci Matt
• 9,460 points
7,645 views
0 votes
1 answer
0 votes
1 answer

How to create a Base Class in Selenium and use it with test scripts?

Hi Donna, you can use following code ...READ MORE

answered Jul 18, 2019 in Selenium by Anvi
• 14,150 points
12,016 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
12,619 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,572 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
6,629 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,519 views
0 votes
2 answers

How to test React JS dropdown with Selenium and java?

First of all you need to click ...READ MORE

answered Aug 20, 2019 in Selenium by ghoshPooja
• 140 points
16,857 views
0 votes
1 answer

How to get next sibling element using XPath and Selenium for Java?

Below code will help you: Try following-sibling axis : WebElement ...READ MORE

answered May 15, 2018 in Selenium by Samarpit
• 5,910 points
20,280 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP