Java Code Examples for java.time.temporal.TemporalUnit#isDateBased()
The following examples show how to use
java.time.temporal.TemporalUnit#isDateBased() .
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: ZonedDateTime.java From Bytecoder with Apache License 2.0 | 4 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) { ZonedDateTime start = this; try { end = end.withZoneSameInstant(zone); } catch (DateTimeException ex) { // end may be out of valid range. Adjust to end's zone. start = withZoneSameInstant(end.zone); } if (unit.isDateBased()) { return start.dateTime.until(end.dateTime, unit); } else { return start.toOffsetDateTime().until(end.toOffsetDateTime(), unit); } } return unit.between(this, end); }
Example 2
Source File: ChronoLocalDate.java From openjdk-8 with GNU General Public License v2.0 | 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 3
Source File: ZonedDateTime.java From jdk8u-jdk with GNU General Public License v2.0 | 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 4
Source File: ChronoLocalDate.java From jdk8u_jdk with GNU General Public License v2.0 | 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 5
Source File: ChronoLocalDate.java From desugar_jdk_libs with GNU General Public License v2.0 | 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 6
Source File: ZonedDateTime.java From j2objc with Apache License 2.0 | 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 7
Source File: ZonedDateTime.java From openjdk-jdk9 with GNU General Public License v2.0 | 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
Source File: ZonedDateTime.java From openjdk-8 with GNU General Public License v2.0 | 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 9
Source File: ZonedDateTime.java From Java8CN with Apache License 2.0 | 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 10
Source File: ZonedDateTime.java From jdk8u_jdk with GNU General Public License v2.0 | 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 11
Source File: ZonedDateTime.java From jdk8u-jdk with GNU General Public License v2.0 | 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 12
Source File: ZonedDateTime.java From openjdk-8 with GNU General Public License v2.0 | 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 13
Source File: ZonedDateTime.java From openjdk-8-source with GNU General Public License v2.0 | 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 14
Source File: ZonedDateTime.java From Java8CN with Apache License 2.0 | 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 15
Source File: ChronoLocalDate.java From j2objc with Apache License 2.0 | 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 16
Source File: ChronoLocalDate.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 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 17
Source File: ChronoLocalDate.java From Bytecoder with Apache License 2.0 | 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 18
Source File: ZonedDateTime.java From dragonwell8_jdk with GNU General Public License v2.0 | 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 19
Source File: ChronoLocalDate.java From jdk1.8-source-analysis with Apache License 2.0 | 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 20
Source File: ZonedDateTime.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 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); }