0

How to add number of days into a given date in the format mm/dd/yyyy .

If my date is 9/12/2007, I want to add 30 days into the date and the result should be 10/12/2007.

I have many frequencies like Weekly, monthly, Every 2 weeks, Twice a month, Every 4 weeks, Once in 2 months, Every 3 months, Every 6 months, Every 3 months, Annually, etc.

If we select the different frequencies from the list, the result should vary based on the frequency. Can anyone help me on this ?

Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
user3518223
  • 155
  • 1
  • 5
  • 13

2 Answers2

3

Convert your date to a LocalDate, add the required values to it and then convert it back to the format you need it.

For example adding 30 days would look like this:

LocalDate d = LocalDate.of(2007,9,12).plus(30, ChronoUnit.DAYS)

And if you look at ChronoUnit you can see there are some units defined like weeks, days, months and so on...

GHajba
  • 3,665
  • 5
  • 25
  • 35
1
String dt = "9/12/2007";  // Start date
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, 1); 
sidgate
  • 14,650
  • 11
  • 68
  • 119