---
title: "Date comparison  Validation in android"  
description: "Date comparison  Validation in android"  
author: "Anonymous User"  
published: 2015-12-30  
updated: 2023-06-28  
canonical: https://www.mindstick.com/forum/33809/date-comparison-validation-in-android  
category: "android"  
tags: ["android", "android edittext"]  
reading_time: 3 minutes  

---

# Date comparison  Validation in android

I have edit texts for start date and End date. I want to implement date [validation](https://www.mindstick.com/articles/12234/validation-using-data-annotation-using-entity-framework) which will [check whether](https://www.mindstick.com/forum/157543/write-a-java-program-to-check-whether-a-string-is-a-palindrome) the end date is greater than start date or not. [Please suggest](https://answers.mindstick.com/qa/43506/please-suggest-the-bathing-date-at-kumbh) me the [right](https://www.mindstick.com/articles/12766/check-whether-you-are-doing-digital-transformation-right-or-not) way.\
I have tried to find out the [difference between two](https://www.mindstick.com/forum/160701/how-to-calculate-the-difference-between-two-dates-in-c-sharp) [dates](https://yourviews.mindstick.com/story/1513/7-zodiac-signs-astrology-dates-meanings-amp-compatibility) in day in my [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) :

```
             //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             .............             .............
```

## Replies

### Reply by Aryan Kumar

Sure, here is how to do date comparison validation in Android:

Code snippet

```plaintext
import java.util.Calendar;
import java.util.Date;

public class DateComparisonValidation {

    public static boolean isDate1AfterDate2(Date date1, Date date2) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date1);

        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date2);

        return calendar1.after(calendar2);
    }

    public static void main(String[] args) {
        Date date1 = new Date();
        Date date2 = new Date();

        if (isDate1AfterDate2(date1, date2)) {
            System.out.println("Date1 is after Date2");
        } else {
            System.out.println("Date1 is not after Date2");
        }
    }
}
```

In this code, we first create two Date objects, `date1` and `date2`. Then, we call the `isDate1AfterDate2()` method to compare the two dates. The `isDate1AfterDate2()` method returns a boolean value, which indicates whether `date1` is after `date2`.

Finally, we print a message to the console to indicate the result of the comparison.

Here is an explanation of the code:

- The `Calendar.getInstance()` method returns a Calendar object that represents the current date and time.
- The `setTime()` method sets the Calendar object to the specified date.
- The `after()` method returns a boolean value, which indicates whether the specified date is after the current date.

### Reply by Anonymous User

Hi Glen,\
Use this code:

```
SimpleDateFormat dfDate  = new SimpleDateFormat("yyyy-MM-dd");public static boolean CheckDates("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        }        else if(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;}
```


---

Original Source: https://www.mindstick.com/forum/33809/date-comparison-validation-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
