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?
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?
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);