1

In my android application there is a user defined date. This date should be greater than or equal to the system date. If it is, then process will continue. If it is not,then it gives a toast Follow up date should be greater than or equal to system date.

I used this code segment.

if (sys_date.after(df.parse((date.getText().toString())))) {
    Toast.makeText(getActivity(), "Follow up date should be greater than or equal to system date", Toast.LENGTH_SHORT).show();
} else {
    //process will continue
}

But the problem is that : it gives the toast message even if the user defined date is equal to the system date.

Anyone could be so kind enough to explain what's going on here and how can I solve this matter.

Thanx in advance

SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
Rose18
  • 2,892
  • 8
  • 47
  • 98
  • Did you parse it correctly?? – SweetWisher ツ Nov 08 '13 at 04:39
  • FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes. Much of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Feb 23 '18 at 00:27

4 Answers4

7

I am using this

public static boolean isAfterToday(int year, int month, int day)
{
    Calendar today = Calendar.getInstance();
    Calendar myDate = Calendar.getInstance();

    myDate.set(year, month, day);

    if (myDate.before(today)) 
    {
        return false;
    }
    return true;
}
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
2

I have made this method validate all possible validations of dates. It can help you.

    public static final int DATE_EQUAL_TO_TODAY = 1;
    public static final int DATE_EQUAL_TO_GREATER_THAN_TODAY = 2;
    public static final int DATE_EQUAL_TO_LESS_THAN_TODAY = 3;
    public static final int END_DATE_GREATER_OR_EQUAL_TO_START_DATE=4;
    public static final int END_DATE_CAN_GREATER_THAN_TODAY=5;
    public static int DATE_SHOULD_BE_GREATER_THAN_TODAY=6;
    public static boolean dateVadidation(String date,int validationType)
    {
    String currentDate = showDateFormat(System.currentTimeMillis());
    SimpleDateFormat df = new SimpleDateFormat("MM-dd-yyyy");
    if (validationType == DATE_EQUAL_TO_TODAY) {

        try {

            Date dtCurrent = df.parse(currentDate);


            Date dtCompareWith = df.parse(date);

            return dtCurrent.before(dtCompareWith)  || dtCurrent.after(dtCompareWith);

        } catch (Exception e) {
            Log.e("DATE_EQUAL_TO_TODAY", "Date Parsing Exception:" + e.getMessage());
        }
    }
    if(validationType == DATE_EQUAL_TO_GREATER_THAN_TODAY)
    {
        try {

            Date dtCurrent = df.parse(currentDate);


            Date dtCompareWith = df.parse(date);

            return dtCurrent.after(dtCompareWith);


        } catch (Exception e) {
            Log.e("DATE_EQUAL_TO_GREATER_THAN_TODAY", "Date Parsing Exception:" + e.getMessage());
        }
    }
    if(validationType == DATE_EQUAL_TO_LESS_THAN_TODAY)
    {
        try {

            Date dtCurrent = df.parse(currentDate);


            Date dtCompareWith = df.parse(date);

            return dtCurrent.before(dtCompareWith);

        } catch (Exception e) {
            Log.e("DATE_EQUAL_TO_LESS_THAN_TODAY", "Date Parsing Exception:" + e.getMessage());

        }
    }

    if(validationType==END_DATE_CAN_GREATER_THAN_TODAY)
    {
    try {


        Date dtTo = df.parse(date);

        Date dtCurrent = df.parse(currentDate);

        return dtTo.before(dtCurrent) ;

    }

    catch (Exception e) {
        Log.e("END_DATE_CAN_GREATER_THAN_TODAY", "Date Parsing Exception:" + e.getMessage());
    }
    }

    if(validationType==DATE_SHOULD_BE_GREATER_THAN_TODAY)
    {
    try {


        Date dtTo = df.parse(date);

        Date dtCurrent = df.parse(currentDate);


        return (dtTo.after(dtCurrent) && (! dtTo.equals(dtCurrent))) ;

    }
    catch (Exception e) {
        Log.e("DATE_SHOULD_BE_GREATER_THAN_TODAY", "Date Parsing Exception:" + e.getMessage());
    }
    }
    return false;

}
Sandeep
  • 2,573
  • 3
  • 21
  • 28
  • validation for END_DATE_CAN_GREATER_THAN_TODAY seems to have a bug - shouldn't it be **dtTo.after(dtCurrent)**? – Prahalad Deshpande Nov 08 '13 at 05:16
  • Just posted my query related to the semantics of END_DATE_CAN_GREATER_THAN_TODAY – Prahalad Deshpande Nov 08 '13 at 05:19
  • yeah you can pass this constant to method as parameter. whats the issue? there are all possible validations type which we often use. – Sandeep Nov 08 '13 at 05:22
  • Let me be specific - return dtTo.before(dtCurrent) ; should actually be return dtTo.after(dtCurrent) assuming that dtTo represents the END DATE and it can be greater than current date. – Prahalad Deshpande Nov 08 '13 at 05:46
  • There may be some issue. so it can be corrected by using ! operator or direct change in method. but the idea will b same – Sandeep Nov 08 '13 at 06:21
0

If you want to compare dates after ignoring the time portion, then this SO question will provide you an insight. Also refer to the SO question link posted by Pankaj Kumar

Date objects implement the Comparable interface. Hence you can use the compareTo method for exact Date comparison (time components included). Of course, then there is Date.equals() which will return true if the Date.getTime() method returns the same long value for the two date instances being compared.

I would recommend using a mature library such as Joda. Beware of some memory issues and workarounds discussed here.

Community
  • 1
  • 1
Prahalad Deshpande
  • 4,709
  • 1
  • 20
  • 22
0
  1. Convert the date String to Calendar and get the time from that in MilliSeconds in long. You have to use DateFormatter to do that conversion.

  2. Take another Calendar object and get time in MilliSeconds in another long. It's the system time.

  3. Compare the above two long variables.

Hope it helps.

ayon
  • 2,180
  • 2
  • 17
  • 32