The easiest way to validate would be by using a SimpleDateFormat
and using the flag of lenient
class DateFormat
. The standard is true
, to capture formatting errors should use it as false
.
Exemplifying:
// Configure o SimpleDateFormat no onCreate ou onCreateView
String pattern = "dd/MM/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
sdf.setLenient(false);
// Durante a confirmacao de cadastro, faça a validacao
String data = NascimentoUsu.getText().toString();
try {
Date date = sdf.parse(data);
// Data formatada corretamente
} catch (ParseException e) {
// Erro de parsing!!
e.printStackTrace();
}
The flag lenient
being true
, causes the SimpleDateFormat
use a heuristic to correct "wrong data".
With lenient true
, the date 29/02/2014 would be converted to 01/03/2014. Using lenient
as false
, it generates a Parsing error.
You want to validate during typing or at the end?
– Wakim
I did not think that any of the solutions would be welcome. I would prefer in the end when I order to save.
– Guilherme