Add days to a date in EditText

1

I have a date in the format dd-MM-yyyy in an EditText with InputType="date" called "start".

I want to get this date, put its contents in the variable Date called "inifer" and on this date add an integer variable called "diafer" and subtract 1 and play in the variable "terfer".

My commands are:

    String datastr = inicio.getText().toString();
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    Date inifer = new Date();
    try
    {
        inifer = dateFormat.parse(datastr);
    }
    catch (ParseException e)
    {
        e.printStackTrace();
    }

    Log.i("Data de início", dateFormat.format(inifer));
    int diafer = Integer.parseInt(diasfer.getText().toString());
    Log.i("dias de férias", String.valueOf(diafer));

    Calendar c = Calendar.getInstance();
    Date terfer = inifer;
    c.setTime(terfer);
    c.add(c.DATE, diafer - 1);
    terfer.setTime(c.DATE);
    int mester = c.MONTH;
    int diater = c.DAY_OF_MONTH;
    Log.i("mês de término férias", String.valueOf(mester));
    Log.i("dia de término férias", String.valueOf(diater));

The Logs show the correct content except the last two.

With the date of 10/05/2016 and 30 days of vacation, they show month 2 and day 5, when it should be month 6 and day 8, as 29 days after 10/05/2016 is day 08 / 06/2016.

Where did I go wrong?

    
asked by anonymous 10.05.2016 / 15:53

1 answer

1

Use the get method of Calendar to return the information of the desired fields.

String datastr = inicio.getText().toString();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date inifer = new Date();
try
{
    inifer = dateFormat.parse(datastr);
}
catch (ParseException e)
{
    e.printStackTrace();
}
Log.i("Data de início", dateFormat.format(inifer));
int diafer = Integer.parseInt(diasfer.getText().toString());
Log.i("dias de férias", String.valueOf(diafer));

Calendar c = Calendar.getInstance();
Date terfer = inifer;
c.setTime(terfer);
c.add(c.DATE, diafer - 1);
// Não necessário terfer é atualizado por referência
//terfer.setTime(c.DATE);
int mester = c.get(Calendar.MONTH);
int diater = c.get(Calendar.DAY_OF_MONTH);
Log.i("mês de término férias", String.valueOf(mester));
Log.i("dia de término férias", String.valueOf(diater));
    
10.05.2016 / 19:33