Example usage for org.joda.time MutableDateTime addSeconds

List of usage examples for org.joda.time MutableDateTime addSeconds

Introduction

In this page you can find the example usage for org.joda.time MutableDateTime addSeconds.

Prototype

public void addSeconds(final int seconds) 

Source Link

Document

Add a number of seconds to the date.

Usage

From source file:org.codelibs.elasticsearch.common.joda.DateMathParser.java

License:Apache License

private long parseMath(String mathString, long time, boolean roundUp, DateTimeZone timeZone)
        throws ElasticsearchParseException {
    if (timeZone == null) {
        timeZone = DateTimeZone.UTC;//from  w  ww  .j  a v  a 2s  .c  o m
    }
    MutableDateTime dateTime = new MutableDateTime(time, timeZone);
    for (int i = 0; i < mathString.length();) {
        char c = mathString.charAt(i++);
        final boolean round;
        final int sign;
        if (c == '/') {
            round = true;
            sign = 1;
        } else {
            round = false;
            if (c == '+') {
                sign = 1;
            } else if (c == '-') {
                sign = -1;
            } else {
                throw new ElasticsearchParseException("operator not supported for date math [{}]", mathString);
            }
        }

        if (i >= mathString.length()) {
            throw new ElasticsearchParseException("truncated date math [{}]", mathString);
        }

        final int num;
        if (!Character.isDigit(mathString.charAt(i))) {
            num = 1;
        } else {
            int numFrom = i;
            while (i < mathString.length() && Character.isDigit(mathString.charAt(i))) {
                i++;
            }
            if (i >= mathString.length()) {
                throw new ElasticsearchParseException("truncated date math [{}]", mathString);
            }
            num = Integer.parseInt(mathString.substring(numFrom, i));
        }
        if (round) {
            if (num != 1) {
                throw new ElasticsearchParseException("rounding `/` can only be used on single unit types [{}]",
                        mathString);
            }
        }
        char unit = mathString.charAt(i++);
        MutableDateTime.Property propertyToRound = null;
        switch (unit) {
        case 'y':
            if (round) {
                propertyToRound = dateTime.yearOfCentury();
            } else {
                dateTime.addYears(sign * num);
            }
            break;
        case 'M':
            if (round) {
                propertyToRound = dateTime.monthOfYear();
            } else {
                dateTime.addMonths(sign * num);
            }
            break;
        case 'w':
            if (round) {
                propertyToRound = dateTime.weekOfWeekyear();
            } else {
                dateTime.addWeeks(sign * num);
            }
            break;
        case 'd':
            if (round) {
                propertyToRound = dateTime.dayOfMonth();
            } else {
                dateTime.addDays(sign * num);
            }
            break;
        case 'h':
        case 'H':
            if (round) {
                propertyToRound = dateTime.hourOfDay();
            } else {
                dateTime.addHours(sign * num);
            }
            break;
        case 'm':
            if (round) {
                propertyToRound = dateTime.minuteOfHour();
            } else {
                dateTime.addMinutes(sign * num);
            }
            break;
        case 's':
            if (round) {
                propertyToRound = dateTime.secondOfMinute();
            } else {
                dateTime.addSeconds(sign * num);
            }
            break;
        default:
            throw new ElasticsearchParseException("unit [{}] not supported for date math [{}]", unit,
                    mathString);
        }
        if (propertyToRound != null) {
            if (roundUp) {
                // we want to go up to the next whole value, even if we are already on a rounded value
                propertyToRound.add(1);
                propertyToRound.roundFloor();
                dateTime.addMillis(-1); // subtract 1 millisecond to get the largest inclusive value
            } else {
                propertyToRound.roundFloor();
            }
        }
    }
    return dateTime.getMillis();
}

From source file:org.elasticsearch.common.joda.DateMathParser.java

License:Apache License

private long parseMath(String mathString, long time, boolean roundUp) throws ElasticsearchParseException {
    MutableDateTime dateTime = new MutableDateTime(time, DateTimeZone.UTC);
    try {/*from   w  ww  .j  a  v a2  s . c o  m*/
        for (int i = 0; i < mathString.length();) {
            char c = mathString.charAt(i++);
            int type;
            if (c == '/') {
                type = 0;
            } else if (c == '+') {
                type = 1;
            } else if (c == '-') {
                type = 2;
            } else {
                throw new ElasticsearchParseException(
                        "operator not supported for date math [" + mathString + "]");
            }

            int num;
            if (!Character.isDigit(mathString.charAt(i))) {
                num = 1;
            } else {
                int numFrom = i;
                while (Character.isDigit(mathString.charAt(i))) {
                    i++;
                }
                num = Integer.parseInt(mathString.substring(numFrom, i));
            }
            if (type == 0) {
                // rounding is only allowed on whole numbers
                if (num != 1) {
                    throw new ElasticsearchParseException(
                            "rounding `/` can only be used on single unit types [" + mathString + "]");
                }
            }
            char unit = mathString.charAt(i++);
            switch (unit) {
            case 'y':
                if (type == 0) {
                    if (roundUp) {
                        dateTime.yearOfCentury().roundCeiling();
                    } else {
                        dateTime.yearOfCentury().roundFloor();
                    }
                } else if (type == 1) {
                    dateTime.addYears(num);
                } else if (type == 2) {
                    dateTime.addYears(-num);
                }
                break;
            case 'M':
                if (type == 0) {
                    if (roundUp) {
                        dateTime.monthOfYear().roundCeiling();
                    } else {
                        dateTime.monthOfYear().roundFloor();
                    }
                } else if (type == 1) {
                    dateTime.addMonths(num);
                } else if (type == 2) {
                    dateTime.addMonths(-num);
                }
                break;
            case 'w':
                if (type == 0) {
                    if (roundUp) {
                        dateTime.weekOfWeekyear().roundCeiling();
                    } else {
                        dateTime.weekOfWeekyear().roundFloor();
                    }
                } else if (type == 1) {
                    dateTime.addWeeks(num);
                } else if (type == 2) {
                    dateTime.addWeeks(-num);
                }
                break;
            case 'd':
                if (type == 0) {
                    if (roundUp) {
                        dateTime.dayOfMonth().roundCeiling();
                    } else {
                        dateTime.dayOfMonth().roundFloor();
                    }
                } else if (type == 1) {
                    dateTime.addDays(num);
                } else if (type == 2) {
                    dateTime.addDays(-num);
                }
                break;
            case 'h':
            case 'H':
                if (type == 0) {
                    if (roundUp) {
                        dateTime.hourOfDay().roundCeiling();
                    } else {
                        dateTime.hourOfDay().roundFloor();
                    }
                } else if (type == 1) {
                    dateTime.addHours(num);
                } else if (type == 2) {
                    dateTime.addHours(-num);
                }
                break;
            case 'm':
                if (type == 0) {
                    if (roundUp) {
                        dateTime.minuteOfHour().roundCeiling();
                    } else {
                        dateTime.minuteOfHour().roundFloor();
                    }
                } else if (type == 1) {
                    dateTime.addMinutes(num);
                } else if (type == 2) {
                    dateTime.addMinutes(-num);
                }
                break;
            case 's':
                if (type == 0) {
                    if (roundUp) {
                        dateTime.secondOfMinute().roundCeiling();
                    } else {
                        dateTime.secondOfMinute().roundFloor();
                    }
                } else if (type == 1) {
                    dateTime.addSeconds(num);
                } else if (type == 2) {
                    dateTime.addSeconds(-num);
                }
                break;
            default:
                throw new ElasticsearchParseException(
                        "unit [" + unit + "] not supported for date math [" + mathString + "]");
            }
        }
    } catch (Exception e) {
        if (e instanceof ElasticsearchParseException) {
            throw (ElasticsearchParseException) e;
        }
        throw new ElasticsearchParseException("failed to parse date math [" + mathString + "]");
    }
    return dateTime.getMillis();
}

From source file:org.n52.sos.cache.AbstractCacheScheduler.java

License:Apache License

public MutableDateTime resolveNextScheduleDate(LocalTime localTime, DateTime referenceTime) {
    /*//from   w  w  w  .  ja v a 2  s .  co  m
     * every 4am, starting with next
     */
    MutableDateTime mdt = referenceTime.toMutableDateTime();
    mdt.setHourOfDay(localTime.getHourOfDay());
    mdt.setMinuteOfHour(localTime.getMinuteOfHour());
    mdt.setSecondOfMinute(localTime.getSecondOfMinute());

    if (!referenceTime.isBefore(mdt)) {
        mdt.addDays(1);
    }

    Random random = new Random();
    mdt.addSeconds(random.nextInt(11) * 2);
    return mdt;
}

From source file:tv.arte.resteventapi.core.services.impl.EventServiceImpl.java

License:Open Source License

/**
 * {@inheritDoc}/*  ww  w .j  a v  a  2  s. co m*/
 */
public RestEvent processRestEvent(String id) {

    RestEvent restEvent = restEventRepository.findById(id);

    if (restEvent == null) {
        logger.warn("Unable to process event with id " + id + ". The event do not exists.");
        return restEvent;
    }

    if (RestEventStatus.SENT.equals(restEvent.getStatus())) {
        logger.info(
                "No action will be performed for event with id " + id + ". The event has already been sent.");
        return restEvent;
    }

    if (restEvent.getMaxAttempts() <= restEvent.getCounter()) {
        logger.info("No action will be performed for event with id " + id
                + ". The event has no more attempts left.");
        return restEvent;
    }

    if (logger.isDebugEnabled())
        logger.debug("Event scheduled for " + restEvent.getNextExecution().toString()
                + ", is acctualy executed at " + Calendar.getInstance().getTime().toString());

    try {
        RestClientExecutionResult executionResult = RestEventApiRestClient.execute(restEvent);
        restEvent.setHttpResponseCode(executionResult.getResponseCode());

        if (RestClientCallState.OK.equals(executionResult.getState())
                && ((executionResult.getResponseCode() >= 200 && executionResult.getResponseCode() < 300)
                        || executionResult.getResponseCode() == 304)) {
            restEvent.setStatus(RestEventStatus.SENT);
        } else {
            if (RestClientCallState.TIMEOUT.equals(executionResult.getState())) {
                logger.info("The request for RestEvent with id " + id + " has been timed out.");
            }

            restEvent.setStatus(RestEventStatus.ERROR);
        }
    } catch (Exception e) {
        restEvent.setStatus(RestEventStatus.ERROR);
    }

    restEvent.setCounter(restEvent.getCounter() != null ? restEvent.getCounter() + 1 : 1);

    if (!RestEventStatus.SENT.equals(restEvent.getStatus())
            && restEvent.getCounter() < restEvent.getMaxAttempts()) {
        MutableDateTime nextExecution = new MutableDateTime();
        nextExecution.addSeconds(restEvent.getInterval());
        restEvent.setNextExecution(nextExecution.toDate());
    }

    restEvent.setVersion(restEvent.getVersion() != null ? restEvent.getVersion() + 1L : 1L);
    restEvent.setLastModified(Calendar.getInstance().getTime());

    restEventRepository.save(restEvent);

    return restEvent;
}