Example usage for java.time ZonedDateTime compareTo

List of usage examples for java.time ZonedDateTime compareTo

Introduction

In this page you can find the example usage for java.time ZonedDateTime compareTo.

Prototype

@Override
default int compareTo(ChronoZonedDateTime<?> other) 

Source Link

Document

Compares this date-time to another date-time, including the chronology.

Usage

From source file:io.stallion.services.SecureTempTokens.java

public TempToken fetchToken(String tokenString) {
    if (StringUtils.isBlank(tokenString)) {
        throw new ClientException("The passed in token is empty");
    }//from   w  ww. j a  v a  2s.  c o  m
    TempToken token = DB.instance().fetchBean(TempToken.class,
            "SELECT * FROM stallion_temp_tokens WHERE token=? LIMIT 1", tokenString);
    if (token == null) {
        throw new ClientException("Token not found");
    }
    ZonedDateTime now = DateUtils.utcNow();
    if (now.compareTo(token.getExpiresAt()) > 0) {
        throw new ClientException("Token has expired");
    }
    if (token.getUsedAt() != null) {
        throw new ClientException("Token was already used");
    }
    return token;
}

From source file:com.omertron.slackbot.functions.scheduler.AbstractBotTask.java

/**
 * Calculate the time between "now" and the execution time.
 *
 * @param targetHour/* w  w  w . ja v  a2s  . com*/
 * @param targetMin
 * @param targetSec
 * @return
 */
private long computeNextDelay(int targetHour, int targetMin, int targetSec) {
    ZonedDateTime zonedNow = localeDateTime();
    ZonedDateTime zonedNextTarget = zonedNow.withHour(targetHour).withMinute(targetMin).withSecond(targetSec)
            .withNano(0);

    if (zonedNow.compareTo(zonedNextTarget) >= 0) {
        zonedNextTarget = zonedNextTarget.plusDays(1);
    }

    Duration duration = Duration.between(zonedNow, zonedNextTarget);

    // If we are scheduled within the next minute, then skip a day as we probably just ran fast
    if (duration.getSeconds() <= 60l) {
        zonedNextTarget = zonedNextTarget.plusDays(1);
        duration = Duration.between(zonedNow, zonedNextTarget);
    }

    return duration.getSeconds();
}

From source file:org.edgexfoundry.scheduling.ScheduleContext.java

private ZonedDateTime initNextTime(ZonedDateTime start, ZonedDateTime now, Period period, Duration duration) {
    // if the start time is in the future next will just be start
    ZonedDateTime next = start;
    // if the start time is in the past, increment until we find the next time to execute
    // cannot call isComplete() here as it depends on nextTime
    if (startTime.compareTo(now) <= 0 && !schedule.getRunOnce()) {
        // TODO: optimize the below. consider a one-second timer case...
        // For example if only a single unit, e.g. only minutes, then can optimize relative to start
        // if there are more than one unit, then the loop may be best as it will be difficult
        while (next.compareTo(now) <= 0) {
            next = next.plus(period);//  ww  w  .  j  a v a  2s  . co m
            next = next.plus(duration);
        }
    }
    return next;
}