Remove date from EditText and add five years

1

I would like to capture a date with a EditText , and from it create another date by adding another 5 years. How could I do that?

    
asked by anonymous 04.01.2018 / 18:01

1 answer

1

Using Date , GregorianCalendar and SimpleDateFormat :

EditText seuEdit = ...;
String texto = seuEdit.getText().toString();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date d = sdf.parse(texto);
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(d);
gc.add(Calendar.YEAR, 5);
String daquiHaCincoAnos = sdf.format(gc.getTime());

Using LocalDate and DateTimeFormatter :

EditText seuEdit = ...;
String texto = seuEdit.getText().toString();
DateTimeFormatter fmt = DateTimeFormatter
    .ofPattern("dd/MM/uuuu")
    .withResolverStyle(ResolverStyle.STRICT);
LocalDate ld1 = LocalDate.parse(texto, fmt);
LocalDate ld2 = ld1.plusYears(5);
String daquiHaCincoAnos = ld2.format(fmt);
    
04.01.2018 / 18:54