本文整理汇总了Java中java.time.temporal.TemporalAmount类的典型用法代码示例。如果您正苦于以下问题:Java TemporalAmount类的具体用法?Java TemporalAmount怎么用?Java TemporalAmount使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TemporalAmount类属于java.time.temporal包,在下文中一共展示了TemporalAmount类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test_badPlusAdjusterChrono
import java.time.temporal.TemporalAmount; //导入依赖的package包/类
@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");
}
}
}
示例2: test_badPlusAdjusterChrono
import java.time.temporal.TemporalAmount; //导入依赖的package包/类
@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");
}
}
}
示例3: test_badMinusAdjusterChrono
import java.time.temporal.TemporalAmount; //导入依赖的package包/类
@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");
}
}
}
示例4: test_badPlusAdjusterChrono
import java.time.temporal.TemporalAmount; //导入依赖的package包/类
@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");
}
}
}
示例5: test_plus_TemporalAmount
import java.time.temporal.TemporalAmount; //导入依赖的package包/类
@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));
}
}
}
示例6: validateAmount
import java.time.temporal.TemporalAmount; //导入依赖的package包/类
/**
* 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;
}
示例7: from
import java.time.temporal.TemporalAmount; //导入依赖的package包/类
/**
* 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);
}
示例8: test_plusTemporalAmount
import java.time.temporal.TemporalAmount; //导入依赖的package包/类
@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");
}
示例9: plus
import java.time.temporal.TemporalAmount; //导入依赖的package包/类
@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));
}
示例10: minus
import java.time.temporal.TemporalAmount; //导入依赖的package包/类
@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));
}
示例11: getTimeToLive
import java.time.temporal.TemporalAmount; //导入依赖的package包/类
public TemporalAmount getTimeToLive() {
return Duration.ofMinutes(5);
}
示例12: test_plus_TemporalAmount_wrap
import java.time.temporal.TemporalAmount; //导入依赖的package包/类
@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));
}
示例13: getUnitOrDefault
import java.time.temporal.TemporalAmount; //导入依赖的package包/类
public static long getUnitOrDefault(TemporalAmount period, TemporalUnit unit, long def) {
return period.getUnits().contains(unit) ? period.get(unit) : def;
}
示例14: test_minus_TemporalAmount_null
import java.time.temporal.TemporalAmount; //导入依赖的package包/类
@Test(expectedExceptions=NullPointerException.class)
public void test_minus_TemporalAmount_null() {
TEST_DATE_TIME.minus((TemporalAmount) null);
}
示例15: test_minus_TemporalAmount_zero
import java.time.temporal.TemporalAmount; //导入依赖的package包/类
@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);
}