Compare two dates, handling null values.
TODO: correct the comparison order to align with Date.compareTo

@param d1 Date instance
@param d2 Date instance
@return int comparison result
12650
public static int compare(Date d1, Date d2)
   {
      int result;
      if (d1 == null || d2 == null)
      {
         result = (d1 == d2 ? 0 : (d1 == null ? 1 : -1));
      }
      else
      {
         long diff = d1.getTime() - d2.getTime();
         result = ((diff == 0) ? 0 : ((diff > 0) ? 1 : -1));
      }
      return (result);
   }
880058
public static int getNumOfMonths(Date date1, Date date2) {
		Calendar firstDate = Calendar.getInstance();
		Date date = new Date(date1.getTime());
		firstDate.setTime(date);
		Calendar secondDate = Calendar.getInstance();
		Date date3 = new Date(date2.getTime());
		secondDate.setTime(date3);
		int months = firstDate.get(Calendar.MONTH) - secondDate.get(Calendar.MONTH);

		return months;
	}
467302
@Override
    public int compare(final Issue i1, final Issue i2) {

        if (null == i1 && null == i2) {
            return 0;
        } else if (null == i1) {
            return 1;
        } else if (null == i2) {
            return -1;
        } else {
            return i1.getCreatedOn().compareTo(i2.getCreatedOn());
        }
    }
776052
private boolean eq(final Object o1, final Object o2) {

		if (o1 == null && o2 == null) {
			return true;
		}

		if ((o1 == null && o2 != null) || (o1 != null && o2 == null)) {
			return false;
		}

		try {

			if (o1 instanceof Number && o2 instanceof Number) {

				return compareNumberNumber(o1, o2) == 0;

			} else if (o1 instanceof String && o2 instanceof String) {

				return compareStringString(o1, o2) == 0;

			} else if (o1 instanceof Date && o2 instanceof Date) {

				return compareDateDate(o1, o2) == 0;

			} else if (o1 instanceof Date && o2 instanceof String) {

				return compareDateString(o1, o2) == 0;

			} else if (o1 instanceof String && o2 instanceof Date) {

				return compareStringDate(o1, o2) == 0;

			} else if (o1 instanceof Boolean && o2 instanceof String) {

				return compareBooleanString((Boolean)o1, (String)o2) == 0;

			} else if (o1 instanceof String && o2 instanceof Boolean) {

				return compareStringBoolean((String)o1, (Boolean)o2) == 0;

			} else if (o1 instanceof Number && o2 instanceof String) {

				return compareNumberString((Number)o1, (String)o2) == 0;

			} else if (o1 instanceof String && o2 instanceof Number) {

				return compareStringNumber((String)o1, (Number)o2) == 0;

			} else {

				return compareStringString(o1.toString(), o2.toString()) == 0;

			}

		} catch (NumberFormatException nfe) {

			return false;

		}
	}
585969
public static boolean isSameDay(final Date date1, final Date date2) {
        final Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        final Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);

        return cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) && cal1.get(Calendar.DATE) == cal2.get(Calendar.DATE);
    }
1172164
public static CompareDates compareTwoDates(Date date1, Date date2) {
		Date d1 = new Date(date1.getTime());// to unify the format of the dates
											// before the compare
		Date d2 = new Date(date2.getTime());
		if (d1.compareTo(d2) < 0)
			return CompareDates.DATE1_LESS_THAN_DATE2;
		else if (d1.compareTo(d2) > 0)
			return CompareDates.DATE1_GREATER_THAN_DATE2;
		else
			return CompareDates.DATE1_EQUAL_DATE2;
	}
134812
private static boolean doEquals(DerValue d1, DerValue d2) {
        synchronized (d1.data) {
            synchronized (d2.data) {
                d1.data.reset();
                d2.data.reset();
                return d1.buffer.equals(d2.buffer);
            }
        }
    }
622880
private int compareDate(Date a, Date b) {
		final Date ta = new Date(a.getTime());
		final Date tb = new Date(b.getTime());
		final long d1 = setHourToZero(ta).getTime();
		final long d2 = setHourToZero(tb).getTime();
		return (int) Math.round((d2 - d1) / 1000.0 / 60.0 / 60.0 / 24.0);
	}
382927
private int compare(Object key1, Object key2) {
        if (comparator == null)
            return ((Comparable) key1).compareTo(key2);
        else
            return comparator.compare(key1, key2);
    }
1026015
function isSameDay(d1, d2) {
      return d1.getDate() == d2.getDate() && isSameMonthAndYear(d1, d2);
    }
33380
def diff_in_days(date_to_compare)
      # d1 will be the earlier date, d2 the later
      if date_to_compare > self
        d1, d2 = dup, date_to_compare.dup
      elsif self > date_to_compare
        d1, d2 = date_to_compare.dup, dup
      else
        return 0  # same date
      end

      total = 0
      while d1.year != d2.year
        total += d1.days_left_in_year + 1 # need one extra day to push us to jan 1
        d1 = ZDate.new(ZDate.format_date(d1.year + 1))
      end
      total += d2.day_of_year - d1.day_of_year
      total
    end
1157549
private static long dateTimeDifference(final Temporal d1, final Temporal d2) {
      return ChronoUnit.SECONDS.between(d1, d2);
  }
973168
@Override
    public int compare(Field o1, Field o2) {
        int i = order.indexOf(o1.getId());
        int j = order.indexOf(o2.getId());
        if(i==j) return 0;
        if(i==-1 && j >= 0) return 1;
        if(j==-1 && i >= 0) return -1;
        return i - j;
    }
803765
public static function getAgeDays(Date $d1, Date $d2): int
    {
        if ($d2->maximumJulianDay() >= $d1->minimumJulianDay() && $d2->minimumJulianDay() <= $d1->maximumJulianDay()) {
            // Overlapping dates
            $jd = $d1->minimumJulianDay();
        } else {
            // Non-overlapping dates
            $jd = $d2->minimumJulianDay();
        }

        // Days - integer only (for sorting, rather than for display)
        if ($jd && $d1->minimumJulianDay()) {
            return $jd - $d1->minimumJulianDay();
        }

        return -1;
    }
568706
@Override
    public final int compare(T o1, T o2){
        if(o1==o2)
            return 0;
        else if(o1==null)
            return -1;
        else if(o2==null)
            return 1;
        else
            return _compare(o1, o2);
    }
436738
function compare(a, b, n) {
	var n2 = n,
		then = Date.now()
	while (n2--) {
		a === b
	}
	return (Date.now() - then) * 1e6 / n
}
1133884
private function isSameDay(\DateTime $d1, \DateTime $d2)
    {
        return $d1->format('Y') == $d2->format('Y') && $d1->format('z') == $d2->format('z');
    }