Example usage for java.util Calendar after

List of usage examples for java.util Calendar after

Introduction

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

Prototype

public boolean after(Object when) 

Source Link

Document

Returns whether this Calendar represents a time after the time represented by the specified Object.

Usage

From source file:eionet.gdem.qa.WQCleanerJob.java

/**
 * Check the job's age and return true if it is possible to delete it.
 *
 * @param job//from  w  ww  .  j  av  a 2 s . c  o m
 *            Workqueue job object
 * @return true if job can be deleted.
 */
public static boolean canDeleteJob(WorkqueueJob job) {
    boolean canDelete = false;
    if (job != null && job.getJobTimestamp() != null && job.getStatus() >= Constants.XQ_READY) {
        Calendar now = Calendar.getInstance();
        int maxAge = Properties.wqJobMaxAge == 0 ? -1 : -Properties.wqJobMaxAge;
        now.add(Calendar.HOUR, maxAge);
        Calendar jobCal = Calendar.getInstance();
        jobCal.setTime(job.getJobTimestamp());
        if (now.after(jobCal)) {
            canDelete = true;
        }
    }
    return canDelete;
}

From source file:nl.strohalm.cyclos.utils.DateHelper.java

/**
 * a null proof method returning the earliest of any number of Calendars
 * @param dates any null arguments are ignored. If all arguments are null just returns null.
 * @return the earliest date of the arguments.
 *///from w  ww. ja  v  a 2  s. c om
public static Calendar getEarliest(final Calendar... dates) {
    Calendar oldest = null;
    for (final Calendar date : dates) {
        if (oldest == null || (date != null && oldest.after(date))) {
            oldest = date;
        }
    }
    return oldest;
}

From source file:org.apache.metron.parsers.utils.DateUtils.java

/**
 * Parse the data according to a sequence of possible parse patterns.
 * /*from w  ww  . j a  v a2  s  .c  o m*/
 * If the given date is entirely numeric, it is assumed to be a unix
 * timestamp.
 * 
 * If the year is not specified in the date string, use the current year.
 * Assume that any date more than 4 days in the future is in the past as per
 * SyslogUtils
 * 
 * @param candidate
 *            The possible date.
 * @param validPatterns
 *            A list of SimpleDateFormat instances to try parsing with.
 * @return A java.util.Date based on the parse result
 * @throws ParseException
 */
public static long parseMultiformat(String candidate, List<SimpleDateFormat> validPatterns)
        throws ParseException {
    if (StringUtils.isNumeric(candidate)) {
        return Long.valueOf(candidate);
    } else {
        for (SimpleDateFormat pattern : validPatterns) {
            try {
                Calendar cal = Calendar.getInstance();
                cal.setTime(pattern.parse(candidate));
                Calendar current = Calendar.getInstance();
                if (cal.get(Calendar.YEAR) == 1970) {
                    cal.set(Calendar.YEAR, current.get(Calendar.YEAR));
                }
                current.add(Calendar.DAY_OF_MONTH, 4);
                if (cal.after(current)) {
                    cal.add(Calendar.YEAR, -1);
                }
                return cal.getTimeInMillis();
            } catch (ParseException e) {
                continue;
            }
        }
        throw new ParseException("Failed to parse any of the given date formats", 0);
    }
}

From source file:org.apache.gobblin.service.modules.orchestration.AzkabanAjaxAPIClient.java

/***
 * Generate a random scheduled time between specified execution time window in the Azkaban compatible format
 * which is: hh,mm,a,z Eg. ScheduleTime=12,00,PM,PDT
 *
 * @param windowStartHour Window start hour in 24 hr (HH) format (inclusive)
 * @param windowEndHour Window end hour in 24 hr (HH) format (exclusive)
 * @param delayMinutes If current time is within window, then additional delay for bootstrapping if desired
 * @return Scheduled time string of the format hh,mm,a,z
 */// w w  w .ja  v a 2s . c o  m
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "DMI_RANDOM_USED_ONLY_ONCE", justification = "As expected for randomization")
public static String getScheduledTimeInAzkabanFormat(int windowStartHour, int windowEndHour, int delayMinutes) {
    // Validate
    if (windowStartHour < 0 || windowEndHour > 23 || windowStartHour >= windowEndHour) {
        throw new IllegalArgumentException(
                "Window start should be less than window end, and both should be between " + "0 and 23");
    }
    if (delayMinutes < 0 || delayMinutes > 59) {
        throw new IllegalArgumentException("Delay in minutes should be between 0 and 59 (inclusive)");
    }

    // Setup window
    Calendar windowStartTime = Calendar.getInstance();
    windowStartTime.set(Calendar.HOUR_OF_DAY, windowStartHour);
    windowStartTime.set(Calendar.MINUTE, 0);
    windowStartTime.set(Calendar.SECOND, 0);

    Calendar windowEndTime = Calendar.getInstance();
    windowEndTime.set(Calendar.HOUR_OF_DAY, windowEndHour);
    windowEndTime.set(Calendar.MINUTE, 0);
    windowEndTime.set(Calendar.SECOND, 0);

    // Check if current time is between windowStartTime and windowEndTime, then let the execution happen
    // after delayMinutes minutes
    Calendar now = Calendar.getInstance();
    if (now.after(windowStartTime) && now.before(windowEndTime)) {
        // Azkaban takes a few seconds / a minute to bootstrap,
        // so extra few minutes get the first execution to run instantly
        now.add(Calendar.MINUTE, delayMinutes);

        return new SimpleDateFormat("hh,mm,a,z").format(now.getTime());
    }

    // Current time is not between windowStartTime and windowEndTime, so get random execution time for next day
    int allowedSchedulingWindow = (int) ((windowEndTime.getTimeInMillis() - windowStartTime.getTimeInMillis())
            / MILLISECONDS_IN_HOUR);
    int randomHourInWindow = new Random(System.currentTimeMillis()).nextInt(allowedSchedulingWindow);
    int randomMinute = new Random(System.currentTimeMillis()).nextInt(60);
    windowStartTime.add(Calendar.HOUR, randomHourInWindow);
    windowStartTime.set(Calendar.MINUTE, randomMinute);

    return new SimpleDateFormat("hh,mm,a,z").format(windowStartTime.getTime());
}

From source file:de.azapps.mirakel.reminders.ReminderAlarm.java

public static void init(final Context ctx) {
    observer = of(new MirakelContentObserver(new Handler(ctx.getMainLooper()), ctx, Task.URI,
            new MirakelContentObserver.ObserverCallBack() {
                @Override/*ww  w .j  a  v a 2s  . c o  m*/
                public void handleChange() {
                    updateAlarms(ctx);
                }

                @Override
                public void handleChange(final long id) {
                    final Optional<Task> t = Task.get(id);
                    final Calendar c = new GregorianCalendar();
                    if (t.isPresent()) {
                        final Task task = t.get();
                        if (task.getReminder().isPresent() && c.after(task.getReminder().get())) {
                            updateAlarm(ctx, t.get());
                        } else {
                            cancelAlarm(ctx, t.get());
                        }
                    }

                }
            }));
    updateAlarms(ctx);
}

From source file:com.glaf.core.util.DateUtils.java

/**
 * ?/* ww  w .j  a v  a2s . c  o  m*/
 * 
 * @param startDate
 * @param endDate
 * @return
 */
public static int getDaysBetween(Calendar startDate, Calendar endDate) {
    if (startDate.after(endDate)) {
        java.util.Calendar swap = startDate;
        startDate = endDate;
        endDate = swap;
    }
    int days = endDate.get(java.util.Calendar.DAY_OF_YEAR) - startDate.get(java.util.Calendar.DAY_OF_YEAR);
    int y2 = endDate.get(java.util.Calendar.YEAR);
    if (startDate.get(java.util.Calendar.YEAR) != y2) {
        startDate = (java.util.Calendar) startDate.clone();
        do {
            days += startDate.getActualMaximum(java.util.Calendar.DAY_OF_YEAR);
            startDate.add(java.util.Calendar.YEAR, 1);
        } while (startDate.get(java.util.Calendar.YEAR) != y2);
    }
    return days;
}

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 . java  2 s .  c  o  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:com.glaf.core.util.DateUtils.java

/**
 * //from   w ww .  j  a  v a 2 s  .  c  om
 * 
 * @param startDate
 * @param endDate
 * @return
 */
public static int getWorkingDay(Calendar startDate, Calendar endDate) {
    int result = -1;
    if (startDate.after(endDate)) {
        java.util.Calendar swap = startDate;
        startDate = endDate;
        endDate = swap;
    }

    int charge_start_date = 0;// ???
    int charge_end_date = 0;// ????
    // ??
    int stmp;
    int etmp;
    stmp = 7 - startDate.get(Calendar.DAY_OF_WEEK);
    etmp = 7 - endDate.get(Calendar.DAY_OF_WEEK);
    if (stmp != 0 && stmp != 6) {// ???0
        charge_start_date = stmp - 1;
    }
    if (etmp != 0 && etmp != 6) {// ????0
        charge_end_date = etmp - 1;
    }

    result = (getDaysBetween(getNextMonday(startDate), getNextMonday(endDate)) / 7) * 5 + charge_start_date
            - charge_end_date;
    return result;
}

From source file:com.vmware.identity.rest.core.server.util.VerificationUtil.java

/**
 * Verify the issued-at and expires-at dates in an access token
 *
 * @param token the token to verify/*from  w ww. j  a v  a  2s. co  m*/
 * @param skew the amount of skew to allow in milliseconds
 * @param sm a string manager to get the exception messages from
 *
 * @throws InvalidTokenException if the token is at an invalid date
 */
public static void verifyTimestamps(AccessToken token, long skew, StringManager sm)
        throws InvalidTokenException {
    Calendar now = Calendar.getInstance();
    Calendar issuedAt = Calendar.getInstance();
    Calendar expiresAt = Calendar.getInstance();

    if (token.getIssueTime() != null) {
        issuedAt.setTimeInMillis(token.getIssueTime().getTime() - skew);
    }

    if (token.getExpirationTime() != null) {
        expiresAt.setTimeInMillis(token.getExpirationTime().getTime() + skew);
    }

    if (token.getIssueTime() == null || issuedAt.after(now)) {
        throw new InvalidTokenException(sm.getString("auth.ite.bad.issue", issuedAt.getTime(), now.getTime()));
    }

    if (token.getExpirationTime() == null || expiresAt.before(now)) {
        throw new InvalidTokenException(
                sm.getString("auth.ite.bad.expiry", expiresAt.getTime(), now.getTime()));
    }
}

From source file:fr.inria.ucn.Helpers.java

/**
 * @param c//from w  ww.jav  a 2  s .c  om
 * @return <code>True</code> if user has enabled the night-time mode and current time is
 * within night, else <code>False</code>.
 */
public static boolean isNightTime(Context c) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
    if (!prefs.getBoolean(Constants.PREF_STOP_NIGHT, false))
        return false;

    int nstart = prefs.getInt(Constants.PREF_NIGHT_START, 23 * 3600);
    int nstop = prefs.getInt(Constants.PREF_NIGHT_STOP, 6 * 3600);

    Calendar nightstart = Calendar.getInstance();
    nightstart.roll(Calendar.HOUR_OF_DAY, -1 * nightstart.get(Calendar.HOUR_OF_DAY));
    nightstart.roll(Calendar.MINUTE, -1 * nightstart.get(Calendar.MINUTE));
    nightstart.roll(Calendar.SECOND, -1 * nightstart.get(Calendar.SECOND));
    nightstart.roll(Calendar.MILLISECOND, -1 * nightstart.get(Calendar.MILLISECOND));
    nightstart.add(Calendar.SECOND, nstart);

    Calendar nightstop = Calendar.getInstance();
    nightstop.roll(Calendar.HOUR_OF_DAY, -1 * nightstop.get(Calendar.HOUR_OF_DAY));
    nightstop.roll(Calendar.MINUTE, -1 * nightstop.get(Calendar.MINUTE));
    nightstop.roll(Calendar.SECOND, -1 * nightstop.get(Calendar.SECOND));
    nightstop.roll(Calendar.MILLISECOND, -1 * nightstop.get(Calendar.MILLISECOND));
    nightstop.add(Calendar.SECOND, nstop);
    if (nightstop.before(nightstart))
        nightstop.add(Calendar.HOUR, 24);

    Log.d(Constants.LOGTAG, "nightstart " + nstart + " -> " + nightstart.toString());
    Log.d(Constants.LOGTAG, "nightstop " + nstop + " -> " + nightstop.toString());

    Calendar now = Calendar.getInstance();
    return (now.after(nightstart) && now.before(nightstop));
}