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.esofthead.mycollab.core.utils.DateTimeUtils.java

public static int compareByDate(Date date1, Date date2) {
    Date newDate1 = trimHMSOfDate(date1);
    Date newDate2 = trimHMSOfDate(date2);
    return newDate1.compareTo(newDate2);
}

From source file:Main.java

public static boolean expireDate(String date) {
    Date todayDate = null;
    Date inputDate = null;/* w  w  w  .  j  a  v a2 s  .  c o m*/
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String today = sdf.format(new Date());
    try {
        todayDate = sdf.parse(today);
        inputDate = sdf.parse(date);
        if (todayDate.compareTo(inputDate) <= 0) {
            return false;
        } else {
            return true;
        }
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
    }

}

From source file:com.sanyanyu.syybi.utils.DateUtils.java

/**
 * @Title:getDaysListBetweenDates/*from w w w  .j a v  a  2s.c  om*/
 * @Description: .
 * @param begin
 *             .
 * @param end
 *            ? .
 * @return
 * @return List<String>
 */
public static List<String> getDaysListBetweenDates(String begin, String end) {
    List<String> dateList = new ArrayList<String>();
    Date d1;
    Date d2;
    d1 = DateUtils.parseDate(begin);
    d2 = DateUtils.parseDate(end);
    if (d1.compareTo(d2) > 0) {
        return dateList;
    }
    do {
        dateList.add(DateFormatUtils.format(d1, "yyyy-MM-dd"));
        d1 = DateUtils.addDays(d1, 1);

    } while (d1.compareTo(d2) <= 0);
    return dateList;
}

From source file:de.micromata.genome.gwiki.plugin.rogmp3_1_0.Track.java

public static Date getLastUsage(List<Track> tracks) {
    if (tracks.isEmpty() == true) {
        return new Date(0);
    }/*from  w ww.  j a v  a 2s.co  m*/
    Date d = new Date(0);
    for (Track track : tracks) {
        Date td = track.getUsage(UserUtils.getCurrentUserName()).getDate();
        if (td.compareTo(d) > 0) {
            d = td;
        }
    }
    return d;
}

From source file:com.sanyanyu.syybi.utils.DateUtils.java

/**
 * ?/*from ww  w  .  j  a v  a2 s  . c om*/
 * @param begin
 * @param end
 * @return
 */
public static List<String> getMonthListBetweenDates(String begin, String end) {

    //?
    if (StringUtils.isNotBlank(begin) && begin.indexOf("-") > -1 && begin.length() == 7
            && StringUtils.isNotBlank(end) && end.indexOf("-") > -1 && end.length() == 7) {

        List<String> monthList = new ArrayList<String>();

        Date d1 = DateUtils.parseDate(begin + "-01");
        Date d2 = DateUtils.parseDate(end + "-01");

        if (d1.compareTo(d2) > 0) {
            return null;
        }

        do {

            monthList.add(DateFormatUtils.format(d1, "yyyy-MM"));

            d1 = DateUtils.addMonths(d1, 1);

        } while (d1.compareTo(d2) <= 0);

        return monthList;

    }

    return null;

}

From source file:de.micromata.genome.gwiki.plugin.rogmp3_1_0.Track.java

public static void sortByDate(List<? extends EntityWithTracks> list, final boolean desc) {
    Collections.sort(list, new Comparator<EntityWithTracks>() {

        @Override/*w w w .  j  a  v  a  2s.c o  m*/
        public int compare(EntityWithTracks o1, EntityWithTracks o2) {
            Date ua1 = o1.getLastUsage();
            Date ua2 = o2.getLastUsage();
            return ua1.compareTo(ua2) * (desc ? -1 : 1);
        }
    });
}

From source file:org.shareok.data.datahandlers.DataHandlersUtil.java

/**
 * //from w  w  w  .  j a  v a 2s  . c  om
 * @param dateStr1 : yyyy-MM-dd
 * @param dateStr2 : yyyy-MM-dd
 * @return : is dateStr1 before dateStr2
 */
public static Integer datesCompare(String dateStr1, String dateStr2) {
    Integer isBefore = null;
    try {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse(dateStr1);
        Date date2 = sdf.parse(dateStr2);
        isBefore = date1.compareTo(date2);
    } catch (ParseException ex) {
        logger.error(ex);
    }
    return isBefore;
}

From source file:org.patientview.radar.util.RadarUtility.java

/**
 * @param event1Start cannot be null/*from   w ww  .  j av a 2  s. c  om*/
 * @param event2Start cannot be null
 * @return true if there is an overlap
 */
public static boolean isEventsOverlapping(Date event1Start, Date event1End, Date event2Start, Date event2End) {
    if (event1End == null) {
        if (event2End == null) {
            if (event1Start.compareTo(event2Start) == 0) {
                return true;
            }
        } else {
            if (event1Start.compareTo(event2Start) >= 0 && event1Start.compareTo(event2End) < 1) {
                return true;
            }
        }
    } else {
        if (event2Start.compareTo(event1Start) >= 0 && event2Start.compareTo(event1End) < 1) {
            return true;
        }
        if (event2End != null) {
            if (event1Start.compareTo(event2Start) >= 0 && event1Start.compareTo(event2End) < 1
                    || event1End.compareTo(event2Start) >= 0 && event1End.compareTo(event2End) < 1
                    || event2Start.compareTo(event1Start) >= 0 && event2Start.compareTo(event1End) < 1
                    || event2End.compareTo(event1Start) >= 0 && event2End.compareTo(event1End) < 1) {
                return true;
            }
        }
    }
    return false;
}

From source file:mitm.common.util.DateTimeUtils.java

/**
 * Compares the two dates. Returns a negative integer, zero, or a positive integer as the first date is less 
 * than, equal to, or greater than the second. 
 *//*w  w w .j a  va 2  s. c  om*/
public static int compare(Date date, Date otherDate) {
    if (date == null && otherDate == null) {
        return 0;
    }

    if (date == null) {
        return -1;
    }

    if (otherDate == null) {
        return 1;
    }

    return date.compareTo(otherDate);
}

From source file:org.dawnsci.fileviewer.Utils.java

public static int compareFiles(File a, File b, SortType sortType, int direction) {
    // boolean aIsDir = a.isDirectory();
    // boolean bIsDir = b.isDirectory();
    // if (aIsDir && ! bIsDir) return -1;
    // if (bIsDir && ! aIsDir) return 1;

    // sort case-sensitive files in a case-insensitive manner
    int compare = 0;
    switch (sortType) {
    case NAME:/*from   w w w.j  a  v  a  2  s  .c  o m*/
        compare = a.getName().compareToIgnoreCase(b.getName());
        if (compare == 0)
            compare = a.getName().compareTo(b.getName());
        break;
    case SIZE:
        long sizea = a.length(), sizeb = b.length();
        compare = sizea < sizeb ? -1 : 1;
        break;
    case TYPE:
        String typea = getFileTypeString(a), typeb = getFileTypeString(b);
        compare = typea.compareToIgnoreCase(typeb);
        if (compare == 0)
            compare = typea.compareTo(typeb);
        break;
    case DATE:
        Date date1 = new Date(a.lastModified());
        Date date2 = new Date(b.lastModified());
        compare = date1.compareTo(date2);
        break;
    default:
        return 0;
    }
    if (FileTableViewerComparator.DESC == direction)
        return (-1 * compare);
    return compare;
}