Your subject is your month comparator, the road that claims
month1.compareTo(desiredMonth) < 0)
desiredMonth is a string, not a quantity. You want to use Java’s date compareTo class, not its string compareTo.
In case your date picker solely must go backwards in time, it’s adequate to get your code to run to simply change the compareTo assertion to month1.equals(desiredMonth). This retains it from going into the second else situation when just one merchandise matches.
In case you want the date picker to have the ability to intelligently discover a desired date in both route, you will want to ensure it might inform what month names are earlier than which different month names, ideally utilizing a datetime utility. You are able to do this comparability manually, with out datetime, as nicely, like so:
- add an array of months close to your desiredMonth/desiredYear variables
String[] monthsOrder = {“January”, “February”, “March”, “April”,
“Could”, “June”, “July”, “August”, “September”, “October”, “November”,
“December”};
-
write a way that returns the index of a month’s string from that date record
public static int findMonthIndex(String[] months, String targetMonth) { for (int i = 0; i < months.size; i++) { if (months[i].equals(targetMonth)) { return i; } } //If month not in record simply crash throw new IllegalArgumentException("Unhealthy month"); }
-
as an alternative of checking if the distinction between two month strings is zero, evaluate the outcomes of two calls to your month conversion operate:
findMonthIndex(monthsOrder, month1) < findMonthIndex(monthsOrder, desiredMonth))
- Your yr comparability can have the identical downside. You want to evaluate the years as numbers relatively than as strings, and have instances dealing with every mixture of yr and month being larger or lower than the specified month.
There’s nonetheless a variety of room for simplification and exception catching however here’s what a completed and dealing code appears like:
String[] monthsOrder = {"January", "February", "March", "April", "Could", "June", "July", "August", "September", "October", "November", "December"};
whereas (true) {
String year1 = driver.findElement(By.className("ui-datepicker-year")).getText();
String month1 = driver.findElement(By.className("ui-datepicker-month")).getText();
if (year1.equals(desiredYear) && month1.equalsIgnoreCase(desiredMonth)) {
System.out.println("Desired date reached: the yr is " + year1 + " and the month is " + month1);
break;
} else if (Integer.parseInt(year1) > Integer.parseInt(desiredYear)){
driver.findElement(By.className("ui-icon-circle-triangle-w")).click on();
} else if (Integer.parseInt(year1) == Integer.parseInt(desiredYear) && findMonthIndex(monthsOrder, month1) < findMonthIndex(monthsOrder, desiredMonth)) {
driver.findElement(By.className("ui-icon-circle-triangle-e")).click on();
} else if (Integer.parseInt(year1) == Integer.parseInt(desiredYear) && findMonthIndex(monthsOrder, month1) > findMonthIndex(monthsOrder, desiredMonth)) {
driver.findElement(By.className("ui-icon-circle-triangle-w")).click on();
} else {
driver.findElement(By.className("ui-icon-circle-triangle-e")).click on();
}
}
driver.stop();
}
public static int findMonthIndex(String[] months, String targetMonth) {
for (int i = 0; i < months.size; i++) {
if (months[i].equals(targetMonth)) {
return i;
}
}
//If month not in record simply crash
throw new IllegalArgumentException("Unhealthy month");
}