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:org.projectforge.business.teamcal.event.TeamEventUtils.java

public static List<TeamEventDO> convert(final List<VEvent> list) {
    final List<TeamEventDO> events = new ArrayList<TeamEventDO>();
    if (list == null || list.size() == 0) {
        return events;
    }/*from  www . j a  va  2s. c  o  m*/
    for (final VEvent vEvent : list) {
        events.add(createTeamEventDO(vEvent));
    }
    Collections.sort(events, new Comparator<TeamEventDO>() {
        public int compare(final TeamEventDO o1, final TeamEventDO o2) {
            final Date startDate1 = o1.getStartDate();
            final Date startDate2 = o2.getStartDate();
            if (startDate1 == null) {
                if (startDate2 == null) {
                    return 0;
                }
                return -1;
            }
            return startDate1.compareTo(startDate2);
        };
    });
    return events;
}

From source file:com.itd.dbmrgdao.TestTime3_backup.java

protected static String CompareTime(String a, String b, String operator) throws ParseException {

    if (a == null) {
        return b;
    }//from   ww  w. j a va  2s  .  c  o m

    SimpleDateFormat parser1 = new SimpleDateFormat("HHmm");
    Date x = parser1.parse(a);
    Date y = parser1.parse(b);

    try {
        // The Magic happens here i only get the Time out of the Date Object
        SimpleDateFormat parser2 = new SimpleDateFormat("HH:mm");
        x = parser2.parse(parser2.format(x));
        y = parser2.parse(parser2.format(y));

    } catch (ParseException ex) {
        System.err.println(ex);

    }

    switch (operator) {
    case "01":
        return ((x.compareTo(y) <= 0) ? a : b);
    case "04":
        return ((x.compareTo(y) >= 0) ? a : b);
    default:
        throw new IllegalArgumentException("Operator " + operator + " not fould in control!");
    }
}

From source file:com.itd.dbmrgdao.TestTime3.java

protected static String CompareTime(String a, String b, String operator) throws ParseException {

    if (a == null) {
        return b;
    }/*  w  w  w.j a v  a2  s. com*/

    SimpleDateFormat parser1 = new SimpleDateFormat("HH:mm");
    Date x = parser1.parse(a);
    Date y = parser1.parse(b);

    try {
        // The Magic happens here i only get the Time out of the Date Object
        SimpleDateFormat parser2 = new SimpleDateFormat("HH:mm");
        x = parser2.parse(parser2.format(x));
        y = parser2.parse(parser2.format(y));

    } catch (ParseException ex) {
        System.err.println(ex);

    }

    switch (operator) {
    case "01":
        return ((x.compareTo(y) <= 0) ? a : b);
    case "04":
        return ((x.compareTo(y) >= 0) ? a : b);
    default:
        throw new IllegalArgumentException("Operator " + operator + " not fould in control!");
    }
}

From source file:org.nuclos.common2.DateUtils.java

/**
 * compares two "from" dates where <code>null</code> means -infinity.
 * @param dateFrom1//from   w  w w  .j  av  a 2s . co m
 * @param dateFrom2
 * @return -1/0/+1 (as usual)
 */
public static int compareDateFrom(Date dateFrom1, Date dateFrom2) {
    final int result;

    if (dateFrom1 == null) {
        result = (dateFrom2 == null ? 0 : -1);
    } else if (dateFrom2 == null) {
        result = 1;
    } else {
        result = dateFrom1.compareTo(dateFrom2);
    }
    return result;
}

From source file:org.nuclos.common2.DateUtils.java

/**
 * compares two "until" dates where <code>null</code> means +infinity.
 * @param dateUntil1//from w  w  w .  j a v a2 s . co m
 * @param dateUntil2
 * @return -1/0/+1 (as usual)
 */
public static int compareDateUntil(Date dateUntil1, Date dateUntil2) {
    final int result;

    if (dateUntil1 == null) {
        result = (dateUntil2 == null ? 0 : 1);
    } else if (dateUntil2 == null) {
        result = -1;
    } else {
        result = dateUntil1.compareTo(dateUntil2);
    }
    return result;
}

From source file:org.lockss.daemon.CrawlWindows.java

/**
 * Returns a List of {@link TimeInterval}s for the crawl window between a given
 * start and end point.  Each represents a period when the window is open.
 * @param window the CrawlWindow//from w w w.ja  va 2s . com
 * @param start the start Date
 * @param end the end Date
 * @return a list of TimeIntervals
 */
public static synchronized List getCrawlIntervals(CrawlWindow window, Date start, Date end) {
    List intervals = new ArrayList();
    boolean isOpen = false;
    Calendar startCal = Calendar.getInstance();
    startCal.setTime(start);
    Date testDate = startCal.getTime();
    Date intStartDate = null;
    while (testDate.compareTo(end) < 0) {
        boolean canCrawl = window.canCrawl(testDate);
        if (canCrawl != isOpen) {
            if (!isOpen) {
                // cache the start of the next interval
                intStartDate = testDate;
            } else {
                // close the current interval and add to list
                intervals.add(new TimeInterval(intStartDate, testDate));
                if (intervals.size() == MAX_INTERVAL_LIST_SIZE) {
                    logger.warning("Maximum interval list size reached: " + MAX_INTERVAL_LIST_SIZE);
                    break;
                }
                intStartDate = null;
            }
            isOpen = canCrawl;
        }
        // increment by one minute
        startCal.add(Calendar.MINUTE, 1);
        testDate = startCal.getTime();
    }

    if (intStartDate != null) {
        intervals.add(new TimeInterval(intStartDate, end));
    }

    return intervals;
}

From source file:com.fengduo.bee.commons.util.DateViewTools.java

/**
 * ??,??/*from  w w  w .j  a v  a  2 s . com*/
 * 
 * <pre>
 * ?currentTime > date 
 * ? 2011-11-4 ,isExpirationTime(2011-11-4 ) ture
 * ? 2011-11-5 ,isExpirationTime(2011-11-4 ) true
 * 
 * </pre>
 * 
 * @param date
 *            ?
 * @return true ?<code>true</code><code>null</code>
 *         <code>true</code>,<code>false</code>
 */
public static boolean isExpirationTime(Date date) {
    if (date == null) {
        return true;
    }
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    String nows = format.format(new Date());
    Date now = null;
    try {
        now = format.parse(nows);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (date.compareTo(now) < 0) {// ??
        return true;
    } else {// ??
        return false;
    }
}

From source file:org.openmrs.module.atomfeed.AtomFeedUtil.java

/**
 * This method writes the atom feed data to the given stream. <br/>
 * The given stream is not closed//from  w  ww  . j  ava2 s  . c  om
 * 
 * @param stream an open outputstream that will be written to
 * @param asOfDate if not null, limits the entries to only ones updated after this date
 * @should download full stream with null date
 * @should download partial stream by given date
 * @should stream multiline entry
 */
public static void getAtomFeedStream(OutputStream stream, Date asOfDate) {
    OutputStream out = new BufferedOutputStream(stream);

    File atomheaderfile = getFeedHeaderFile();
    if (atomheaderfile.exists()) {
        try {
            // stream the atom header to output
            String atomHeader = FileUtils.readFileToString(atomheaderfile);
            // truncate "</feed>" from the atom header string
            if (StringUtils.isNotBlank(atomHeader)) {
                atomHeader = StringUtils.substringBeforeLast(atomHeader, "</feed>");
            }
            // write part of the header to the stream 
            out.write(atomHeader.getBytes());

            // then stream the entries to the output
            File atomfile = getFeedEntriesFile();

            // if the date filtering parameter is passed in
            // we need to limit the entries to only ones, which were 
            // updated after this date
            if (asOfDate != null) {
                String entry;
                BufferedReader br = new BufferedReader(new FileReader(atomfile));
                while ((entry = br.readLine()) != null) {
                    // if current entry has a new line then handle it gracefully
                    while (!StringUtils.endsWith(entry, "</entry>")) {
                        String newLine = br.readLine();
                        // if end of file is reached and new line does not contain new entry
                        if (newLine != null && !StringUtils.contains(newLine, "<entry>")) {
                            entry = entry.concat("\n").concat(newLine);
                        } else {
                            // otherwise an invalid entry is found, terminate processing
                            throw new Exception("Invalid atom feed entry. No end tag </entry> found.");
                        }
                    }
                    Date updated = new SimpleDateFormat(RFC_3339_DATE_FORMAT)
                            .parse(StringUtils.substringBetween(entry, "<updated>", "</updated>"));
                    if (updated.compareTo(asOfDate) > -1) {
                        // write entry to the stream 
                        entry = entry.concat("\n");
                        out.write(entry.getBytes());
                    } else {
                        // if entry with updatedDate lower that given one is reached
                        // we need to stop filtering
                        break;
                    }
                }
            } else {
                // bulk write all entries to the stream 
                out.write(FileUtils.readFileToByteArray(atomfile));
            }

            // write the "</feed>" element that isn't in the entries file (and was
            // in the header file)
            out.write("</feed>".getBytes());
            out.flush();
        } catch (Exception e) {
            log.error("Unable to stream atom header file and/or entries file, because of error: ", e);
        }
    }
}

From source file:com.sammyun.util.DateUtil.java

/**
 * ?? //from  w w  w . j av  a 2s . c  o m
 * 
 * @param startDate
 * @param endDate
 * @param specDate
 * @return ?
 * @throws ParseException
 */
public static int getContainCount(Date startDate, Date endDate, List<Date> specDate) throws ParseException {
    List<Date> dateList = new ArrayList<Date>();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    startDate = sdf.parse(date2String(startDate, 10));
    endDate = sdf.parse(date2String(endDate, 10));
    for (Date date : specDate) {
        if (date.compareTo(startDate) >= 0 && date.compareTo(endDate) < 0) {
            dateList.add(date);
        }
    }
    return dateList.size();
}

From source file:org.mifos.framework.util.helpers.DateUtils.java

public static int whichDirection(Date date) {
    Calendar currentCalendar = getCurrentDateCalendar();
    java.sql.Date currentDate = new java.sql.Date(currentCalendar.getTimeInMillis());

    // check if the date is before now

    int result = date.compareTo(currentDate);
    return result;
}