Java Code Examples for java.time.temporal.TemporalUnit.isDateBased()

The following are Jave code examples for showing how to use isDateBased() of the java.time.temporal.TemporalUnit class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: OpenJSharp   File: ZonedDateTime.java   Source Code and License Vote up 3 votes
/**
 * Returns a copy of this date-time with the specified amount added.
 * <p>
 * This returns a {@code ZonedDateTime}, based on this one, with the amount
 * in terms of the unit added. If it is not possible to add the amount, because the
 * unit is not supported or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoUnit} then the addition is implemented here.
 * The zone is not part of the calculation and will be unchanged in the result.
 * The calculation for date and time units differ.
 * <p>
 * Date units operate on the local time-line.
 * The period is first added to the local date-time, then converted back
 * to a zoned date-time using the zone ID.
 * The conversion uses {@link #ofLocal(LocalDateTime, ZoneId, ZoneOffset)}
 * with the offset before the addition.
 * <p>
 * Time units operate on the instant time-line.
 * The period is first added to the local date-time, then converted back to
 * a zoned date-time using the zone ID.
 * The conversion uses {@link #ofInstant(LocalDateTime, ZoneOffset, ZoneId)}
 * with the offset before the addition.
 * <p>
 * If the field is not a {@code ChronoUnit}, then the result of this method
 * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
 * passing {@code this} as the argument. In this case, the unit determines
 * whether and how to perform the addition.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param amountToAdd  the amount of the unit to add to the result, may be negative
 * @param unit  the unit of the amount to add, not null
 * @return a {@code ZonedDateTime} based on this date-time with the specified amount added, not null
 * @throws DateTimeException if the addition cannot be made
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public ZonedDateTime plus(long amountToAdd, TemporalUnit unit) {
    if (unit instanceof ChronoUnit) {
        if (unit.isDateBased()) {
            return resolveLocal(dateTime.plus(amountToAdd, unit));
        } else {
            return resolveInstant(dateTime.plus(amountToAdd, unit));
        }
    }
    return unit.addTo(this, amountToAdd);
}
 
Example 2
Project: OpenJSharp   File: ZonedDateTime.java   Source Code and License Vote up 3 votes
/**
 * Calculates the amount of time until another date-time in terms of the specified unit.
 * <p>
 * This calculates the amount of time between two {@code ZonedDateTime}
 * objects in terms of a single {@code TemporalUnit}.
 * The start and end points are {@code this} and the specified date-time.
 * The result will be negative if the end is before the start.
 * For example, the period in days between two date-times can be calculated
 * using {@code startDateTime.until(endDateTime, DAYS)}.
 * <p>
 * The {@code Temporal} passed to this method is converted to a
 * {@code ZonedDateTime} using {@link #from(TemporalAccessor)}.
 * If the time-zone differs between the two zoned date-times, the specified
 * end date-time is normalized to have the same zone as this date-time.
 * <p>
 * The calculation returns a whole number, representing the number of
 * complete units between the two date-times.
 * For example, the period in months between 2012-06-15T00:00Z and 2012-08-14T23:59Z
 * will only be one month as it is one minute short of two months.
 * <p>
 * There are two equivalent ways of using this method.
 * The first is to invoke this method.
 * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
 * <pre>
 *   // these two lines are equivalent
 *   amount = start.until(end, MONTHS);
 *   amount = MONTHS.between(start, end);
 * </pre>
 * The choice should be made based on which makes the code more readable.
 * <p>
 * The calculation is implemented in this method for {@link ChronoUnit}.
 * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},
 * {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS}, {@code DAYS},
 * {@code WEEKS}, {@code MONTHS}, {@code YEARS}, {@code DECADES},
 * {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} are supported.
 * Other {@code ChronoUnit} values will throw an exception.
 * <p>
 * The calculation for date and time units differ.
 * <p>
 * Date units operate on the local time-line, using the local date-time.
 * For example, the period from noon on day 1 to noon the following day
 * in days will always be counted as exactly one day, irrespective of whether
 * there was a daylight savings change or not.
 * <p>
 * Time units operate on the instant time-line.
 * The calculation effectively converts both zoned date-times to instants
 * and then calculates the period between the instants.
 * For example, the period from noon on day 1 to noon the following day
 * in hours may be 23, 24 or 25 hours (or some other amount) depending on
 * whether there was a daylight savings change or not.
 * <p>
 * If the unit is not a {@code ChronoUnit}, then the result of this method
 * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
 * passing {@code this} as the first argument and the converted input temporal
 * as the second argument.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param endExclusive  the end date, exclusive, which is converted to a {@code ZonedDateTime}, not null
 * @param unit  the unit to measure the amount in, not null
 * @return the amount of time between this date-time and the end date-time
 * @throws DateTimeException if the amount cannot be calculated, or the end
 *  temporal cannot be converted to a {@code ZonedDateTime}
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
    ZonedDateTime end = ZonedDateTime.from(endExclusive);
    if (unit instanceof ChronoUnit) {
        end = end.withZoneSameInstant(zone);
        if (unit.isDateBased()) {
            return dateTime.until(end.dateTime, unit);
        } else {
            return toOffsetDateTime().until(end.toOffsetDateTime(), unit);
        }
    }
    return unit.between(this, end);
}
 
Example 3
Project: OpenJSharp   File: ChronoLocalDate.java   Source Code and License Vote up 3 votes
/**
 * Checks if the specified unit is supported.
 * <p>
 * This checks if the specified unit can be added to or subtracted from this date.
 * If false, then calling the {@link #plus(long, TemporalUnit)} and
 * {@link #minus(long, TemporalUnit) minus} methods will throw an exception.
 * <p>
 * The set of supported units is defined by the chronology and normally includes
 * all {@code ChronoUnit} date units except {@code FOREVER}.
 * <p>
 * If the unit is not a {@code ChronoUnit}, then the result of this method
 * is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)}
 * passing {@code this} as the argument.
 * Whether the unit is supported is determined by the unit.
 *
 * @param unit  the unit to check, null returns false
 * @return true if the unit can be added/subtracted, false if not
 */
@Override
default boolean isSupported(TemporalUnit unit) {
    if (unit instanceof ChronoUnit) {
        return unit.isDateBased();
    }
    return unit != null && unit.isSupportedBy(this);
}
 
Example 4
Project: jdk8u-jdk   File: ZonedDateTime.java   Source Code and License Vote up 3 votes
/**
 * Returns a copy of this date-time with the specified amount added.
 * <p>
 * This returns a {@code ZonedDateTime}, based on this one, with the amount
 * in terms of the unit added. If it is not possible to add the amount, because the
 * unit is not supported or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoUnit} then the addition is implemented here.
 * The zone is not part of the calculation and will be unchanged in the result.
 * The calculation for date and time units differ.
 * <p>
 * Date units operate on the local time-line.
 * The period is first added to the local date-time, then converted back
 * to a zoned date-time using the zone ID.
 * The conversion uses {@link #ofLocal(LocalDateTime, ZoneId, ZoneOffset)}
 * with the offset before the addition.
 * <p>
 * Time units operate on the instant time-line.
 * The period is first added to the local date-time, then converted back to
 * a zoned date-time using the zone ID.
 * The conversion uses {@link #ofInstant(LocalDateTime, ZoneOffset, ZoneId)}
 * with the offset before the addition.
 * <p>
 * If the field is not a {@code ChronoUnit}, then the result of this method
 * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
 * passing {@code this} as the argument. In this case, the unit determines
 * whether and how to perform the addition.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param amountToAdd  the amount of the unit to add to the result, may be negative
 * @param unit  the unit of the amount to add, not null
 * @return a {@code ZonedDateTime} based on this date-time with the specified amount added, not null
 * @throws DateTimeException if the addition cannot be made
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public ZonedDateTime plus(long amountToAdd, TemporalUnit unit) {
    if (unit instanceof ChronoUnit) {
        if (unit.isDateBased()) {
            return resolveLocal(dateTime.plus(amountToAdd, unit));
        } else {
            return resolveInstant(dateTime.plus(amountToAdd, unit));
        }
    }
    return unit.addTo(this, amountToAdd);
}
 
Example 5
Project: jdk8u-jdk   File: ZonedDateTime.java   Source Code and License Vote up 3 votes
/**
 * Calculates the amount of time until another date-time in terms of the specified unit.
 * <p>
 * This calculates the amount of time between two {@code ZonedDateTime}
 * objects in terms of a single {@code TemporalUnit}.
 * The start and end points are {@code this} and the specified date-time.
 * The result will be negative if the end is before the start.
 * For example, the amount in days between two date-times can be calculated
 * using {@code startDateTime.until(endDateTime, DAYS)}.
 * <p>
 * The {@code Temporal} passed to this method is converted to a
 * {@code ZonedDateTime} using {@link #from(TemporalAccessor)}.
 * If the time-zone differs between the two zoned date-times, the specified
 * end date-time is normalized to have the same zone as this date-time.
 * <p>
 * The calculation returns a whole number, representing the number of
 * complete units between the two date-times.
 * For example, the amount in months between 2012-06-15T00:00Z and 2012-08-14T23:59Z
 * will only be one month as it is one minute short of two months.
 * <p>
 * There are two equivalent ways of using this method.
 * The first is to invoke this method.
 * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
 * <pre>
 *   // these two lines are equivalent
 *   amount = start.until(end, MONTHS);
 *   amount = MONTHS.between(start, end);
 * </pre>
 * The choice should be made based on which makes the code more readable.
 * <p>
 * The calculation is implemented in this method for {@link ChronoUnit}.
 * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},
 * {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS}, {@code DAYS},
 * {@code WEEKS}, {@code MONTHS}, {@code YEARS}, {@code DECADES},
 * {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} are supported.
 * Other {@code ChronoUnit} values will throw an exception.
 * <p>
 * The calculation for date and time units differ.
 * <p>
 * Date units operate on the local time-line, using the local date-time.
 * For example, the period from noon on day 1 to noon the following day
 * in days will always be counted as exactly one day, irrespective of whether
 * there was a daylight savings change or not.
 * <p>
 * Time units operate on the instant time-line.
 * The calculation effectively converts both zoned date-times to instants
 * and then calculates the period between the instants.
 * For example, the period from noon on day 1 to noon the following day
 * in hours may be 23, 24 or 25 hours (or some other amount) depending on
 * whether there was a daylight savings change or not.
 * <p>
 * If the unit is not a {@code ChronoUnit}, then the result of this method
 * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
 * passing {@code this} as the first argument and the converted input temporal
 * as the second argument.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param endExclusive  the end date, exclusive, which is converted to a {@code ZonedDateTime}, not null
 * @param unit  the unit to measure the amount in, not null
 * @return the amount of time between this date-time and the end date-time
 * @throws DateTimeException if the amount cannot be calculated, or the end
 *  temporal cannot be converted to a {@code ZonedDateTime}
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
    ZonedDateTime end = ZonedDateTime.from(endExclusive);
    if (unit instanceof ChronoUnit) {
        end = end.withZoneSameInstant(zone);
        if (unit.isDateBased()) {
            return dateTime.until(end.dateTime, unit);
        } else {
            return toOffsetDateTime().until(end.toOffsetDateTime(), unit);
        }
    }
    return unit.between(this, end);
}
 
Example 6
Project: jdk8u-jdk   File: ChronoLocalDate.java   Source Code and License Vote up 3 votes
/**
 * Checks if the specified unit is supported.
 * <p>
 * This checks if the specified unit can be added to or subtracted from this date.
 * If false, then calling the {@link #plus(long, TemporalUnit)} and
 * {@link #minus(long, TemporalUnit) minus} methods will throw an exception.
 * <p>
 * The set of supported units is defined by the chronology and normally includes
 * all {@code ChronoUnit} date units except {@code FOREVER}.
 * <p>
 * If the unit is not a {@code ChronoUnit}, then the result of this method
 * is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)}
 * passing {@code this} as the argument.
 * Whether the unit is supported is determined by the unit.
 *
 * @param unit  the unit to check, null returns false
 * @return true if the unit can be added/subtracted, false if not
 */
@Override
default boolean isSupported(TemporalUnit unit) {
    if (unit instanceof ChronoUnit) {
        return unit.isDateBased();
    }
    return unit != null && unit.isSupportedBy(this);
}
 
Example 7
Project: openjdk-jdk10   File: ZonedDateTime.java   Source Code and License Vote up 3 votes
/**
 * Returns a copy of this date-time with the specified amount added.
 * <p>
 * This returns a {@code ZonedDateTime}, based on this one, with the amount
 * in terms of the unit added. If it is not possible to add the amount, because the
 * unit is not supported or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoUnit} then the addition is implemented here.
 * The zone is not part of the calculation and will be unchanged in the result.
 * The calculation for date and time units differ.
 * <p>
 * Date units operate on the local time-line.
 * The period is first added to the local date-time, then converted back
 * to a zoned date-time using the zone ID.
 * The conversion uses {@link #ofLocal(LocalDateTime, ZoneId, ZoneOffset)}
 * with the offset before the addition.
 * <p>
 * Time units operate on the instant time-line.
 * The period is first added to the local date-time, then converted back to
 * a zoned date-time using the zone ID.
 * The conversion uses {@link #ofInstant(LocalDateTime, ZoneOffset, ZoneId)}
 * with the offset before the addition.
 * <p>
 * If the field is not a {@code ChronoUnit}, then the result of this method
 * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
 * passing {@code this} as the argument. In this case, the unit determines
 * whether and how to perform the addition.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param amountToAdd  the amount of the unit to add to the result, may be negative
 * @param unit  the unit of the amount to add, not null
 * @return a {@code ZonedDateTime} based on this date-time with the specified amount added, not null
 * @throws DateTimeException if the addition cannot be made
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public ZonedDateTime plus(long amountToAdd, TemporalUnit unit) {
    if (unit instanceof ChronoUnit) {
        if (unit.isDateBased()) {
            return resolveLocal(dateTime.plus(amountToAdd, unit));
        } else {
            return resolveInstant(dateTime.plus(amountToAdd, unit));
        }
    }
    return unit.addTo(this, amountToAdd);
}
 
Example 8
Project: openjdk-jdk10   File: ZonedDateTime.java   Source Code and License Vote up 3 votes
/**
 * Calculates the amount of time until another date-time in terms of the specified unit.
 * <p>
 * This calculates the amount of time between two {@code ZonedDateTime}
 * objects in terms of a single {@code TemporalUnit}.
 * The start and end points are {@code this} and the specified date-time.
 * The result will be negative if the end is before the start.
 * For example, the amount in days between two date-times can be calculated
 * using {@code startDateTime.until(endDateTime, DAYS)}.
 * <p>
 * The {@code Temporal} passed to this method is converted to a
 * {@code ZonedDateTime} using {@link #from(TemporalAccessor)}.
 * If the time-zone differs between the two zoned date-times, the specified
 * end date-time is normalized to have the same zone as this date-time.
 * <p>
 * The calculation returns a whole number, representing the number of
 * complete units between the two date-times.
 * For example, the amount in months between 2012-06-15T00:00Z and 2012-08-14T23:59Z
 * will only be one month as it is one minute short of two months.
 * <p>
 * There are two equivalent ways of using this method.
 * The first is to invoke this method.
 * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
 * <pre>
 *   // these two lines are equivalent
 *   amount = start.until(end, MONTHS);
 *   amount = MONTHS.between(start, end);
 * </pre>
 * The choice should be made based on which makes the code more readable.
 * <p>
 * The calculation is implemented in this method for {@link ChronoUnit}.
 * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},
 * {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS}, {@code DAYS},
 * {@code WEEKS}, {@code MONTHS}, {@code YEARS}, {@code DECADES},
 * {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} are supported.
 * Other {@code ChronoUnit} values will throw an exception.
 * <p>
 * The calculation for date and time units differ.
 * <p>
 * Date units operate on the local time-line, using the local date-time.
 * For example, the period from noon on day 1 to noon the following day
 * in days will always be counted as exactly one day, irrespective of whether
 * there was a daylight savings change or not.
 * <p>
 * Time units operate on the instant time-line.
 * The calculation effectively converts both zoned date-times to instants
 * and then calculates the period between the instants.
 * For example, the period from noon on day 1 to noon the following day
 * in hours may be 23, 24 or 25 hours (or some other amount) depending on
 * whether there was a daylight savings change or not.
 * <p>
 * If the unit is not a {@code ChronoUnit}, then the result of this method
 * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
 * passing {@code this} as the first argument and the converted input temporal
 * as the second argument.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param endExclusive  the end date, exclusive, which is converted to a {@code ZonedDateTime}, not null
 * @param unit  the unit to measure the amount in, not null
 * @return the amount of time between this date-time and the end date-time
 * @throws DateTimeException if the amount cannot be calculated, or the end
 *  temporal cannot be converted to a {@code ZonedDateTime}
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
    ZonedDateTime end = ZonedDateTime.from(endExclusive);
    if (unit instanceof ChronoUnit) {
        end = end.withZoneSameInstant(zone);
        if (unit.isDateBased()) {
            return dateTime.until(end.dateTime, unit);
        } else {
            return toOffsetDateTime().until(end.toOffsetDateTime(), unit);
        }
    }
    return unit.between(this, end);
}
 
Example 9
Project: openjdk-jdk10   File: ChronoLocalDate.java   Source Code and License Vote up 3 votes
/**
 * Checks if the specified unit is supported.
 * <p>
 * This checks if the specified unit can be added to or subtracted from this date.
 * If false, then calling the {@link #plus(long, TemporalUnit)} and
 * {@link #minus(long, TemporalUnit) minus} methods will throw an exception.
 * <p>
 * The set of supported units is defined by the chronology and normally includes
 * all {@code ChronoUnit} date units except {@code FOREVER}.
 * <p>
 * If the unit is not a {@code ChronoUnit}, then the result of this method
 * is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)}
 * passing {@code this} as the argument.
 * Whether the unit is supported is determined by the unit.
 *
 * @param unit  the unit to check, null returns false
 * @return true if the unit can be added/subtracted, false if not
 */
@Override
default boolean isSupported(TemporalUnit unit) {
    if (unit instanceof ChronoUnit) {
        return unit.isDateBased();
    }
    return unit != null && unit.isSupportedBy(this);
}