类java.time.temporal.TemporalUnit源码实例Demo

下面列出了怎么用java.time.temporal.TemporalUnit的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: TencentKona-8   文件: TCKYear.java
@Test
public void test_isSupported_TemporalUnit() {
    assertEquals(TEST_2008.isSupported((TemporalUnit) null), false);
    assertEquals(TEST_2008.isSupported(ChronoUnit.NANOS), false);
    assertEquals(TEST_2008.isSupported(ChronoUnit.MICROS), false);
    assertEquals(TEST_2008.isSupported(ChronoUnit.MILLIS), false);
    assertEquals(TEST_2008.isSupported(ChronoUnit.SECONDS), false);
    assertEquals(TEST_2008.isSupported(ChronoUnit.MINUTES), false);
    assertEquals(TEST_2008.isSupported(ChronoUnit.HOURS), false);
    assertEquals(TEST_2008.isSupported(ChronoUnit.HALF_DAYS), false);
    assertEquals(TEST_2008.isSupported(ChronoUnit.DAYS), false);
    assertEquals(TEST_2008.isSupported(ChronoUnit.WEEKS), false);
    assertEquals(TEST_2008.isSupported(ChronoUnit.MONTHS), false);
    assertEquals(TEST_2008.isSupported(ChronoUnit.YEARS), true);
    assertEquals(TEST_2008.isSupported(ChronoUnit.DECADES), true);
    assertEquals(TEST_2008.isSupported(ChronoUnit.CENTURIES), true);
    assertEquals(TEST_2008.isSupported(ChronoUnit.MILLENNIA), true);
    assertEquals(TEST_2008.isSupported(ChronoUnit.ERAS), true);
    assertEquals(TEST_2008.isSupported(ChronoUnit.FOREVER), false);
}
 
源代码2 项目: jdk8u-jdk   文件: Duration.java
/**
 * Returns a copy of this duration with the specified duration added.
 * <p>
 * The duration amount is measured in terms of the specified unit.
 * Only a subset of units are accepted by this method.
 * The unit must either have an {@linkplain TemporalUnit#isDurationEstimated() exact duration} or
 * be {@link ChronoUnit#DAYS} which is treated as 24 hours. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param amountToAdd  the amount to add, measured in terms of the unit, positive or negative
 * @param unit  the unit that the amount is measured in, must have an exact duration, not null
 * @return a {@code Duration} based on this duration with the specified duration added, not null
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
public Duration plus(long amountToAdd, TemporalUnit unit) {
    Objects.requireNonNull(unit, "unit");
    if (unit == DAYS) {
        return plus(Math.multiplyExact(amountToAdd, SECONDS_PER_DAY), 0);
    }
    if (unit.isDurationEstimated()) {
        throw new UnsupportedTemporalTypeException("Unit must not have an estimated duration");
    }
    if (amountToAdd == 0) {
        return this;
    }
    if (unit instanceof ChronoUnit) {
        switch ((ChronoUnit) unit) {
            case NANOS: return plusNanos(amountToAdd);
            case MICROS: return plusSeconds((amountToAdd / (1000_000L * 1000)) * 1000).plusNanos((amountToAdd % (1000_000L * 1000)) * 1000);
            case MILLIS: return plusMillis(amountToAdd);
            case SECONDS: return plusSeconds(amountToAdd);
        }
        return plusSeconds(Math.multiplyExact(unit.getDuration().seconds, amountToAdd));
    }
    Duration duration = unit.getDuration().multipliedBy(amountToAdd);
    return plusSeconds(duration.getSeconds()).plusNanos(duration.getNano());
}
 
@Override
public ChronoLocalDateTimeImpl<D> plus(long amountToAdd, TemporalUnit unit) {
    if (unit instanceof ChronoUnit) {
        ChronoUnit f = (ChronoUnit) unit;
        switch (f) {
            case NANOS: return plusNanos(amountToAdd);
            case MICROS: return plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
            case MILLIS: return plusDays(amountToAdd / MILLIS_PER_DAY).plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000000);
            case SECONDS: return plusSeconds(amountToAdd);
            case MINUTES: return plusMinutes(amountToAdd);
            case HOURS: return plusHours(amountToAdd);
            case HALF_DAYS: return plusDays(amountToAdd / 256).plusHours((amountToAdd % 256) * 12);  // no overflow (256 is multiple of 2)
        }
        return with(date.plus(amountToAdd, unit), time);
    }
    return ChronoLocalDateTimeImpl.ensureValid(date.getChronology(), unit.addTo(this, amountToAdd));
}
 
源代码4 项目: jdk8u60   文件: ChronoLocalDateTimeImpl.java
@Override
public ChronoLocalDateTimeImpl<D> plus(long amountToAdd, TemporalUnit unit) {
    if (unit instanceof ChronoUnit) {
        ChronoUnit f = (ChronoUnit) unit;
        switch (f) {
            case NANOS: return plusNanos(amountToAdd);
            case MICROS: return plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
            case MILLIS: return plusDays(amountToAdd / MILLIS_PER_DAY).plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000000);
            case SECONDS: return plusSeconds(amountToAdd);
            case MINUTES: return plusMinutes(amountToAdd);
            case HOURS: return plusHours(amountToAdd);
            case HALF_DAYS: return plusDays(amountToAdd / 256).plusHours((amountToAdd % 256) * 12);  // no overflow (256 is multiple of 2)
        }
        return with(date.plus(amountToAdd, unit), time);
    }
    return ChronoLocalDateTimeImpl.ensureValid(date.getChronology(), unit.addTo(this, amountToAdd));
}
 
源代码5 项目: jdk8u_jdk   文件: TCKChronoLocalDate.java
@Test(dataProvider="calendars")
public void test_badMinusTemporalUnitChrono(Chronology chrono) {
    LocalDate refDate = LocalDate.of(2013, 1, 1);
    ChronoLocalDate date = chrono.date(refDate);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoLocalDate date2 = chrono2.date(refDate);
        TemporalUnit adjuster = new FixedTemporalUnit(date2);
        if (chrono != chrono2) {
            try {
                date.minus(1, adjuster);
                Assert.fail("TemporalUnit.doAdd minus should have thrown a ClassCastException" + date.getClass()
                        + ", can not be cast to " + date2.getClass());
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoLocalDate result = date.minus(1, adjuster);
            assertEquals(result, date2, "WithAdjuster failed to replace date");
        }
    }
}
 
源代码6 项目: hottub   文件: ChronoLocalDate.java
/**
 * {@inheritDoc}
 * @throws DateTimeException {@inheritDoc}
 * @throws ArithmeticException {@inheritDoc}
 */
@Override
default ChronoLocalDate plus(long amountToAdd, TemporalUnit unit) {
    if (unit instanceof ChronoUnit) {
        throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
    }
    return ChronoLocalDateImpl.ensureValid(getChronology(), unit.addTo(this, amountToAdd));
}
 
源代码7 项目: jdk8u_jdk   文件: CopticDate.java
@Override
public long until(Temporal endExclusive, TemporalUnit unit) {
    CopticDate end = getChronology().date(endExclusive);
    if (unit instanceof ChronoUnit) {
        return LocalDate.from(this).until(end, unit);  // TODO: this is wrong
    }
    return unit.between(this, end);
}
 
源代码8 项目: jdk8u_jdk   文件: ChronoPeriod.java
/**
 * Checks if any of the supported units of this period are negative.
 *
 * @return true if any unit of this period is negative
 */
default boolean isNegative() {
    for (TemporalUnit unit : getUnits()) {
        if (get(unit) < 0) {
            return true;
        }
    }
    return false;
}
 
源代码9 项目: jdk8u-jdk   文件: ChronoPeriodImpl.java
@Override
public long get(TemporalUnit unit) {
    if (unit == ChronoUnit.YEARS) {
        return years;
    } else if (unit == ChronoUnit.MONTHS) {
        return months;
    } else if (unit == ChronoUnit.DAYS) {
        return days;
    } else {
        throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
    }
}
 
源代码10 项目: jdk8u60   文件: TCKDuration.java
@Test(groups="{tck}")
public void test_duration_getUnits() {
    Duration duration = Duration.ofSeconds(5000, 1000);
    List<TemporalUnit> units = duration.getUnits();
    assertEquals(units.size(), 2, "Period.getUnits length");
    assertTrue(units.contains(ChronoUnit.SECONDS), "Period.getUnits contains ChronoUnit.SECONDS");
    assertTrue(units.contains(ChronoUnit.NANOS), "contains ChronoUnit.NANOS");
}
 
源代码11 项目: jdk8u60   文件: TCKInstant.java
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit_negated(long seconds1, int nanos1, long seconds2, long nanos2, TemporalUnit unit, long expected) {
    Instant i1 = Instant.ofEpochSecond(seconds1, nanos1);
    Instant i2 = Instant.ofEpochSecond(seconds2, nanos2);
    long amount = i2.until(i1, unit);
    assertEquals(amount, -expected);
}
 
源代码12 项目: openjdk-jdk9   文件: MockSimplePeriod.java
private MockSimplePeriod(long amount, TemporalUnit unit) {
    Objects.requireNonNull(unit, "unit");
    if (unit == FOREVER) {
        throw new DateTimeException("Cannot create a period of the Forever unit");
    }
    this.amount = amount;
    this.unit = unit;
}
 
源代码13 项目: jdk8u60   文件: ChronoLocalDate.java
/**
 * {@inheritDoc}
 * @throws DateTimeException {@inheritDoc}
 * @throws ArithmeticException {@inheritDoc}
 */
@Override
default ChronoLocalDate plus(long amountToAdd, TemporalUnit unit) {
    if (unit instanceof ChronoUnit) {
        throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
    }
    return ChronoLocalDateImpl.ensureValid(getChronology(), unit.addTo(this, amountToAdd));
}
 
源代码14 项目: TencentKona-8   文件: ChronoPeriodImpl.java
@Override
public long get(TemporalUnit unit) {
    if (unit == ChronoUnit.YEARS) {
        return years;
    } else if (unit == ChronoUnit.MONTHS) {
        return months;
    } else if (unit == ChronoUnit.DAYS) {
        return days;
    } else {
        throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
    }
}
 
源代码15 项目: TencentKona-8   文件: TCKInstant.java
@Test(dataProvider="plusTemporalAmount")
public void test_plusTemporalAmount(TemporalUnit unit, TemporalAmount amount, int seconds, int nanos) {
    Instant inst = Instant.ofEpochMilli(1000);
    Instant actual = inst.plus(amount);
    Instant expected = Instant.ofEpochSecond(seconds, nanos);
    assertEquals(actual, expected, "plus(TemporalAmount) failed");
}
 
源代码16 项目: jdk8u_jdk   文件: TCKInstant.java
@Test(dataProvider="plusTemporalAmount")
public void test_plusTemporalAmount(TemporalUnit unit, TemporalAmount amount, int seconds, int nanos) {
    Instant inst = Instant.ofEpochMilli(1000);
    Instant actual = inst.plus(amount);
    Instant expected = Instant.ofEpochSecond(seconds, nanos);
    assertEquals(actual, expected, "plus(TemporalAmount) failed");
}
 
源代码17 项目: openjdk-jdk8u   文件: TCKInstant.java
@Test(dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class)
public void test_truncatedTo_invalid(Instant input, TemporalUnit unit) {
    input.truncatedTo(unit);
}
 
源代码18 项目: gama   文件: GamaDate.java
@Override
public long until(final Temporal endExclusive, final TemporalUnit unit) {
	return unit.between(internal, endExclusive);
}
 
源代码19 项目: jdk8u-dev-jdk   文件: TCKYearMonth.java
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit_between(YearMonth ym1, YearMonth ym2, TemporalUnit unit, long expected) {
    long amount = unit.between(ym1, ym2);
    assertEquals(amount, expected);
}
 
源代码20 项目: openjdk-jdk8u   文件: TCKChronoLocalDate.java
@Override
public TemporalUnit getRangeUnit() {
    throw new UnsupportedOperationException("Not supported yet.");
}
 
源代码21 项目: openjdk-8-source   文件: TCKYear.java
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit(Year year1, Year year2, TemporalUnit unit, long expected) {
    long amount = year1.until(year2, unit);
    assertEquals(amount, expected);
}
 
源代码22 项目: hottub   文件: TCKDuration.java
@Test(expectedExceptions=NullPointerException.class)
public void plus_longTemporalUnit_null() {
   Duration t = Duration.ofSeconds(1);
   t.plus(1, (TemporalUnit) null);
}
 
源代码23 项目: TencentKona-8   文件: TCKOffsetTime.java
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit(OffsetTime offsetTime1, OffsetTime offsetTime2, TemporalUnit unit, long expected) {
    long amount = offsetTime1.until(offsetTime2, unit);
    assertEquals(amount, expected);
}
 
源代码24 项目: TencentKona-8   文件: TCKDuration.java
@Test(expectedExceptions=NullPointerException.class)
public void plus_longTemporalUnit_null() {
   Duration t = Duration.ofSeconds(1);
   t.plus(1, (TemporalUnit) null);
}
 
源代码25 项目: openjdk-jdk8u-backup   文件: TCKInstant.java
@Test(dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class)
public void test_truncatedTo_invalid(Instant input, TemporalUnit unit) {
    input.truncatedTo(unit);
}
 
源代码26 项目: dragonwell8_jdk   文件: TCKYear.java
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit(Year year1, Year year2, TemporalUnit unit, long expected) {
    long amount = year1.until(year2, unit);
    assertEquals(amount, expected);
}
 
源代码27 项目: Bytecoder   文件: LocalDateTime.java
/**
 * Returns a copy of this date-time with the specified amount added.
 * <p>
 * This returns a {@code LocalDateTime}, 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.
 * Date units are added as per {@link LocalDate#plus(long, TemporalUnit)}.
 * Time units are added as per {@link LocalTime#plus(long, TemporalUnit)} with
 * any overflow in days added equivalent to using {@link #plusDays(long)}.
 * <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 LocalDateTime} 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 LocalDateTime plus(long amountToAdd, TemporalUnit unit) {
    if (unit instanceof ChronoUnit) {
        ChronoUnit f = (ChronoUnit) unit;
        switch (f) {
            case NANOS: return plusNanos(amountToAdd);
            case MICROS: return plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
            case MILLIS: return plusDays(amountToAdd / MILLIS_PER_DAY).plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000_000);
            case SECONDS: return plusSeconds(amountToAdd);
            case MINUTES: return plusMinutes(amountToAdd);
            case HOURS: return plusHours(amountToAdd);
            case HALF_DAYS: return plusDays(amountToAdd / 256).plusHours((amountToAdd % 256) * 12);  // no overflow (256 is multiple of 2)
        }
        return with(date.plus(amountToAdd, unit), time);
    }
    return unit.addTo(this, amountToAdd);
}
 
源代码28 项目: hottub   文件: TCKChronoZonedDateTime.java
@Override
public TemporalUnit getBaseUnit() {
    throw new UnsupportedOperationException("Not supported yet.");
}
 
源代码29 项目: openjdk-8   文件: LocalDate.java
/**
 * Returns a copy of this date with the specified amount added.
 * <p>
 * This returns a {@code LocalDate}, 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>
 * In some cases, adding the amount can cause the resulting date to become invalid.
 * For example, adding one month to 31st January would result in 31st February.
 * In cases like this, the unit is responsible for resolving the date.
 * Typically it will choose the previous valid date, which would be the last valid
 * day of February in this example.
 * <p>
 * If the field is a {@link ChronoUnit} then the addition is implemented here.
 * The supported fields behave as follows:
 * <ul>
 * <li>{@code DAYS} -
 *  Returns a {@code LocalDate} with the specified number of days added.
 *  This is equivalent to {@link #plusDays(long)}.
 * <li>{@code WEEKS} -
 *  Returns a {@code LocalDate} with the specified number of weeks added.
 *  This is equivalent to {@link #plusWeeks(long)} and uses a 7 day week.
 * <li>{@code MONTHS} -
 *  Returns a {@code LocalDate} with the specified number of months added.
 *  This is equivalent to {@link #plusMonths(long)}.
 *  The day-of-month will be unchanged unless it would be invalid for the new
 *  month and year. In that case, the day-of-month is adjusted to the maximum
 *  valid value for the new month and year.
 * <li>{@code YEARS} -
 *  Returns a {@code LocalDate} with the specified number of years added.
 *  This is equivalent to {@link #plusYears(long)}.
 *  The day-of-month will be unchanged unless it would be invalid for the new
 *  month and year. In that case, the day-of-month is adjusted to the maximum
 *  valid value for the new month and year.
 * <li>{@code DECADES} -
 *  Returns a {@code LocalDate} with the specified number of decades added.
 *  This is equivalent to calling {@link #plusYears(long)} with the amount
 *  multiplied by 10.
 *  The day-of-month will be unchanged unless it would be invalid for the new
 *  month and year. In that case, the day-of-month is adjusted to the maximum
 *  valid value for the new month and year.
 * <li>{@code CENTURIES} -
 *  Returns a {@code LocalDate} with the specified number of centuries added.
 *  This is equivalent to calling {@link #plusYears(long)} with the amount
 *  multiplied by 100.
 *  The day-of-month will be unchanged unless it would be invalid for the new
 *  month and year. In that case, the day-of-month is adjusted to the maximum
 *  valid value for the new month and year.
 * <li>{@code MILLENNIA} -
 *  Returns a {@code LocalDate} with the specified number of millennia added.
 *  This is equivalent to calling {@link #plusYears(long)} with the amount
 *  multiplied by 1,000.
 *  The day-of-month will be unchanged unless it would be invalid for the new
 *  month and year. In that case, the day-of-month is adjusted to the maximum
 *  valid value for the new month and year.
 * <li>{@code ERAS} -
 *  Returns a {@code LocalDate} with the specified number of eras added.
 *  Only two eras are supported so the amount must be one, zero or minus one.
 *  If the amount is non-zero then the year is changed such that the year-of-era
 *  is unchanged.
 *  The day-of-month will be unchanged unless it would be invalid for the new
 *  month and year. In that case, the day-of-month is adjusted to the maximum
 *  valid value for the new month and year.
 * </ul>
 * <p>
 * All other {@code ChronoUnit} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <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 LocalDate} based on this date 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 LocalDate plus(long amountToAdd, TemporalUnit unit) {
    if (unit instanceof ChronoUnit) {
        ChronoUnit f = (ChronoUnit) unit;
        switch (f) {
            case DAYS: return plusDays(amountToAdd);
            case WEEKS: return plusWeeks(amountToAdd);
            case MONTHS: return plusMonths(amountToAdd);
            case YEARS: return plusYears(amountToAdd);
            case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10));
            case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100));
            case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000));
            case ERAS: return with(ERA, Math.addExact(getLong(ERA), amountToAdd));
        }
        throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
    }
    return unit.addTo(this, amountToAdd);
}
 
源代码30 项目: openjdk-jdk8u-backup   文件: ThaiBuddhistDate.java
@Override
public ThaiBuddhistDate minus(long amountToAdd, TemporalUnit unit) {
    return super.minus(amountToAdd, unit);
}
 
 类所在包
 同包方法