End-date Greater Than Start-date Validation Android
I have two EditText's. One with start date and other with end date. I need to make a validation and check if end date is greater than start-date. I don't know how i can do this. In
Solution 1:
SimpleDateFormatdfDate=newSimpleDateFormat("yyyy-MM-dd");
publicstaticbooleancheckDates("2012-07-12", "2012-06-12)" {
boolean b = false;
try {
if(dfDate.parse(d1).before(dfDate.parse(d2)))
{
b = true;//If start date is before end date
}
elseif(dfDate.parse(d1).equals(dfDate.parse(d2)))
{
b = true;//If two dates are equal
}
else
{
b = false; //If start date is after the end date
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
Solution 2:
Try this function.
publicstaticbooleanisDateAfter(String startDate,String endDate)
{
try
{
String myFormatString = "yyyy-M-dd"; // for exampleSimpleDateFormat df = newSimpleDateFormat(myFormatString);
Date date1 = df.parse(endDate));
Date startingDate = df.parse(startDate);
if (date1.after(startingDate))
returntrue;
elsereturnfalse;
}
catch (Exception e)
{
returnfalse;
}
}
It return true if enddate is after start date.
Solution 3:
publicstaticboolean CheckDates(String d1, String d2){
SimpleDateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd");
boolean b = false;
try {
if(dfDate.parse(d1).before(dfDate.parse(d2)))
{
b = true;//If start date is before end date
}
elseif(dfDate.parse(d1).equals(dfDate.parse(d2)))
{
b = true;//If two dates are equal
}
else
{
b = false; //If start date is after the end date
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
Solution 4:
Solution 5:
Thanks to everyone ....
Here is my solution
if(calInic.after(calFim)==true)
Log.w(SessaoQuotaEdit.class.getName(),"ERROR: DATA FINAL É ANTES DA DATA INICIAL");
elseif(calInic.before(calFim)==true)
Log.w(SessaoQuotaEdit.class.getName(),"CORRECTO: DATAS CORRECTAS");
elseif(calInic.equals(calFim)==true)
Log.w(SessaoQuotaEdit.class.getName(),"CORRECTO: DATAS IGUAIS");
Post a Comment for "End-date Greater Than Start-date Validation Android"