1
\$\begingroup\$

I am automating a scenario of flight booking on Cleartrip.com(https://www.cleartrip.com/flights) in which I have to enter depart date, 5 days from today and return date, 6 days from today. I have written below code.

Date dt = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(dt);
calendar.add(Calendar.DATE, 5);
dt = calendar.getTime();
String departdate = new SimpleDateFormat("dd/mm/yyyy").format(dt);

//enter Journy date in the field
WebElement depart = driver.findElement(By.id("DepartDate"));
depart.sendKeys(departdate);

Date dt1 = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(dt1);
calendar.add(Calendar.DATE, 6);
dt1 = calendar.getTime();
String returndate = new SimpleDateFormat("dd/mm/yyyy").format(dt1);

//enter returndate in the field
WebElement return= driver.findElement(By.id("ReturnDate"));
return.sendKeys(returndate);
\$\endgroup\$
0
\$\begingroup\$

You don't need the Date type. You can do it all with a Calendar instance and a DateFormat instance:

    DateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy");
    Calendar now = Calendar.getInstance(); // gets current date
    now.add(Calendar.DATE, 5);             // add five days
    String firstDate = dateFormat.format(now.getTime());
    ...
    now.add(Calendar.DATE, 1);             // add one more day
    String secondDate = dateFormat.format(now.getTime());
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.