Question: I am using the datepicker dialog and am trying to compare the current date with the date specified in the dialog.
if the date is before the current date I wish to display invalid date.

Both current date and date entered are in same format. but I am getting error. when printing in console both current date and entered date are coming the the same format

```
if(date1.before(currDate))
{
     Toast.makeText(getApplicationContext(), "Enter the valid Date", Toast.LENGTH_LONG).show();
}

```

result:

> 
> current date 5/7/2017
> 
> 
> picked date 5-7-2017
> 
> 
>

Answer: 
Try this

```
        String strCurrDate = cal.get(Calendar.DATE) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.YEAR);
        String strAnotherDate = "05/03/2017";
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

        try {
            Date currDate = formatter.parse(strCurrDate);
            Date anotherDate = formatter.parse(strAnotherDate);
            if (currDate.before(anotherDate)) {
                Toast.makeText(getApplicationContext(), "Enter the valid Date", Toast.LENGTH_LONG).show();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

```
You can try this code for date comparison

```
@SuppressLint("SimpleDateFormat")
public static boolean compareSameDate(String firstDate, String secondDate) {
    boolean flag = false;
    try {
        SimpleDateFormat formatter = new SimpleDateFormat(
            "dd/MM/yyyy",
            Locale.ENGLISH);

        Date date1 = formatter.parse(firstDate);
        Date date2 = formatter.parse(secondDate);

        if (date2.compareTo(date1) == 0) {
            flag = true;
        } else if (date2.compareTo(date1) < 0) {
            flag = true;
        } else {
            flag = false;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return flag;
}

```