I have tried to find out the difference between two dates in day in my code :
//EditText with string of start date
dataInicio = (EditText)findViewById(R.id.ses_dpDataIni);
//EditText with string of end date
dataFim = (EditText)findViewById(R.id.ses_dpDataFim);
//Convert String to calendar to check the difference between two dates..
try{
dateInic = dataInicio.getText().toString();
dateFim = dataFim.getText().toString();
calInic=Calendar.getInstance();
calFim = Calendar.getInstance();
calInic.setTime(form.parse(dateInic));
calFim.setTime(form.parse(dateFim));
}
catch (ParseException e) {
e.printStackTrace();
}
Log.w(SessaoQuotaEdit.class.getName(),"DIFERENCA DE DIAS" +daysBetween(calInic,calFim));
tvDiasQuotas = (TextView)findViewById(R.id.ses_tvNumDiasQuota);
tvDiasQuotas.setText("NUMBER OF DAYS: " +daysBetween(calInic,calFim));
//CHECK IS END-DATE IS GREATER THAN START-DATE
.............
.............
Aryan Kumar
27-Jun-2023Sure, here is how to do date comparison validation in Android:
Code snippet
In this code, we first create two Date objects,
date1
anddate2
. Then, we call theisDate1AfterDate2()
method to compare the two dates. TheisDate1AfterDate2()
method returns a boolean value, which indicates whetherdate1
is afterdate2
.Finally, we print a message to the console to indicate the result of the comparison.
Here is an explanation of the code:
Calendar.getInstance()
method returns a Calendar object that represents the current date and time.setTime()
method sets the Calendar object to the specified date.after()
method returns a boolean value, which indicates whether the specified date is after the current date.Anonymous User
30-Dec-2015