本文整理汇总了Java中java.time.temporal.TemporalAmount类的典型用法代码示例。如果您正苦于以下问题:Java TemporalAmount类的具体用法?Java TemporalAmount怎么用?Java TemporalAmount使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TemporalAmount类属于java.time.temporal包,在下文中一共展示了TemporalAmount类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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");
}
}
}
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:24,代码来源:TCKChronoZonedDateTime.java
示例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");
}
}
}
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:23,代码来源:TCKChronoLocalDate.java
示例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");
}
}
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:TCKChronoZonedDateTime.java
示例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");
}
}
}
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:24,代码来源:TCKChronoLocalDateTime.java
示例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));
}
}
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:TCKYearMonth.java
示例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;
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:18,代码来源:ChronoPeriodImpl.java
示例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);
}
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:47,代码来源:Period.java
示例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");
}
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:8,代码来源:TCKInstant.java
示例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));
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:10,代码来源:ChronoPeriodImpl.java
示例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));
}
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:10,代码来源:ChronoPeriodImpl.java
示例11: getTimeToLive
import java.time.temporal.TemporalAmount; //导入依赖的package包/类
public TemporalAmount getTimeToLive() {
return Duration.ofMinutes(5);
}
开发者ID:l0s,项目名称:fernet-java8,代码行数:4,代码来源:ProtocolBuffersExampleIT.java
示例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));
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:7,代码来源:TCKLocalTime.java
示例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;
}
开发者ID:OvercastNetwork,项目名称:ProjectAres,代码行数:4,代码来源:TimeUtils.java
示例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);
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:5,代码来源:TCKZonedDateTime.java
示例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);
}
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:7,代码来源:TCKLocalTime.java
示例16: test_minus_TemporalAmount_null
import java.time.temporal.TemporalAmount; //导入依赖的package包/类
@Test(expectedExceptions=NullPointerException.class)
public void test_minus_TemporalAmount_null() {
TEST_12_30_40_987654321.minus((TemporalAmount) null);
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:5,代码来源:TCKLocalTime.java
示例17: test_minusValid
import java.time.temporal.TemporalAmount; //导入依赖的package包/类
@Test(dataProvider="minusValid")
public void test_minusValid(int year, TemporalAmount amount, int expected) {
assertEquals(Year.of(year).minus(amount), Year.of(expected));
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:5,代码来源:TCKYear.java
示例18: test_plus_TemporalAmount_dateNotAllowed
import java.time.temporal.TemporalAmount; //导入依赖的package包/类
@Test(expectedExceptions=DateTimeException.class)
public void test_plus_TemporalAmount_dateNotAllowed() {
TemporalAmount period = MockSimplePeriod.of(7, ChronoUnit.MONTHS);
TEST_12_30_40_987654321.plus(period);
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:6,代码来源:TCKLocalTime.java
示例19: test_minus_TemporalAmount_positiveHours
import java.time.temporal.TemporalAmount; //导入依赖的package包/类
@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));
}
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:7,代码来源:TCKLocalTime.java
示例20: test_minus_TemporalAmount_wrap
import java.time.temporal.TemporalAmount; //导入依赖的package包/类
@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));
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:7,代码来源:TCKLocalTime.java
注:本文中的java.time.temporal.TemporalAmount类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论