Example usage for java.util Date compareTo

List of usage examples for java.util Date compareTo

Introduction

In this page you can find the example usage for java.util Date compareTo.

Prototype

public int compareTo(Date anotherDate) 

Source Link

Document

Compares two Dates for ordering.

Usage

From source file:com.reizes.shiva.utils.DateUtil.java

public static Date min(Date d1, Date d2) {
    if (d1 == null) {
        return d2;
    } else if (d2 == null) {
        return d1;
    }//  w  w w.j av  a2  s.c  o m

    return d1.compareTo(d2) < 0 ? d1 : d2;
}

From source file:com.reizes.shiva.utils.DateUtil.java

public static Date max(Date d1, Date d2) {
    if (d1 == null) {
        return d2;
    } else if (d2 == null) {
        return d1;
    }//from w w  w. j  a  v  a  2 s.  c  om

    return d1.compareTo(d2) < 0 ? d2 : d1;
}

From source file:org.kuali.student.enrollment.class2.acal.util.AcalCommonUtils.java

/**
 * Allows you to see if a date range overlaps another date range.
 *
 * This can be used to determine if a Holiday Calendar overlaps an Academic Calendar.
 *
 * @param periodStartDate/*from  ww w .j av a 2s.co  m*/
 * @param periodEndDate
 * @param subStart
 * @param subEnd
 * @return
 */
public static boolean doDatesOverlap(Date periodStartDate, Date periodEndDate, Date subStart, Date subEnd) {
    boolean bRet = false;

    int compStart = subStart.compareTo(periodEndDate);
    int compEnd = subEnd.compareTo(periodStartDate);
    if (compStart <= 0 && compEnd >= 0) {
        bRet = true;
    }

    return bRet;
}

From source file:org.logger.event.web.utils.ServerValidationUtils.java

public static void rejectIfInvalidDate(Errors errors, Date data, String field, String errorCode,
        String errorMsg) {// ww w.ja v  a 2  s.com
    Date date = new Date();
    if (data.compareTo(date) <= 0) {
        errors.rejectValue(field, errorCode, errorMsg);
    }
}

From source file:de.codesourcery.planning.impl.AbstractJob.java

protected static boolean isBeforeOrOn(Date toCheck, Date other) {
    return toCheck.compareTo(other) <= 0;
}

From source file:de.codesourcery.planning.impl.AbstractJob.java

protected static boolean isAfterOrOn(Date toCheck, Date other) {
    return toCheck.compareTo(other) >= 0;
}

From source file:de.codesourcery.planning.impl.AbstractJob.java

protected static Date latest(Date d1, Date d2) {
    final int comp = d1.compareTo(d2);
    if (comp == -1) {
        return d2;
    } else if (comp == 1) {
        return d1;
    }/*from  w  w w  . j a v  a 2  s. c o  m*/
    return d1;
}

From source file:de.codesourcery.planning.impl.AbstractJob.java

protected static Date earliest(Date d1, Date d2) {
    final int comp = d1.compareTo(d2);
    if (comp == -1) {
        return d1;
    } else if (comp == 1) {
        return d2;
    }//  ww w.  java2s. c  o m
    return d1;
}

From source file:com.ocpsoft.socialpm.util.Dates.java

/**
 * Perform an inclusive date range comparison to a specific field precision
 * /*from   w  w w .j ava2 s  . co m*/
 * @param field
 *            see <i>java.util.Calendar</i> Millisecond, Second, Minute,
 *            Hour, Day, Week, etc...
 */
public static boolean isInPrecisionRange(Date start, Date end, Date date, final int field) {
    start = DateUtils.truncate(start, field);
    end = DateUtils.truncate(end, field);
    date = DateUtils.truncate(date, field);
    if ((date.compareTo(start) >= 0) && (date.compareTo(end) <= 0)) {
        return true;
    }
    return false;
}

From source file:Main.java

/**
 * <p>//from   ww w  . j  a v  a 2 s  .c o m
 * <b> public int compare(mDate, dateToCOmpareWith, format) </b>
 * </p>
 *
 * Compares two dates with each other using the rules of the given date
 * format.
 *
 * @param mDate
 *            The date you want to compare
 * @param dateToCompareWith
 *            The date comparing with
 * @param format
 *            An accepted format which must be valid for a
 *            <code>SimpleDateFormat</code>
 * @return <p>
 *         returns 0 if the dates are equal
 *         </p>
 *         <p>
 *         returns > 0 if mDate is larger then the date to compare with
 *         </p>
 *         <p>
 *         returns < 0 if mDate is smaller then the date to compare with
 *         </p>
 */
public static int compareDates(String mDate, String dateToCompareWith, String format) {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    Date date1 = null;
    Date date2 = null;
    try {
        date1 = sdf.parse(mDate);
        date2 = sdf.parse(dateToCompareWith);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date1.compareTo(date2);
}