by checking whether the date of an Edittext is greater than the date of another Edittext

Asked

Viewed 265 times

0

I have two Edittext in my application and select the date using a Datepicker need to make the user not enter a date less than the one given in the first Edittext.

  • You’re setting the date that comes from DatePicker in the EditText ? How is the date format in the first EditText ?

  • Yes the date comes from Datepicker to Edittext the format is dd-MM-yyyy for both Edittext

1 answer

1


Assuming the dates of the two EditText are in format dd-MM-yyyy can do the following:

EditText editData1 = (EditText)findViewById(R.id.meu_edit1); //id para o seu 1º EditText
EditText editData2 = (EditText)findViewById(R.id.meu_edit2); //id para o seu 2º EditText

//criar formato para interpretação da data
SimpleDateFormat formato = new SimpleDateFormat("dd-MM-yyyy");

try {
    Date data1 = formato.parse(editData1.getText().toString()); //interpretar data1
    Date data2 = formato.parse(editData2.getText().toString()); //interpretar data2

    if (data2.getTime() < data1.getTime()){
        //código para quando segunda data é menor
    }
}
catch (ParseException ex){
    Log.d("Debug", "Um dos campos não tem uma data válida");
}

Using the following Imports:

import android.util.Log;
import android.widget.EditText;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

Browser other questions tagged

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