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,220 points
35 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,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
632 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,420 points
2,027 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 in Selenium by Anvi
• 13,960 points
186 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,110 points
908 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
1,099 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
832 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 in Selenium by ghoshPooja
• 140 points
3,500 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
6,392 views