-2

I am having trouble converting/comparing dates extracted from a calendar to the current date I get a red line in eclipse under my if statement My goal is to evaluate the current date against the dates in the rows/cell and select/click the button beside the row/cell.

however to get to the second part i need to correctly evaluate the 1st part which is the dates.

my code is below:

 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;

------------ this works for me thanks for all your help -----------------
 int reqIndex = 0;

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

   // 5th colunm date print test
   List < String > texts = payDates.stream().map(WebElement::getText).collect(Collectors.toList());
   System.out.println("date ->" + texts);

   //** Begin third inner for-loop****
   for (WebElement pd: payDates) { // begin inner for loop
    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;
    }

   } //** END third inner for-loop****
   reqIndex++;
   PS_OBJ_CycleData.Nextbtn(driver).click();
   Thread.sleep(5000);
Jonathan
  • 395
  • 2
  • 8
  • 25
  • Do i need to convert payDates from webElemnt to a string or date format? if so how? – Jonathan Mar 17 '18 at 22:00
  • You need to explain to us what *exactly* is in a `pd` object, a `WebElement`? Is that text? If so, what kind of text? – Basil Bourque Mar 18 '18 at 01:24
  • yes pd holds the calendar date text in string format – Jonathan Mar 18 '18 at 01:33
  • And exactly what calendar date text might that be? The suspense is killing us. – Basil Bourque Mar 18 '18 at 01:43
  • Possible duplicate of [How to check if a date is greater than another in Java?](https://stackoverflow.com/questions/19109960/how-to-check-if-a-date-is-greater-than-another-in-java) – JeffC Mar 18 '18 at 02:50
  • Possible duplicate of [How to compare dates in Java?](https://stackoverflow.com/questions/2592501/how-to-compare-dates-in-java) – Ole V.V. Mar 19 '18 at 08:43
  • Since you can use `LocalDate` from java.time, the modern Java date and time API, use this API exclusively. Avoid the outdated `Date` and `Calendar` and certainly the notoriously troublesome `SimpleDateFormat`. The modern API is so much nicer to work with and contains all the functionality you could wish for. – Ole V.V. Mar 19 '18 at 08:45
  • okay thanks however i found the answer that works for me its above – Jonathan Mar 19 '18 at 13:01

2 Answers2

3

tl;dr

  • Avoid legacy date-time classes (Date, Calendar, SimpleDateFormat). Use only java.time classes.
  • Convert/parse your mysterious input data as a LocalDate.
  • Compare by calling isBefore, isAfter, and isEqual methods.

Avoid legacy date-time classes

Firstly, do not mix legacy date-time classes with the modern java.time classes. Avoid ever using java.util.Date, Calendar, SimpleDateFormat, and any other date-time class not found in the java.time package. Those old classes are a wretched mess, entirely supplanted by the java.time framework. Good riddance.

ISO 8601

Your input format of YYYY-MM-DD is defined by the standard ISO 8601. The java.time classes use these standard formats by default when parsing/generating strings. So no need to specify a formatting pattern.

LocalDate.parse( "2018-01-23" )

When exchanging date-time values as text, always use ISO 8601 formats. Avoid other formats such as MM/dd/yyyy.

Current date

LocalDate.now()

When getting the current date, always pass the desired/expected time zone. Otherwise you are implicitly on the JVM’s current default password. That default can be changed at any moment by any code of any app within the JVM.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment, so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" ) ;  
LocalDate today = LocalDate.now( z ) ;

If you want to use the JVM’s current default time zone, ask for it and pass as an argument. If omitted, the JVM’s current default is applied implicitly. Better to be explicit, as the default may be changed at any moment during runtime by any code in any thread of any app within the JVM.

ZoneId z = ZoneId.systemDefault() ;  // Get JVM’s current default time zone.

Convert your input to java.time

Your Question cannot be answered fully until you explain exactly what is in your input, the pd object.

Is that pd a string? If so, what kind of text is in there? Search Stack Overflow for DateTimeFormatter class to find many examples and discussions of parsing text to date-time objects using java.time.

If your input is a string such as 01/23/2017, define a formatting pattern to match.

String input = "01/23/2017" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM/dd/uuuu" ) ;  // Define a custom format to match input.
LocalDate ld = LocalDate.parse( input , f ) ;

As mentioned above, always use standard ISO 8601 formats rather than custom formats such as this when exchanging date-time values as text.

Comparing

To compare LocalDate objects, call the methods isBefore, isAfter, and isEqual.

boolean isFuture = someLocalDate.isAfter( today ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

Convert pd to date (or better, its epoch as long (date is deprecated)) and do d.compare instead of >=

ShaharT
  • 442
  • 5
  • 13