-1

I have two instances of Calender, expiryDate and nowDate. The date of expiryDate is set to 16 august 2020, nowDate gets the current date.

Calendar expiryDate = Calendar.getInstance();
Calendar nowDate = Calendar.getInstance();

expiryDate.set(Calendar.DAY_OF_MONTH, 16);
expiryDate.set(Calendar.MONTH, Calendar.AUGUST);
expiryDate.set(Calendar.YEAR, 2020);

What code can determine which instance has the latest date?

Common Sense
  • 149
  • 1
  • 8
  • 1
    see https://stackoverflow.com/a/12970352/6478047 – Manohar Sep 09 '19 at 12:52
  • One thing to keep in mind for your code is the same-day check - e.g. is "6PM vs midnight (start of day)" considered expired? –  Sep 09 '19 at 13:44

4 Answers4

1

First Convert your Calendar to date Object.

Date date1 = calendar.getTime();

then use date compareTo method.

Date class has its own methods for date comparison: compareTo

if (date1.compareTo(date2) > 0) {
    Log.i("app", "Date1 is after Date2");
    }
Md Abdul Gafur
  • 6,213
  • 2
  • 27
  • 37
  • 1
    this would be an unnecessary conversion... also if you do convert Calendar to Date, just use the before and after methods of the Date class thats easier – Kushan Sep 09 '19 at 12:47
  • @Kushan if want to write code one then it is ok , i think no problem – Md Abdul Gafur Sep 09 '19 at 12:52
0

Simplest way is use milliseconds Kotlin:

  if (expiryDate.timeInMillis< nowDate.timeInMillis) {
         // Expired
   } else {
         // Not expired
   }

Java

  if (expiryDate.getTimeInMillis() < nowDate.getTimeInMillis()) {
         // Expired
   } else {
         // Not expired
   }
Antonis Radz
  • 3,036
  • 1
  • 16
  • 34
0

Best and easiest, get epoch millis from both and compare the long millis

Calendar expiryDate = Calendar.getInstance();
Calendar nowDate = Calendar.getInstance();

expiryDate.set(Calendar.DAY_OF_MONTH, 16);
expiryDate.set(Calendar.MONTH, Calendar.AUGUST);
expiryDate.set(Calendar.YEAR, 2020);

long nowMillis = nowDate.getTimeInMillis();

long expiryDateMillis = expiryDate.getTimeInMillis();

if(nowMillis>expiryDateMillis){
  //now date is after expiry
}else{
  //now date is before expiry
}
Kushan
  • 5,855
  • 3
  • 31
  • 45
0

The implementation of the Calendar.compareTo() function is equivalent to the getTimeInMillis solutions - so why not just use the Calendar's code:

So OP would be:

if (nowDate.compareTo(expiryDate) > 0) {
    // expired
} else {
    // not expired
}

Note - this does not solve the "same-day" case.

For reference here's the Calendar's implementation of the compareTo:

public int compareTo(Calendar anotherCalendar) {
    if (anotherCalendar == null) {
        throw new NullPointerException();
    }
    long timeInMillis = getTimeInMillis();
    long anotherTimeInMillis = anotherCalendar.getTimeInMillis();
    if (timeInMillis > anotherTimeInMillis) {
        return 1;
    }
    if (timeInMillis == anotherTimeInMillis) {
        return 0;
    }
    return -1;
}
  • Just noticed this is the same as @Manohar Reddy 's duplicate reference in comments so post most likely will be tagged as such. –  Sep 09 '19 at 13:39