How to do date validation?

Asked

Viewed 363 times

2

How to validate the date as the date of birth? Can not put the date in the future.

        data.addTextChangedListener(DateUtils.insert("##/##/####", data));


                 mData = data.getText().toString();
  • Have you ever tried anything?

  • I don’t understand. You can explain it better?

  • In my registration screen the user will put the date of birth,I will pass the text written to string,the date that the user put can not be for example the year greater than 2017.. month greater than 12

  • Thank you for your attention..

1 answer

0

You can try something like this:

String dataDigitadaStr = editText.getText().toString(); // Ex: "04/05/2010"

SimpleDateFormat formater = new SimpleDateFormat("dd/MM/yyyy"); 
Date dataDigitada= formater.parse(dataDigitadaStr); 

if(dataDigitada.getTime() >= Date.now()){
   Toast.makeText(this, "Data digitada maior que a data atual", Toast.LENGTH_SHORT).show();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.