Example usage for java.util Calendar compareTo

List of usage examples for java.util Calendar compareTo

Introduction

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

Prototype

private int compareTo(long t) 

Source Link

Usage

From source file:Main.java

public static void main(String[] args) {

    // create two calendar at the different dates
    Calendar cal1 = new GregorianCalendar(2015, 8, 15);
    Calendar cal2 = new GregorianCalendar(2013, 1, 02);

    // compare the time values represented by two calendar objects.
    int i = cal1.compareTo(cal2);

    // return positive value if equals else return negative value
    System.out.println("The result is :" + i);

    // compare again but with the two calendars swapped
    int j = cal2.compareTo(cal1);

    // return positive value if equals else return negative value
    System.out.println("The result is :" + j);

}

From source file:Main.java

public static int compareTo(Calendar cal1, Calendar cal2) {
    return cal1.compareTo(cal2);
}

From source file:Main.java

public static boolean isExpire(Calendar expireTime) {
    Calendar now = Calendar.getInstance();
    if (now.compareTo(expireTime) >= 0) {
        return true;
    } else {/*from   ww w.ja v a2s . c o m*/
        return false;
    }
}

From source file:Main.java

public static boolean dataEhMenorOuIgual(Calendar data, Calendar dataFinalCiclo) {
    return data.before(dataFinalCiclo) || data.compareTo(dataFinalCiclo) == 0;
}

From source file:Main.java

public static boolean dataEhMaiorOuIgual(Calendar dataFinal, Calendar dataInicial) {
    return dataFinal.after(dataInicial) || dataFinal.compareTo(dataInicial) == 0;
}

From source file:nl.utwente.bigdata.MapReduce5.java

private static String parseAndMatchTime(String date, Configuration conf) {
    String[] splitdate = date.toString().split("\\s");
    int year = Integer.parseInt(splitdate[5]);
    int month = translateMonthCalendar(splitdate[1]);
    int day = Integer.parseInt(splitdate[2]);
    int hour = Integer.parseInt(splitdate[3].substring(0, 2));
    int minute = Integer.parseInt(splitdate[3].substring(3, 5));
    int second = Integer.parseInt(splitdate[3].substring(6, 7) + "0");
    Calendar start = Calendar.getInstance();
    start.set(Integer.parseInt(conf.get("year")), translateMonthCalendar(conf.get("month")),
            Integer.parseInt(conf.get("day")), Integer.parseInt(conf.get("hour")),
            Integer.parseInt(conf.get("minute")));
    Calendar end = (Calendar) start.clone();
    end.add(Calendar.MINUTE, Integer.parseInt(conf.get("duration")));
    Calendar value = Calendar.getInstance();
    value.set(year, month, day, hour, minute, second);
    if (value.compareTo(start) >= 0 && value.compareTo(end) <= 0) {
        return year + ":" + translateMonth(splitdate[1]) + ":" + day + ":" + hour + ":" + minute + ":" + second;
    } else/* w w  w. j a va2 s. c o  m*/
        return "false";
}

From source file:com.castis.xylophone.adsmadapter.common.util.CiDateUtil.java

public static int countDay(Calendar start, Calendar end, InventoryBoxDayCode dayCode) {

    int count = 0;

    while (start.compareTo(end) <= 0) {

        int day = start.get(Calendar.DAY_OF_WEEK);

        if (dayCode != null) {
            if (Calendar.MONDAY <= day && Calendar.FRIDAY >= day) {
                if (dayCode.equals(InventoryBoxDayCode.WEEKDAY))
                    count++;/*  ww w.ja v  a2 s. c o  m*/
            } else {
                if (dayCode.equals(InventoryBoxDayCode.WEEKEND))
                    count++;
            }
        } else {
            count++;
        }

        start.add(Calendar.DAY_OF_MONTH, 1);
    }

    return count;
}

From source file:Main.java

/**
 * Get a list of initial and end calendar of months in the range received
 * @param cStart Calendar date to start//from   w  ww  .j  av a 2  s.  co  m
 * @param cEnd Calendar date to end
 * @return List<Pair<Calendar, Calendar>> list of calendars (initial and end)
 */
public static List<Pair<Calendar, Calendar>> getRangeInMonths(Calendar cStart, Calendar cEnd) {

    //generate the list
    List<Pair<Calendar, Calendar>> calendars = new ArrayList<>();

    //from the first calendar start adding a month until the actual calendar is after the end
    Calendar cActual = generateCalendar(cStart);
    cActual.set(Calendar.DAY_OF_MONTH, 1);
    Calendar c0;
    Calendar cF;

    while (cActual.compareTo(cEnd) < 0) {

        //calendar start
        if (calendars.size() == 0) {
            c0 = generateCalendar(cStart);
        } else {
            c0 = generateCalendar(cActual);
        }

        //increment a month
        cActual.add(Calendar.MONTH, 1);

        //calendar end
        if (cActual.after(cEnd)) {
            cF = generateCalendar(cEnd);
        } else {
            cF = generateCalendar(cActual);

            //remove 1 day to set the last day of the month
            cF.add(Calendar.DAY_OF_YEAR, -1);
        }

        //add the pair to the list
        calendars.add(new Pair<Calendar, Calendar>(c0, cF));
    }

    //return the list
    return calendars;
}

From source file:Main.java

private static Calendar prepareCalendar(int hourOfDay, int minute) {
    Calendar calNow = Calendar.getInstance();
    Calendar calSet = (Calendar) calNow.clone();

    calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
    calSet.set(Calendar.MINUTE, minute);
    calSet.set(Calendar.SECOND, 0);
    calSet.set(Calendar.MILLISECOND, 0);

    if (calSet.compareTo(calNow) <= 0) {
        // Today Set time passed, count to tomorrow
        calSet.add(Calendar.DATE, 1);
    }/*from  ww w  .  j a va2s  .  c o m*/
    return calSet;
}

From source file:org.onehippo.forge.hst.version.util.JcrVersionUtils.java

/**
 * Finds the latest version of the {@code versionableNode} as of {@code asOf} datetime
 * from the given {@code linearVersions}.
 * If {@code asOf} is null, then returns the latest version.
 * @param versionableNode versionable node (type of mix:versionable)
 * @param linearVersions linear versions list
 * @param asOf {@code asOf} datetime// ww  w  .  ja  v  a2 s  . c  om
 * @return the latest version of the {@code versionableNode} as of {@code asOf} datetime
 *         If {@code asOf} is null, then returns the latest version.
 * @throws RepositoryException if unexpected repository exception occurs
 */
public static Version getVersionAsOf(final Node versionableNode, final List<Version> linearVersions,
        final Calendar asOf) throws RepositoryException {
    Version asOfVersion = null;

    if (asOf == null) {
        return getRootVersion(versionableNode, linearVersions);
    } else {
        for (Version version : linearVersions) {
            if (hasLiveFrozenNode(version)) {
                Calendar created = version.getCreated();
                int compare = created.compareTo(asOf);

                if (compare <= 0) {
                    asOfVersion = version;
                } else {
                    break;
                }
            }
        }
    }

    return asOfVersion;
}