Check if field contains a date

0

I have a list of EditText's that are filled with data returned from the database.

Some have a common text, for example

<EditText
      android:id="@+id/edittext"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Jon Snow" />

And in other cases it could be a date:

<EditText
      android:id="@+id/edittext"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="2017-06-20" />

How could I check if the text being displayed in EditText is a date or other common text? Would a regex solve this?

    
asked by anonymous 20.06.2017 / 15:10

1 answer

1
public static boolean isValidaData(String dataStr){
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); //Formate a data do jeito que for necessário, outro exemplo: ("dd/MM/yyyy")
    try {
        java.sql.Date data = new java.sql.Date(format.parse(dataStr).getTime());
        return true;
    } catch (ParseException ex) {
        return false;
    }
}

Then just call the method:

boolean dataValida = isValidaData(meuEditText.getText().toString());

Hugs!

    
20.06.2017 / 16:57