Java Code Examples for java.time.temporal.Temporal#plus()

The following examples show how to use java.time.temporal.Temporal#plus() . 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 openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Temporal addTo(Temporal temporal) {
    validateChrono(temporal);
    if (months == 0) {
        if (years != 0) {
            temporal = temporal.plus(years, YEARS);
        }
    } else {
        long monthRange = monthRange();
        if (monthRange > 0) {
            temporal = temporal.plus(years * monthRange + months, MONTHS);
        } else {
            if (years != 0) {
                temporal = temporal.plus(years, YEARS);
            }
            temporal = temporal.plus(months, MONTHS);
        }
    }
    if (days != 0) {
        temporal = temporal.plus(days, DAYS);
    }
    return temporal;
}
 
Example 2
Source File: ChronoPeriodImpl.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Temporal addTo(Temporal temporal) {
    validateChrono(temporal);
    if (months == 0) {
        if (years != 0) {
            temporal = temporal.plus(years, YEARS);
        }
    } else {
        long monthRange = monthRange();
        if (monthRange > 0) {
            temporal = temporal.plus(years * monthRange + months, MONTHS);
        } else {
            if (years != 0) {
                temporal = temporal.plus(years, YEARS);
            }
            temporal = temporal.plus(months, MONTHS);
        }
    }
    if (days != 0) {
        temporal = temporal.plus(days, DAYS);
    }
    return temporal;
}
 
Example 3
Source File: ChronoPeriodImpl.java    From desugar_jdk_libs with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Temporal addTo(Temporal temporal) {
    validateChrono(temporal);
    if (months == 0) {
        if (years != 0) {
            temporal = temporal.plus(years, YEARS);
        }
    } else {
        long monthRange = monthRange();
        if (monthRange > 0) {
            temporal = temporal.plus(years * monthRange + months, MONTHS);
        } else {
            if (years != 0) {
                temporal = temporal.plus(years, YEARS);
            }
            temporal = temporal.plus(months, MONTHS);
        }
    }
    if (days != 0) {
        temporal = temporal.plus(days, DAYS);
    }
    return temporal;
}
 
Example 4
Source File: ChronoPeriodImpl.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@Override
public Temporal addTo(Temporal temporal) {
    validateChrono(temporal);
    if (months == 0) {
        if (years != 0) {
            temporal = temporal.plus(years, YEARS);
        }
    } else {
        long monthRange = monthRange();
        if (monthRange > 0) {
            temporal = temporal.plus(years * monthRange + months, MONTHS);
        } else {
            if (years != 0) {
                temporal = temporal.plus(years, YEARS);
            }
            temporal = temporal.plus(months, MONTHS);
        }
    }
    if (days != 0) {
        temporal = temporal.plus(days, DAYS);
    }
    return temporal;
}
 
Example 5
Source File: WorkingDaysAdjusters.java    From alf.io with GNU General Public License v3.0 6 votes vote down vote up
private static Temporal adjust(Temporal in, Set<DayOfWeek> dayOfWeeks, List<HoursRange> hoursRanges) {
    DayOfWeek dayOfWeek = DayOfWeek.from(in);
    LocalTime localTime = LocalTime.from(in);
    boolean dayInRange = dayOfWeeks.contains(dayOfWeek);
    boolean hourInRange = hoursRanges.stream().anyMatch(hr -> hr.includes(localTime));
    if(dayInRange && hourInRange) {
        return in;
    }
    Temporal result = in;
    if(!dayInRange) {
        do {
            result = result.plus(1, ChronoUnit.DAYS);
        } while(!dayOfWeeks.contains(DayOfWeek.from(result)));
    }
    if(!hourInRange) {
        OptionalInt distance = hoursRanges.stream()
                .mapToInt(hr -> hr.getDistanceInHours(localTime))
                .sorted()
                .findFirst();
        result = result.plus(distance.orElseThrow(IllegalStateException::new), ChronoUnit.HOURS);
    }
    return result;
}
 
Example 6
Source File: ChronoPeriodImpl.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Temporal addTo(Temporal temporal) {
    validateChrono(temporal);
    if (months == 0) {
        if (years != 0) {
            temporal = temporal.plus(years, YEARS);
        }
    } else {
        long monthRange = monthRange();
        if (monthRange > 0) {
            temporal = temporal.plus(years * monthRange + months, MONTHS);
        } else {
            if (years != 0) {
                temporal = temporal.plus(years, YEARS);
            }
            temporal = temporal.plus(months, MONTHS);
        }
    }
    if (days != 0) {
        temporal = temporal.plus(days, DAYS);
    }
    return temporal;
}
 
Example 7
Source File: Time.java    From manifold with Apache License 2.0 5 votes vote down vote up
@Override
public Temporal addTo( Temporal temporal )
{
  BigInteger wholePart = toBaseNumber().wholePart();
  if( !wholePart.equals( BigInteger.ZERO ) )
  {
    temporal = temporal.plus( wholePart.longValue(), SECONDS );
  }
  Rational fractionPart = toBaseNumber().fractionPart();
  if( !fractionPart.equals( Rational.ZERO ) )
  {
    temporal = temporal.plus( fractionPart.times( 1.0e9 ).longValue(), NANOS );
  }
  return temporal;
}
 
Example 8
Source File: MockSimplePeriod.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Temporal addTo(Temporal temporal) {
    return temporal.plus(amount, unit);
}
 
Example 9
Source File: Period.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * Adds this period to the specified temporal object.
 * <p>
 * This returns a temporal object of the same observable type as the input
 * with this period added.
 * If the temporal has a chronology, it must be the ISO chronology.
 * <p>
 * In most cases, it is clearer to reverse the calling pattern by using
 * {@link Temporal#plus(TemporalAmount)}.
 * <pre>
 *   // these two lines are equivalent, but the second approach is recommended
 *   dateTime = thisPeriod.addTo(dateTime);
 *   dateTime = dateTime.plus(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.
 * <p>
 * This approach ensures that a partial period can be added to a partial date.
 * For example, a period of years and/or months can be added to a {@code YearMonth},
 * but a period including days cannot.
 * The approach also adds years and months together when necessary, which ensures
 * correct behaviour at the end of the month.
 * <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 add
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public Temporal addTo(Temporal temporal) {
    validateChrono(temporal);
    if (months == 0) {
        if (years != 0) {
            temporal = temporal.plus(years, YEARS);
        }
    } else {
        long totalMonths = toTotalMonths();
        if (totalMonths != 0) {
            temporal = temporal.plus(totalMonths, MONTHS);
        }
    }
    if (days != 0) {
        temporal = temporal.plus(days, DAYS);
    }
    return temporal;
}
 
Example 10
Source File: MockSimplePeriod.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Temporal addTo(Temporal temporal) {
    return temporal.plus(amount, unit);
}
 
Example 11
Source File: MockSimplePeriod.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Temporal addTo(Temporal temporal) {
    return temporal.plus(amount, unit);
}
 
Example 12
Source File: Period.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds this period to the specified temporal object.
 * <p>
 * This returns a temporal object of the same observable type as the input
 * with this period added.
 * If the temporal has a chronology, it must be the ISO chronology.
 * <p>
 * In most cases, it is clearer to reverse the calling pattern by using
 * {@link Temporal#plus(TemporalAmount)}.
 * <pre>
 *   // these two lines are equivalent, but the second approach is recommended
 *   dateTime = thisPeriod.addTo(dateTime);
 *   dateTime = dateTime.plus(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.
 * <p>
 * This approach ensures that a partial period can be added to a partial date.
 * For example, a period of years and/or months can be added to a {@code YearMonth},
 * but a period including days cannot.
 * The approach also adds years and months together when necessary, which ensures
 * correct behaviour at the end of the month.
 * <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 add
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public Temporal addTo(Temporal temporal) {
    validateChrono(temporal);
    if (months == 0) {
        if (years != 0) {
            temporal = temporal.plus(years, YEARS);
        }
    } else {
        long totalMonths = toTotalMonths();
        if (totalMonths != 0) {
            temporal = temporal.plus(totalMonths, MONTHS);
        }
    }
    if (days != 0) {
        temporal = temporal.plus(days, DAYS);
    }
    return temporal;
}
 
Example 13
Source File: MockSimplePeriod.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Temporal addTo(Temporal temporal) {
    return temporal.plus(amount, unit);
}
 
Example 14
Source File: MockSimplePeriod.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Temporal addTo(Temporal temporal) {
    return temporal.plus(amount, unit);
}
 
Example 15
Source File: Period.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds this period to the specified temporal object.
 * <p>
 * This returns a temporal object of the same observable type as the input
 * with this period added.
 * If the temporal has a chronology, it must be the ISO chronology.
 * <p>
 * In most cases, it is clearer to reverse the calling pattern by using
 * {@link Temporal#plus(TemporalAmount)}.
 * <pre>
 *   // these two lines are equivalent, but the second approach is recommended
 *   dateTime = thisPeriod.addTo(dateTime);
 *   dateTime = dateTime.plus(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.
 * <p>
 * This approach ensures that a partial period can be added to a partial date.
 * For example, a period of years and/or months can be added to a {@code YearMonth},
 * but a period including days cannot.
 * The approach also adds years and months together when necessary, which ensures
 * correct behaviour at the end of the month.
 * <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 add
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public Temporal addTo(Temporal temporal) {
    validateChrono(temporal);
    if (months == 0) {
        if (years != 0) {
            temporal = temporal.plus(years, YEARS);
        }
    } else {
        long totalMonths = toTotalMonths();
        if (totalMonths != 0) {
            temporal = temporal.plus(totalMonths, MONTHS);
        }
    }
    if (days != 0) {
        temporal = temporal.plus(days, DAYS);
    }
    return temporal;
}
 
Example 16
Source File: MockSimplePeriod.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Temporal addTo(Temporal temporal) {
    return temporal.plus(amount, unit);
}
 
Example 17
Source File: SchedulerImplTest.java    From openhab-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Temporal adjustInto(Temporal arg0) {
    return arg0.plus(100, ChronoUnit.MILLIS);
}
 
Example 18
Source File: MockSimplePeriod.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Override
public Temporal addTo(Temporal temporal) {
    return temporal.plus(amount, unit);
}
 
Example 19
Source File: Duration.java    From jdk8u-dev-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Adds this duration to the specified temporal object.
 * <p>
 * This returns a temporal object of the same observable type as the input
 * with this duration added.
 * <p>
 * In most cases, it is clearer to reverse the calling pattern by using
 * {@link Temporal#plus(TemporalAmount)}.
 * <pre>
 *   // these two lines are equivalent, but the second approach is recommended
 *   dateTime = thisDuration.addTo(dateTime);
 *   dateTime = dateTime.plus(thisDuration);
 * </pre>
 * <p>
 * The calculation will add 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 add
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public Temporal addTo(Temporal temporal) {
    if (seconds != 0) {
        temporal = temporal.plus(seconds, SECONDS);
    }
    if (nanos != 0) {
        temporal = temporal.plus(nanos, NANOS);
    }
    return temporal;
}
 
Example 20
Source File: Duration.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Adds this duration to the specified temporal object.
 * <p>
 * This returns a temporal object of the same observable type as the input
 * with this duration added.
 * <p>
 * In most cases, it is clearer to reverse the calling pattern by using
 * {@link Temporal#plus(TemporalAmount)}.
 * <pre>
 *   // these two lines are equivalent, but the second approach is recommended
 *   dateTime = thisDuration.addTo(dateTime);
 *   dateTime = dateTime.plus(thisDuration);
 * </pre>
 * <p>
 * The calculation will add 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 add
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public Temporal addTo(Temporal temporal) {
    if (seconds != 0) {
        temporal = temporal.plus(seconds, SECONDS);
    }
    if (nanos != 0) {
        temporal = temporal.plus(nanos, NANOS);
    }
    return temporal;
}