-1

I want to enter two dates into two fields where first date is current date and other has firstdate+3months. I wish to get this in a variable first and directly input it to those fields using sendkeys. How can this be achieved. I am using Java for Selenium.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateTimeDisplay {

 public static void main(String[] args) {

 DateFormat dateFormat1 = new SimpleDateFormat("MM/dd/yyyy");
 Date date = new Date();
 String date1= dateFormat1.format(date);
 System.out.println(date1);
 }

}

Here say I want another string date2 which stores date1+3months. How do I achieve it.?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    Possible duplicate of [Adding n hours to a date in Java?](http://stackoverflow.com/questions/3581258/adding-n-hours-to-a-date-in-java) – JeffC Mar 31 '17 at 13:30

1 Answers1

1

Try below code:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang3.time.DateUtils;

public class datetest {

public static void main(String[] args){

    DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    Date date = new Date();
    String date1 = dateFormat.format(date);
    Date newDate = DateUtils.addMonths(date, 3);
    String date2 = dateFormat.format(newDate);
    System.out.println("Current date: "+date1);
    System.out.println("Date after 3 months: "+date2);
}

}

Let me know if you have any queries.

Akarsh
  • 967
  • 5
  • 9