Try the following:
1) Assumption is that you can write the date in the input field and the calendar is only the icon. You can have a helper method something like this
public String threeDaysBefore(){
String threeDaysBefore = "";
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_YEAR, -3);
Date before = cal.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm");
threeDaysBefore = formatter.format(before);
return threeDaysBefore;
}
And later in the code
WebElement calendarManualInput = driver.findElement...// find the manual input field
calendarManualInput.sendKeys(threeDaysBefore());
2) If you can only click the calendar, It would be a little more tricky. You still need the String, but a little different:
public String threeDaysBefore(){
String threeDaysBefore = "";
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_YEAR, -3);
Date before = cal.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("dd");
threeDaysBefore = formatter.format(before);
return threeDaysBefore;
}
But the above has little catch. If the date is 1.4. then it will return you "29" which could be interpreted as 29.4. which you don't want to happen. So later in the code, you will probably have to do this
//this will click three days before
Date today = new Date();
Date minusThree = new Date();
Calendar now = Calendar.getInstance();
now.setTime(today);
Calendar before = Calendar.getInstance();
before.setTime(minusThree);
before.add(Calendar.DAY_OF_YEAR, -3);
int monthNow = now.get(Calendar.MONTH);
int monthBefore = before.get(Calendar.MONTH);
if (monthBefore < monthNow){
// click previous month in the calendar tooltip on page
}
WebElement dateToSelect = driver.findElement(By.xpath("//span[text()='"+threeDaysBefore()+"']"));
dateToSelect.click();