-4

User will input a date and he/she will input days to add to it.

Like this: Date: 1/1/2015 Days to add: 20

The output should be 1/21/2015

I am wondering how to do that. I am beginner in android. T__T cries I tried other sources but i don't understand them at all. THANKS

THE USER WILL SUPPLY THE DATE TO ADD AND THE NUMBER OF DAYS TO ADD. All other sources only explain adding the current date with the number of days.

Nerdy Cat
  • 3
  • 1
  • 5
  • 1
    possible duplicate of [How to add days to a date in Java](http://stackoverflow.com/questions/2507377/how-to-add-days-to-a-date-in-java) – Viktor Yakunin Sep 25 '15 at 15:24

1 Answers1

2

You need to parse the string to a date first.

     SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yyyy");
      try {
        Date myDate = dateParser.parse("01/01/2015");
        Calendar c = Calendar.getInstance();
        c.setTime(myDate);
        c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) + 20);
        Date newDate = c.getTime();
        String newFormattedDate = dateParser.format(newDate);//01/21/2015
    } catch (ParseException e) {
        e.printStackTrace();
        //handle exception

    }