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,210 points
19 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,130 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
367 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,400 points
835 views
0 votes
1 answer

Python & Selenium: How to find and click on YouTube's Like button

Use the CSS Selector button to like: #watch8-sentiment-actions ...READ MORE

answered Mar 26, 2018 in Selenium by nsv999
• 5,030 points
431 views
0 votes
1 answer

How to select an Object by its class in Selenium?

When it comes to Selenium, XPath will ...READ MORE

answered Apr 13, 2018 in Selenium by king_kenny
• 3,590 points
22 views
0 votes
1 answer

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,030 points
71 views
0 votes
1 answer
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,130 points
441 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
55 views
0 votes
1 answer

How to test React JS dropdown with Selenium and java?

Below code will help you: Actions act = ...READ MORE

answered May 14, 2018 in Selenium by Samarpit
• 5,130 points
2,043 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,130 points
3,776 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.