Example usage for org.joda.time DateTime minus

List of usage examples for org.joda.time DateTime minus

Introduction

In this page you can find the example usage for org.joda.time DateTime minus.

Prototype

public DateTime minus(ReadablePeriod period) 

Source Link

Document

Returns a copy of this datetime with the specified period taken away.

Usage

From source file:org.obm.imap.archive.services.SchedulingDatesService.java

License:Open Source License

public DateTime higherBoundary(DateTime treatmentDate, RepeatKind repeatKind) {
    return treatmentDate.minus(RepeatKind.toPeriod(repeatKind, 1)).withTime(23, 59, 59, 999);
}

From source file:org.openmrs.module.reporting.dataset.definition.evaluator.RepeatPerTimePeriodDataSetEvaluator.java

License:Open Source License

@Override
public DataSet evaluate(DataSetDefinition dataSetDefinition, EvaluationContext evalContext)
        throws EvaluationException {
    RepeatPerTimePeriodDataSetDefinition dsd = (RepeatPerTimePeriodDataSetDefinition) dataSetDefinition;

    Mapped<? extends DataSetDefinition> baseMappedDef = dsd.getBaseDefinition();

    MultiParameterDataSetDefinition delegate = new MultiParameterDataSetDefinition(
            baseMappedDef.getParameterizable());

    TimePeriod period = dsd.getRepeatPerTimePeriod();
    if (period == null) {
        throw new IllegalArgumentException("repeatPerTimePeriod is required");
    }//  w ww.j  ava  2s. c  o m

    DateTime thisPeriodStart = new DateTime(((Date) evalContext.getParameterValue("startDate")).getTime());
    DateTime end = new DateTime(
            DateUtil.getEndOfDayIfTimeExcluded((Date) evalContext.getParameterValue("endDate")).getTime());

    while (thisPeriodStart.isBefore(end)) {
        DateTime nextPeriodStart = thisPeriodStart.plus(period.getJodaPeriod());
        boolean lastIteration = !nextPeriodStart.isBefore(end); // i.e. nextPeriodStart >= end
        DateTime thisPeriodEnd;
        if (lastIteration) {
            thisPeriodEnd = end;
        } else {
            thisPeriodEnd = nextPeriodStart.minus(Duration.millis(1));
        }

        Map<String, Object> startAndEndDate = new HashMap<String, Object>();
        startAndEndDate.put("startDate", thisPeriodStart.toDate());
        startAndEndDate.put("endDate", thisPeriodEnd.toDate());
        Map<String, Object> iteration = new HashMap<String, Object>();
        for (Map.Entry<String, Object> entry : baseMappedDef.getParameterMappings().entrySet()) {
            String iterationParamName = entry.getKey();
            Object value = entry.getValue();
            if (value instanceof String) {
                Object evaluated = EvaluationUtil.evaluateExpression((String) value, startAndEndDate);
                // expressions based on parameters other than startDate/endDate will come out like ${loc} -> "loc"
                if (value.equals(EvaluationUtil.EXPRESSION_START + evaluated + EvaluationUtil.EXPRESSION_END)) {
                    continue;
                }
                value = evaluated;
            }
            iteration.put(iterationParamName, value);
        }
        delegate.addIteration(iteration);

        thisPeriodStart = nextPeriodStart;
    }

    return dataSetDefinitionService.evaluate(delegate, evalContext);
}

From source file:org.opensaml.saml.saml2.assertion.SAML20AssertionValidator.java

License:Open Source License

/**
 * Validates the NotBefore and NotOnOrAfter Conditions constraints on the assertion.
 * //w w  w .  j  a v  a  2s . c  o  m
 * @param assertion the assertion whose conditions will be validated
 * @param context current validation context
 * 
 * @return the result of the validation evaluation
 * 
 * @throws AssertionValidationException thrown if there is a problem determining the validity of the conditions
 */
@Nonnull
protected ValidationResult validateConditionsTimeBounds(@Nonnull final Assertion assertion,
        @Nonnull final ValidationContext context) throws AssertionValidationException {

    Conditions conditions = assertion.getConditions();
    if (conditions == null) {
        return ValidationResult.VALID;
    }

    DateTime now = new DateTime(ISOChronology.getInstanceUTC());
    long clockSkew = getClockSkew(context);

    DateTime notBefore = conditions.getNotBefore();
    log.debug("Evaluating Conditions NotBefore '{}' against 'skewed now' time '{}'", notBefore,
            now.plus(clockSkew));
    if (notBefore != null && notBefore.isAfter(now.plus(clockSkew))) {
        context.setValidationFailureMessage(
                String.format("Assertion '%s' with NotBefore condition of '%s' is not yet valid",
                        assertion.getID(), notBefore));
        return ValidationResult.INVALID;
    }

    DateTime notOnOrAfter = conditions.getNotOnOrAfter();
    log.debug("Evaluating Conditions NotOnOrAfter '{}' against 'skewed now' time '{}'", notOnOrAfter,
            now.minus(clockSkew));
    if (notOnOrAfter != null && notOnOrAfter.isBefore(now.minus(clockSkew))) {
        context.setValidationFailureMessage(
                String.format("Assertion '%s' with NotOnOrAfter condition of '%s' is no longer valid",
                        assertion.getID(), notOnOrAfter));
        return ValidationResult.INVALID;
    }

    return ValidationResult.VALID;
}

From source file:org.qipki.crypto.x509.X509GeneratorImpl.java

License:Open Source License

@Override
public X509Certificate generateX509Certificate(PrivateKey privateKey, DistinguishedName issuerDN,
        BigInteger serialNumber, DistinguishedName subjectDN, PublicKey publicKey, Duration validity,
        List<X509ExtensionHolder> x509Extensions) {
    try {//from   w w  w.j a  v  a 2  s.  c om

        X509V3CertificateGenerator x509v3Generator = new X509V3CertificateGenerator();

        DateTime now = new DateTime();

        x509v3Generator.setSerialNumber(serialNumber);
        x509v3Generator.setSubjectDN(subjectDN.toX500Principal());
        x509v3Generator.setIssuerDN(issuerDN.toX500Principal());
        x509v3Generator.setNotBefore(now.minus(Time.CLOCK_SKEW).toDate());
        x509v3Generator.setNotAfter(now.plus(validity).minus(Time.CLOCK_SKEW).toDate());
        x509v3Generator.setSignatureAlgorithm(SignatureAlgorithm.SHA256withRSA.jcaString());
        x509v3Generator.setPublicKey(publicKey);

        for (X509ExtensionHolder eachExtensionHolder : x509Extensions) {
            x509v3Generator.addExtension(eachExtensionHolder.getDerOID(), eachExtensionHolder.isCritical(),
                    eachExtensionHolder.getValue());
        }

        return x509v3Generator.generate(privateKey, cryptoContext.providerName());

    } catch (GeneralSecurityException ex) {
        throw new CryptoFailure("Unable to generate X509Certificate", ex);
    } catch (IllegalStateException ex) {
        throw new CryptoFailure("Unable to generate X509Certificate", ex);
    }
}

From source file:org.sleuthkit.autopsy.timeline.utils.IntervalUtils.java

License:Open Source License

static public Interval getIntervalAround(DateTime aroundInstant, ReadablePeriod period) {
    DateTime start = aroundInstant.minus(period);
    DateTime end = aroundInstant.plus(period);
    Interval range = new Interval(start, end);
    DateTime middleOf = IntervalUtils.middleOf(range);
    long halfRange = range.toDurationMillis() / 4;
    final Interval newInterval = new Interval(middleOf.minus(halfRange), middleOf.plus(halfRange));
    return newInterval;
}

From source file:org.springframework.analytics.metrics.AggregateCounterResolution.java

License:Apache License

/**
 * Subtracts this resolution a given number of times from a supplied date.
 *
 * @param dt the date to subtract from/*from  w  w  w.  ja  v  a  2s  .  c o m*/
 * @param n the number of periods of this resolution to subtract
 * @return the resulting date in the past.
 */
public DateTime minus(DateTime dt, int n) {
    DateTime start = dt;
    for (int i = 0; i < n; i++) {
        start = start.minus(unitPeriod);
    }
    return start;
}

From source file:TVShowTimelineMaker.timeConstraints.CharacterRelation.java

@Override
public boolean consistentWithConstraint(DateTime inFirstDay, DateTime inSecondDay) {
    boolean con = super.consistentWithConstraint(inFirstDay, inSecondDay);
    if (con) {//from   w w  w.  j a  va 2s . co  m
        if (this.mCharacterRelationKind.equals(CharacterRelationKind.MOTHER)) {
            DateTime lowerBoundOnBirthDay1 = inSecondDay.minusYears(START_OF_MENOPAUSE).withHourOfDay(1);
            DateTime upperBoundOnBirthDay1 = inSecondDay.minus(this.mCharacterRelationKind.mPeriod)
                    .withHourOfDay(23);
            con = inFirstDay.isAfter(lowerBoundOnBirthDay1) && inFirstDay.isBefore(upperBoundOnBirthDay1);
        }
    }
    return con;
}

From source file:TVShowTimelineMaker.timeConstraints.CharacterRelation.java

@Override
public boolean complexApplyConstraint() {
    boolean changed = super.complexApplyConstraint();
    if (this.mCharacterRelationKind.equals(CharacterRelationKind.MOTHER)) {
        if ((!this.mFirstNamedCharacter.getBirthday().isMarkedForComplexEval())
                && this.mSecondNamedCharacter.getBirthday().isMarkedForComplexEval()) {
            this.mFirstNamedCharacter.getBirthday().setUpForComplexEval();
        }/*from  ww  w  .  j  av  a 2  s. c  om*/
        if (this.mFirstNamedCharacter.getBirthday().isMarkedForComplexEval()
                && (!this.mSecondNamedCharacter.getBirthday().isMarkedForComplexEval())) {
            this.mSecondNamedCharacter.getBirthday().setUpForComplexEval();
        }
        if (this.mFirstNamedCharacter.getBirthday().isMarkedForComplexEval()
                && this.mSecondNamedCharacter.getBirthday().isMarkedForComplexEval()) {
            NavigableSet<DateTime> firstEpisodeDates = this.mFirstNamedCharacter.getBirthday()
                    .getPossibleDays();
            NavigableSet<DateTime> secondEpisodeDates = this.mSecondNamedCharacter.getBirthday()
                    .getPossibleDays();
            DateTime lowerBoundOnBirthDay1 = secondEpisodeDates.first().minusYears(START_OF_MENOPAUSE)
                    .withHourOfDay(1);
            DateTime upperBoundOnBirthDay1 = secondEpisodeDates.first()
                    .minus(this.mCharacterRelationKind.mPeriod).withHourOfDay(23);
            Iterator<DateTime> firstEpisodeDatesIterator = firstEpisodeDates.iterator();
            Iterator<DateTime> secondEpisodeDatesIterator = secondEpisodeDates.iterator();
            DateTime curFirstEpisodeDate = firstEpisodeDatesIterator.next();
            DateTime curSecondEpisodeDate = secondEpisodeDatesIterator.next();
            boolean cont = true;
            //while (indexInToFirstEpisodeDatesDates < firstEpisodeDates.size()) {
            while (cont) {
                if (curFirstEpisodeDate.isBefore(lowerBoundOnBirthDay1)) {
                    firstEpisodeDatesIterator.remove();
                    if (firstEpisodeDatesIterator.hasNext()) {
                        curFirstEpisodeDate = firstEpisodeDatesIterator.next();
                    } else {
                        cont = false;
                    }
                    changed = true;
                } else if (curFirstEpisodeDate.isAfter(upperBoundOnBirthDay1)) {
                    if (secondEpisodeDatesIterator.hasNext()) {
                        curSecondEpisodeDate = secondEpisodeDatesIterator.next();
                        lowerBoundOnBirthDay1 = curSecondEpisodeDate.minusYears(START_OF_MENOPAUSE)
                                .withHourOfDay(1);
                        upperBoundOnBirthDay1 = curSecondEpisodeDate.minus(this.mCharacterRelationKind.mPeriod)
                                .withHourOfDay(23);
                    } else {
                        firstEpisodeDatesIterator.remove();
                        if (firstEpisodeDatesIterator.hasNext()) {
                            curFirstEpisodeDate = firstEpisodeDatesIterator.next();
                        } else {
                            cont = false;
                        }
                        changed = true;
                    }
                } else {
                    if (firstEpisodeDatesIterator.hasNext()) {
                        curFirstEpisodeDate = firstEpisodeDatesIterator.next();
                    } else {
                        cont = false;
                    }
                }
            }
            firstEpisodeDatesIterator = firstEpisodeDates.iterator();
            secondEpisodeDatesIterator = secondEpisodeDates.iterator();
            curFirstEpisodeDate = firstEpisodeDatesIterator.next();
            curSecondEpisodeDate = secondEpisodeDatesIterator.next();
            cont = true;
            DateTime lowerBoundOnBirthDay2 = firstEpisodeDates.first().plus(this.mCharacterRelationKind.mPeriod)
                    .withHourOfDay(1);
            DateTime upperBoundOnBirthDay2 = firstEpisodeDates.first().plusYears(START_OF_MENOPAUSE)
                    .withHourOfDay(23);
            while (cont) {
                if (curSecondEpisodeDate.isBefore(lowerBoundOnBirthDay2)) {
                    secondEpisodeDatesIterator.remove();
                    if (secondEpisodeDatesIterator.hasNext()) {
                        curSecondEpisodeDate = secondEpisodeDatesIterator.next();
                    } else {
                        cont = false;
                    }
                    changed = true;
                } else if (curSecondEpisodeDate.isAfter(upperBoundOnBirthDay2)) {
                    if (firstEpisodeDatesIterator.hasNext()) {
                        curFirstEpisodeDate = firstEpisodeDatesIterator.next();
                        lowerBoundOnBirthDay2 = curFirstEpisodeDate.plus(this.mCharacterRelationKind.mPeriod)
                                .withHourOfDay(1);
                        upperBoundOnBirthDay2 = curFirstEpisodeDate.plusYears(START_OF_MENOPAUSE)
                                .withHourOfDay(23);
                    } else {
                        secondEpisodeDatesIterator.remove();
                        if (secondEpisodeDatesIterator.hasNext()) {
                            curSecondEpisodeDate = secondEpisodeDatesIterator.next();
                        } else {
                            cont = false;
                        }
                        changed = true;
                    }
                } else {
                    if (secondEpisodeDatesIterator.hasNext()) {
                        curSecondEpisodeDate = secondEpisodeDatesIterator.next();
                    } else {
                        cont = false;
                    }
                }
            }
        }
    }
    return changed;
}

From source file:TVShowTimelineMaker.timeConstraints.CharacterRelation.java

@Override
public boolean consistentWithConstraint(Placement[] inValues) {
    boolean consistent = super.consistentWithConstraint(inValues);
    DateTime inFirstStartTime;/*from  w  w w  . ja v  a 2 s . c o m*/
    DateTime inFirstEndTime;
    DateTime inSecondStartTime;
    DateTime inSecondEndTime;
    if (this.mFirstNamedCharacter.getBirthday() instanceof OnceDayEvent) {
        OnceDayEvent.OnceDayEventPlacement curOnceDayEventPlacement = (OnceDayEvent.OnceDayEventPlacement) inValues[0];
        inFirstStartTime = curOnceDayEventPlacement.day;
        inFirstEndTime = curOnceDayEventPlacement.day;
    } else {
        OncePeriodEvent.OncePeriodEventPlacement curOncePeriodEventPlacement = (OncePeriodEvent.OncePeriodEventPlacement) inValues[0];
        inFirstStartTime = curOncePeriodEventPlacement.startDay;
        inFirstEndTime = curOncePeriodEventPlacement.endDay;
    }
    if (this.mSecondNamedCharacter.getBirthday() instanceof OnceDayEvent) {
        OnceDayEvent.OnceDayEventPlacement curOnceDayEventPlacement = (OnceDayEvent.OnceDayEventPlacement) inValues[1];
        inSecondStartTime = curOnceDayEventPlacement.day;
        inSecondEndTime = curOnceDayEventPlacement.day;
    } else {
        OncePeriodEvent.OncePeriodEventPlacement curOncePeriodEventPlacement = (OncePeriodEvent.OncePeriodEventPlacement) inValues[1];
        inSecondStartTime = curOncePeriodEventPlacement.startDay;
        inSecondEndTime = curOncePeriodEventPlacement.endDay;
    }
    if (consistent) {
        if (this.mCharacterRelationKind.equals(CharacterRelationKind.MOTHER)) {
            DateTime lowerBoundOnBirthDay1 = inSecondStartTime.minusYears(START_OF_MENOPAUSE).withHourOfDay(1);
            DateTime upperBoundOnBirthDay1 = inSecondStartTime.minus(this.mCharacterRelationKind.mPeriod)
                    .withHourOfDay(23);
            consistent = inFirstEndTime.isAfter(lowerBoundOnBirthDay1)
                    && inFirstEndTime.isBefore(upperBoundOnBirthDay1);
        }
    }
    return consistent;
}

From source file:TVShowTimelineMaker.timeConstraints.CharacterRelation.java

@Override
public Event[] increaseWhat(Placement inValues[]) {
    Event[] rValue = super.increaseWhat(inValues);
    DateTime inFirstStartTime;/*ww  w  .j ava 2 s . c o m*/
    DateTime inFirstEndTime;
    DateTime inSecondStartTime;
    DateTime inSecondEndTime;
    if (this.mFirstNamedCharacter.getBirthday() instanceof OnceDayEvent) {
        OnceDayEvent.OnceDayEventPlacement curOnceDayEventPlacement = (OnceDayEvent.OnceDayEventPlacement) inValues[0];
        inFirstStartTime = curOnceDayEventPlacement.day;
        inFirstEndTime = curOnceDayEventPlacement.day;
    } else {
        OncePeriodEvent.OncePeriodEventPlacement curOncePeriodEventPlacement = (OncePeriodEvent.OncePeriodEventPlacement) inValues[0];
        inFirstStartTime = curOncePeriodEventPlacement.startDay;
        inFirstEndTime = curOncePeriodEventPlacement.endDay;
    }
    if (this.mSecondNamedCharacter.getBirthday() instanceof OnceDayEvent) {
        OnceDayEvent.OnceDayEventPlacement curOnceDayEventPlacement = (OnceDayEvent.OnceDayEventPlacement) inValues[1];
        inSecondStartTime = curOnceDayEventPlacement.day;
        inSecondEndTime = curOnceDayEventPlacement.day;
    } else {
        OncePeriodEvent.OncePeriodEventPlacement curOncePeriodEventPlacement = (OncePeriodEvent.OncePeriodEventPlacement) inValues[1];
        inSecondStartTime = curOncePeriodEventPlacement.startDay;
        inSecondEndTime = curOncePeriodEventPlacement.endDay;
    }
    if (rValue.length == 0) {
        if (this.mCharacterRelationKind.equals(CharacterRelationKind.MOTHER)) {
            DateTime lowerBoundOnBirthDay1 = inSecondStartTime.minusYears(START_OF_MENOPAUSE).withHourOfDay(1);
            DateTime upperBoundOnBirthDay1 = inSecondStartTime.minus(this.mCharacterRelationKind.mPeriod)
                    .withHourOfDay(23);
            if (!inFirstEndTime.isAfter(lowerBoundOnBirthDay1)) {
                rValue = new Event[] { this.getFirstEvent() };
            }
            if (!inFirstEndTime.isBefore(upperBoundOnBirthDay1)) {
                rValue = new Event[] { this.getSecondEvent() };
            }
        }
    }
    return rValue;
}