Java 类java.time.temporal.TemporalAmount 实例源码

项目:jdk8u-jdk    文件:TCKChronoZonedDateTime.java   
@Test(dataProvider="calendars")
public void test_badPlusAdjusterChrono(Chronology chrono) {
    LocalDate refDate = LocalDate.of(2013, 1, 1);
    ChronoZonedDateTime<?> czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
        TemporalAmount adjuster = new FixedAdjuster(czdt2);
        if (chrono != chrono2) {
            try {
                czdt.plus(adjuster);
                Assert.fail("WithAdjuster should have thrown a ClassCastException, "
                        + "required: " + czdt + ", supplied: " + czdt2);
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoZonedDateTime<?> result = czdt.plus(adjuster);
            assertEquals(result, czdt2, "WithAdjuster failed to replace date time");
        }
    }
}
项目:jdk8u-jdk    文件:TCKChronoLocalDate.java   
@Test(dataProvider="calendars")
public void test_badPlusAdjusterChrono(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);
        TemporalAmount adjuster = new FixedAdjuster(date2);
        if (chrono != chrono2) {
            try {
                date.plus(adjuster);
                Assert.fail("WithAdjuster should have thrown a ClassCastException");
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoLocalDate result = date.plus(adjuster);
            assertEquals(result, date2, "WithAdjuster failed to replace date");
        }
    }
}
项目:openjdk-jdk10    文件:TCKChronoZonedDateTime.java   
@Test(dataProvider="calendars")
public void test_badPlusAdjusterChrono(Chronology chrono) {
    LocalDate refDate = LocalDate.of(2013, 1, 1);
    ChronoZonedDateTime<?> czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
        TemporalAmount adjuster = new FixedAdjuster(czdt2);
        if (chrono != chrono2) {
            try {
                czdt.plus(adjuster);
                Assert.fail("WithAdjuster should have thrown a ClassCastException, "
                        + "required: " + czdt + ", supplied: " + czdt2);
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoZonedDateTime<?> result = czdt.plus(adjuster);
            assertEquals(result, czdt2, "WithAdjuster failed to replace date time");
        }
    }
}
项目:openjdk-jdk10    文件:TCKChronoZonedDateTime.java   
@Test(dataProvider="calendars")
public void test_badMinusAdjusterChrono(Chronology chrono) {
    LocalDate refDate = LocalDate.of(2013, 1, 1);
    ChronoZonedDateTime<?> czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
        TemporalAmount adjuster = new FixedAdjuster(czdt2);
        if (chrono != chrono2) {
            try {
                czdt.minus(adjuster);
                Assert.fail("WithAdjuster should have thrown a ClassCastException, "
                        + "required: " + czdt + ", supplied: " + czdt2);
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoZonedDateTime<?> result = czdt.minus(adjuster);
            assertEquals(result, czdt2, "WithAdjuster failed to replace date");
        }
    }
}
项目:jdk8u-jdk    文件:TCKChronoLocalDateTime.java   
@Test(dataProvider="calendars")
public void test_badPlusAdjusterChrono(Chronology chrono) {
    LocalDate refDate = LocalDate.of(2013, 1, 1);
    ChronoLocalDateTime<?> cdt = chrono.date(refDate).atTime(LocalTime.NOON);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON);
        TemporalAmount adjuster = new FixedAdjuster(cdt2);
        if (chrono != chrono2) {
            try {
                cdt.plus(adjuster);
                Assert.fail("WithAdjuster should have thrown a ClassCastException, "
                        + "required: " + cdt + ", supplied: " + cdt2);
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoLocalDateTime<?> result = cdt.plus(adjuster);
            assertEquals(result, cdt2, "WithAdjuster failed to replace date time");
        }
    }
}
项目:openjdk-jdk10    文件:TCKYearMonth.java   
@Test(dataProvider="plus_TemporalAmount")
public void test_plus_TemporalAmount(YearMonth base, TemporalAmount temporalAmount, YearMonth expectedYearMonth, Class<?> expectedEx) {
    if (expectedEx == null) {
        assertEquals(base.plus(temporalAmount), expectedYearMonth);
    } else {
        try {
            YearMonth result = base.plus(temporalAmount);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
项目:openjdk-jdk10    文件:ChronoPeriodImpl.java   
/**
 * Obtains an instance of {@code ChronoPeriodImpl} from a temporal amount.
 *
 * @param amount  the temporal amount to convert, not null
 * @return the period, not null
 */
private ChronoPeriodImpl validateAmount(TemporalAmount amount) {
    Objects.requireNonNull(amount, "amount");
    if (amount instanceof ChronoPeriodImpl == false) {
        throw new DateTimeException("Unable to obtain ChronoPeriod from TemporalAmount: " + amount.getClass());
    }
    ChronoPeriodImpl period = (ChronoPeriodImpl) amount;
    if (chrono.equals(period.getChronology()) == false) {
        throw new ClassCastException("Chronology mismatch, expected: " + chrono.getId() + ", actual: " + period.getChronology().getId());
    }
    return period;
}
项目:OpenJSharp    文件:Period.java   
/**
 * Obtains an instance of {@code Period} from a temporal amount.
 * <p>
 * This obtains a period based on the specified amount.
 * A {@code TemporalAmount} represents an  amount of time, which may be
 * date-based or time-based, which this factory extracts to a {@code Period}.
 * <p>
 * The conversion loops around the set of units from the amount and uses
 * the {@link ChronoUnit#YEARS YEARS}, {@link ChronoUnit#MONTHS MONTHS}
 * and {@link ChronoUnit#DAYS DAYS} units to create a period.
 * If any other units are found then an exception is thrown.
 * <p>
 * If the amount is a {@code ChronoPeriod} then it must use the ISO chronology.
 *
 * @param amount  the temporal amount to convert, not null
 * @return the equivalent period, not null
 * @throws DateTimeException if unable to convert to a {@code Period}
 * @throws ArithmeticException if the amount of years, months or days exceeds an int
 */
public static Period from(TemporalAmount amount) {
    if (amount instanceof Period) {
        return (Period) amount;
    }
    if (amount instanceof ChronoPeriod) {
        if (IsoChronology.INSTANCE.equals(((ChronoPeriod) amount).getChronology()) == false) {
            throw new DateTimeException("Period requires ISO chronology: " + amount);
        }
    }
    Objects.requireNonNull(amount, "amount");
    int years = 0;
    int months = 0;
    int days = 0;
    for (TemporalUnit unit : amount.getUnits()) {
        long unitAmount = amount.get(unit);
        if (unit == ChronoUnit.YEARS) {
            years = Math.toIntExact(unitAmount);
        } else if (unit == ChronoUnit.MONTHS) {
            months = Math.toIntExact(unitAmount);
        } else if (unit == ChronoUnit.DAYS) {
            days = Math.toIntExact(unitAmount);
        } else {
            throw new DateTimeException("Unit must be Years, Months or Days, but was " + unit);
        }
    }
    return create(years, months, days);
}
项目: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");
}
项目:openjdk-jdk10    文件:ChronoPeriodImpl.java   
@Override
public ChronoPeriod plus(TemporalAmount amountToAdd) {
    ChronoPeriodImpl amount = validateAmount(amountToAdd);
    return new ChronoPeriodImpl(
            chrono,
            Math.addExact(years, amount.years),
            Math.addExact(months, amount.months),
            Math.addExact(days, amount.days));
}
项目:OpenJSharp    文件:ChronoPeriodImpl.java   
@Override
public ChronoPeriod plus(TemporalAmount amountToAdd) {
    ChronoPeriodImpl amount = validateAmount(amountToAdd);
    return new ChronoPeriodImpl(
            chrono,
            Math.addExact(years, amount.years),
            Math.addExact(months, amount.months),
            Math.addExact(days, amount.days));
}
项目:jdk8u-jdk    文件:ChronoPeriodImpl.java   
@Override
public ChronoPeriod plus(TemporalAmount amountToAdd) {
    ChronoPeriodImpl amount = validateAmount(amountToAdd);
    return new ChronoPeriodImpl(
            chrono,
            Math.addExact(years, amount.years),
            Math.addExact(months, amount.months),
            Math.addExact(days, amount.days));
}
项目:jdk8u-jdk    文件:ChronoPeriodImpl.java   
@Override
public ChronoPeriod minus(TemporalAmount amountToSubtract) {
    ChronoPeriodImpl amount = validateAmount(amountToSubtract);
    return new ChronoPeriodImpl(
            chrono,
            Math.subtractExact(years, amount.years),
            Math.subtractExact(months, amount.months),
            Math.subtractExact(days, amount.days));
}
项目:jdk8u-jdk    文件:TCKYearMonth.java   
@Test(dataProvider="plus_TemporalAmount")
public void test_plus_TemporalAmount(YearMonth base, TemporalAmount temporalAmount, YearMonth expectedYearMonth, Class<?> expectedEx) {
    if (expectedEx == null) {
        assertEquals(base.plus(temporalAmount), expectedYearMonth);
    } else {
        try {
            YearMonth result = base.plus(temporalAmount);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
项目:fernet-java8    文件:ProtocolBuffersExampleIT.java   
public TemporalAmount getTimeToLive() {
    return Duration.ofMinutes(5);
}
项目:openjdk-jdk10    文件:TCKLocalTime.java   
@Test
public void test_plus_TemporalAmount_wrap() {
    TemporalAmount p = MockSimplePeriod.of(1, HOURS);
    LocalTime t = LocalTime.of(23, 30).plus(p);
    assertEquals(t, LocalTime.of(0, 30));
}
项目:ProjectAres    文件:TimeUtils.java   
public static long getUnitOrDefault(TemporalAmount period, TemporalUnit unit, long def) {
    return period.getUnits().contains(unit) ? period.get(unit) : def;
}
项目:openjdk-jdk10    文件:TCKZonedDateTime.java   
@Test(expectedExceptions=NullPointerException.class)
public void test_minus_TemporalAmount_null() {
    TEST_DATE_TIME.minus((TemporalAmount) null);
}
项目:jdk8u-jdk    文件:TCKLocalTime.java   
@Test
public void test_minus_TemporalAmount_zero() {
    TemporalAmount period = Period.ZERO;
    LocalTime t = TEST_12_30_40_987654321.minus(period);
    assertEquals(t, TEST_12_30_40_987654321);
}
项目:openjdk-jdk10    文件:TCKLocalTime.java   
@Test(expectedExceptions=NullPointerException.class)
public void test_minus_TemporalAmount_null() {
    TEST_12_30_40_987654321.minus((TemporalAmount) null);
}
项目:openjdk-jdk10    文件:TCKYear.java   
@Test(dataProvider="minusValid")
public void test_minusValid(int year, TemporalAmount amount, int expected) {
    assertEquals(Year.of(year).minus(amount), Year.of(expected));
}
项目:openjdk-jdk10    文件:TCKLocalTime.java   
@Test(expectedExceptions=DateTimeException.class)
public void test_plus_TemporalAmount_dateNotAllowed() {
    TemporalAmount period = MockSimplePeriod.of(7, ChronoUnit.MONTHS);
    TEST_12_30_40_987654321.plus(period);
}
项目:jdk8u-jdk    文件:TCKLocalTime.java   
@Test
public void test_minus_TemporalAmount_positiveHours() {
    TemporalAmount period = MockSimplePeriod.of(7, ChronoUnit.HOURS);
    LocalTime t = TEST_12_30_40_987654321.minus(period);
    assertEquals(t, LocalTime.of(5, 30, 40, 987654321));
}
项目:openjdk-jdk10    文件:TCKLocalTime.java   
@Test
public void test_minus_TemporalAmount_wrap() {
    TemporalAmount p = MockSimplePeriod.of(1, HOURS);
    LocalTime t = LocalTime.of(0, 30).minus(p);
    assertEquals(t, LocalTime.of(23, 30));
}
项目:jdk8u-jdk    文件:TCKLocalTime.java   
@Test(expectedExceptions=NullPointerException.class)
public void test_plus_TemporalAmount_null() {
    TEST_12_30_40_987654321.plus((TemporalAmount) null);
}
项目:openjdk-jdk10    文件:ChronoLocalDateImpl.java   
@Override
@SuppressWarnings("unchecked")
public D minus(TemporalAmount amount) {
    return (D) ChronoLocalDate.super.minus(amount);
}
项目:jdk8u-jdk    文件:TCKLocalTime.java   
@Test
public void test_plus_TemporalAmount_negativeMinutes() {
    TemporalAmount period = MockSimplePeriod.of(-25, ChronoUnit.MINUTES);
    LocalTime t = TEST_12_30_40_987654321.plus(period);
    assertEquals(t, LocalTime.of(12, 5, 40, 987654321));
}
项目:jdk8u-jdk    文件:TCKLocalTime.java   
@Test(expectedExceptions=DateTimeException.class)
public void test_plus_TemporalAmount_dateNotAllowed() {
    TemporalAmount period = MockSimplePeriod.of(7, ChronoUnit.MONTHS);
    TEST_12_30_40_987654321.plus(period);
}
项目:jdk8u-jdk    文件:TCKOffsetTime.java   
@Test(expectedExceptions=NullPointerException.class)
public void test_minus_MinusAdjuster_null() {
    TEST_11_30_59_500_PONE.minus((TemporalAmount) null);
}
项目:jdk8u-jdk    文件:TCKPeriod.java   
@Test
public void factory_from_TemporalAmount_Period() {
    TemporalAmount amount = Period.of(1, 2, 3);
    assertPeriod(Period.from(amount), 1, 2, 3);
}
项目:jdk8u-jdk    文件:TCKLocalTime.java   
@Test(expectedExceptions=NullPointerException.class)
public void test_minus_TemporalAmount_null() {
    TEST_12_30_40_987654321.minus((TemporalAmount) null);
}
项目:jdk8u-jdk    文件:TCKYear.java   
@Test(dataProvider="minusInvalidUnit", expectedExceptions=UnsupportedTemporalTypeException.class)
public void test_minusInvalidUnit(TemporalAmount amount) {
    TEST_2008.minus(amount);
}
项目:openjdk-jdk10    文件:TCKLocalDateTime.java   
@Test(expectedExceptions=NullPointerException.class)
public void test_minus_TemporalAmount_null() {
    TEST_2007_07_15_12_30_40_987654321.minus((TemporalAmount) null);
}
项目:openjdk-jdk10    文件:TCKOffsetTime.java   
@Test(expectedExceptions=NullPointerException.class)
public void test_plus_PlusAdjuster_null() {
    TEST_11_30_59_500_PONE.plus((TemporalAmount) null);
}
项目:openjdk-jdk10    文件:TCKLocalDateTime.java   
@Test(expectedExceptions=NullPointerException.class)
public void test_plus_TemporalAmount_null() {
    TEST_2007_07_15_12_30_40_987654321.plus((TemporalAmount) null);
}
项目:jdk8u-jdk    文件:TCKZonedDateTime.java   
@Test(expectedExceptions=NullPointerException.class)
public void test_plus_TemporalAmount_null() {
    TEST_DATE_TIME.plus((TemporalAmount) null);
}
项目:openjdk-jdk10    文件:TCKOffsetTime.java   
@Test(expectedExceptions=NullPointerException.class)
public void test_minus_MinusAdjuster_null() {
    TEST_11_30_59_500_PONE.minus((TemporalAmount) null);
}
项目:openjdk-jdk10    文件:TCKLocalTime.java   
@Test
public void test_plus_TemporalAmount_positiveHours() {
    TemporalAmount period = MockSimplePeriod.of(7, ChronoUnit.HOURS);
    LocalTime t = TEST_12_30_40_987654321.plus(period);
    assertEquals(t, LocalTime.of(19, 30, 40, 987654321));
}
项目:jdk8u-jdk    文件:TCKOffsetTime.java   
@Test(expectedExceptions=NullPointerException.class)
public void test_plus_PlusAdjuster_null() {
    TEST_11_30_59_500_PONE.plus((TemporalAmount) null);
}
项目:OpenJSharp    文件:Period.java   
/**
 * Returns a copy of this period with the specified period added.
 * <p>
 * This operates separately on the years, months and days.
 * No normalization is performed.
 * <p>
 * For example, "1 year, 6 months and 3 days" plus "2 years, 2 months and 2 days"
 * returns "3 years, 8 months and 5 days".
 * <p>
 * The specified amount is typically an instance of {@code Period}.
 * Other types are interpreted using {@link Period#from(TemporalAmount)}.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param amountToAdd  the period to add, not null
 * @return a {@code Period} based on this period with the requested period added, not null
 * @throws DateTimeException if the specified amount has a non-ISO chronology or
 *  contains an invalid unit
 * @throws ArithmeticException if numeric overflow occurs
 */
public Period plus(TemporalAmount amountToAdd) {
    Period isoAmount = Period.from(amountToAdd);
    return create(
            Math.addExact(years, isoAmount.years),
            Math.addExact(months, isoAmount.months),
            Math.addExact(days, isoAmount.days));
}