Java Code Examples for org.threeten.bp.temporal.Temporal#minus()

The following examples show how to use org.threeten.bp.temporal.Temporal#minus() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChronoPeriodImpl.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Temporal subtractFrom(Temporal temporal) {
    Jdk8Methods.requireNonNull(temporal, "temporal");
    Chronology temporalChrono = temporal.query(TemporalQueries.chronology());
    if (temporalChrono != null && chronology.equals(temporalChrono) == false) {
        throw new DateTimeException("Invalid chronology, required: " + chronology.getId() + ", but was: " + temporalChrono.getId());
    }
    if (years != 0) {
        temporal = temporal.minus(years, YEARS);
    }
    if (months != 0) {
        temporal = temporal.minus(months, MONTHS);
    }
    if (days != 0) {
        temporal = temporal.minus(days, DAYS);
    }
    return temporal;
}
 
Example 2
Source File: Period.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Subtracts this period from the specified temporal object.
 * <p>
 * This returns a temporal object of the same observable type as the input
 * with this period subtracted.
 * <p>
 * In most cases, it is clearer to reverse the calling pattern by using
 * {@link Temporal#minus(TemporalAmount)}.
 * <pre>
 *   // these two lines are equivalent, but the second approach is recommended
 *   dateTime = thisPeriod.subtractFrom(dateTime);
 *   dateTime = dateTime.minus(thisPeriod);
 * </pre>
 * <p>
 * The calculation operates as follows.
 * First, the chronology of the temporal is checked to ensure it is ISO chronology or null.
 * Second, if the months are zero, the years are added if non-zero, otherwise
 * the combination of years and months is added if non-zero.
 * Finally, any days are added.
 * 
 * The calculation will subtract the years, then months, then days.
 * Only non-zero amounts will be subtracted.
 * If the date-time has a calendar system with a fixed number of months in a
 * year, then the years and months will be combined before being subtracted.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param temporal  the temporal object to adjust, not null
 * @return an object of the same type with the adjustment made, not null
 * @throws DateTimeException if unable to subtract
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public Temporal subtractFrom(Temporal temporal) {
    Jdk8Methods.requireNonNull(temporal, "temporal");
    if (years != 0) {
        if (months != 0) {
            temporal = temporal.minus(toTotalMonths(), MONTHS);
        } else {
            temporal = temporal.minus(years, YEARS);
        }
    } else if (months != 0) {
        temporal = temporal.minus(months, MONTHS);
    }
    if (days != 0) {
        temporal = temporal.minus(days, DAYS);
    }
    return temporal;
}
 
Example 3
Source File: MockSimplePeriod.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Temporal subtractFrom(Temporal dateTime) {
    return dateTime.minus(amount, unit);
}
 
Example 4
Source File: Duration.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Subtracts this duration from the specified temporal object.
 * <p>
 * This returns a temporal object of the same observable type as the input
 * with this duration subtracted.
 * <p>
 * In most cases, it is clearer to reverse the calling pattern by using
 * {@link Temporal#minus(TemporalAmount)}.
 * <pre>
 *   // these two lines are equivalent, but the second approach is recommended
 *   dateTime = thisDuration.subtractFrom(dateTime);
 *   dateTime = dateTime.minus(thisDuration);
 * </pre>
 * <p>
 * The calculation will subtract the seconds, then nanos.
 * Only non-zero amounts will be added.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param temporal  the temporal object to adjust, not null
 * @return an object of the same type with the adjustment made, not null
 * @throws DateTimeException if unable to subtract
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public Temporal subtractFrom(Temporal temporal) {
    if (seconds != 0) {
        temporal = temporal.minus(seconds, SECONDS);
    }
    if (nanos != 0) {
        temporal = temporal.minus(nanos, NANOS);
    }
    return temporal;
}