本文整理汇总了Java中java.time.chrono.ChronoPeriod类的典型用法代码示例。如果您正苦于以下问题:Java ChronoPeriod类的具体用法?Java ChronoPeriod怎么用?Java ChronoPeriod使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ChronoPeriod类属于java.time.chrono包,在下文中一共展示了ChronoPeriod类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: adjustStartDate
import java.time.chrono.ChronoPeriod; //导入依赖的package包/类
/**
* Returns an adjusted fixed schedule with a new start date. This method may
* only be called on a fixed schedule with a start date specified. The
* existing length (whether specified with a end date or duration) is used in
* the returned fixed schedule.
*
* @param startDate the new start date. Must be non {@code null}.
* @return an adjusted fixed schedule with a new start date. Never
* {@code null}.
*/
ChronoFixedSchedule adjustStartDate ( final ChronoLocalDate startDate )
{
Preconditions.checkNotNull(startDate, "startDate required");
Preconditions.checkState(getStartDate() != null,
"no start date specified on this fixed schedule");
ChronoLocalDate endDate = getEndDate();
if ( endDate != null ) {
endDate =
startDate.plus(ChronoPeriod.between(getStartDate(), getEndDate()));
return new ChronoFixedSchedule(startDate, endDate);
} else if ( getDuration() != null ) {
return new ChronoFixedSchedule(startDate, getDuration());
} else {
return new ChronoFixedSchedule(startDate);
}
}
示例2: testOnUntilChronoLocalDate1
import java.time.chrono.ChronoPeriod; //导入依赖的package包/类
@Test
public void testOnUntilChronoLocalDate1() {
PersianDate pd1 = PersianDate.of(1380, 7, 16);
PersianDate pd2 = PersianDate.of(1400, 2, 21);
ChronoPeriod pd1UntilPd2 = pd1.until(pd2);
assertEquals(19, pd1UntilPd2.get(YEARS));
assertEquals(7, pd1UntilPd2.get(MONTHS));
assertEquals(5, pd1UntilPd2.get(DAYS));
ChronoPeriod pd2UntilPd1 = pd2.until(pd1);
assertEquals(-19, pd2UntilPd1.get(YEARS));
assertEquals(-7, pd2UntilPd1.get(MONTHS));
assertEquals(-5, pd2UntilPd1.get(DAYS));
}
示例3: testOnUntilChronoLocalDate2
import java.time.chrono.ChronoPeriod; //导入依赖的package包/类
@Test
public void testOnUntilChronoLocalDate2() {
PersianDate pd1 = PersianDate.of(1396, 5, 10);
PersianDate pd2 = PersianDate.of(1400, 11, 3);
ChronoPeriod pd1UntilPd2 = pd1.until(pd2);
assertEquals(4, pd1UntilPd2.get(YEARS));
assertEquals(5, pd1UntilPd2.get(MONTHS));
assertEquals(23, pd1UntilPd2.get(DAYS));
ChronoPeriod pd2UntilPd1 = pd2.until(pd1);
assertEquals(-4, pd2UntilPd1.get(YEARS));
assertEquals(-5, pd2UntilPd1.get(MONTHS));
assertEquals(-24, pd2UntilPd1.get(DAYS));
}
示例4: from
import java.time.chrono.ChronoPeriod; //导入依赖的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);
}
示例5: test_serialization
import java.time.chrono.ChronoPeriod; //导入依赖的package包/类
@Test(dataProvider="calendars")
public void test_serialization(Chronology chrono) throws Exception {
ChronoPeriod period = chrono.period(1, 2, 3);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(baos);
out.writeObject(period);
out.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream in = new ObjectInputStream(bais);
ChronoPeriod ser = (ChronoPeriod) in.readObject();
assertEquals(ser, period, "deserialized ChronoPeriod is wrong");
}
示例6: test_get
import java.time.chrono.ChronoPeriod; //导入依赖的package包/类
@Test(dataProvider="calendars")
public void test_get(Chronology chrono) {
ChronoPeriod period = chrono.period(1, 2, 3);
assertEquals(period.get(YEARS), 1);
assertEquals(period.get(MONTHS), 2);
assertEquals(period.get(DAYS), 3);
}
示例7: test_getUnits
import java.time.chrono.ChronoPeriod; //导入依赖的package包/类
@Test(dataProvider="calendars")
public void test_getUnits(Chronology chrono) {
ChronoPeriod period = chrono.period(1, 2, 3);
assertEquals(period.getUnits().size(), 3);
assertEquals(period.getUnits().get(0), YEARS);
assertEquals(period.getUnits().get(1), MONTHS);
assertEquals(period.getUnits().get(2), DAYS);
}
示例8: test_isZero_isNegative
import java.time.chrono.ChronoPeriod; //导入依赖的package包/类
@Test(dataProvider="calendars")
public void test_isZero_isNegative(Chronology chrono) {
ChronoPeriod periodPositive = chrono.period(1, 2, 3);
assertEquals(periodPositive.isZero(), false);
assertEquals(periodPositive.isNegative(), false);
ChronoPeriod periodZero = chrono.period(0, 0, 0);
assertEquals(periodZero.isZero(), true);
assertEquals(periodZero.isNegative(), false);
ChronoPeriod periodNegative = chrono.period(-1, 0, 0);
assertEquals(periodNegative.isZero(), false);
assertEquals(periodNegative.isNegative(), true);
}
示例9: test_plus
import java.time.chrono.ChronoPeriod; //导入依赖的package包/类
@Test(dataProvider="calendars")
public void test_plus(Chronology chrono) {
ChronoPeriod period = chrono.period(1, 2, 3);
ChronoPeriod period2 = chrono.period(2, 3, 4);
ChronoPeriod result = period.plus(period2);
assertEquals(result, chrono.period(3, 5, 7));
}
示例10: test_plus_wrongChrono
import java.time.chrono.ChronoPeriod; //导入依赖的package包/类
@Test(dataProvider="calendars", expectedExceptions=DateTimeException.class)
public void test_plus_wrongChrono(Chronology chrono) {
ChronoPeriod period = chrono.period(1, 2, 3);
ChronoPeriod isoPeriod = Period.of(2, 3, 4);
ChronoPeriod thaiPeriod = ThaiBuddhistChronology.INSTANCE.period(2, 3, 4);
// one of these two will fail
period.plus(isoPeriod);
period.plus(thaiPeriod);
}
示例11: test_minus
import java.time.chrono.ChronoPeriod; //导入依赖的package包/类
@Test(dataProvider="calendars")
public void test_minus(Chronology chrono) {
ChronoPeriod period = chrono.period(1, 2, 3);
ChronoPeriod period2 = chrono.period(2, 3, 4);
ChronoPeriod result = period.minus(period2);
assertEquals(result, chrono.period(-1, -1, -1));
}
示例12: test_minus_wrongChrono
import java.time.chrono.ChronoPeriod; //导入依赖的package包/类
@Test(dataProvider="calendars", expectedExceptions=DateTimeException.class)
public void test_minus_wrongChrono(Chronology chrono) {
ChronoPeriod period = chrono.period(1, 2, 3);
ChronoPeriod isoPeriod = Period.of(2, 3, 4);
ChronoPeriod thaiPeriod = ThaiBuddhistChronology.INSTANCE.period(2, 3, 4);
// one of these two will fail
period.minus(isoPeriod);
period.minus(thaiPeriod);
}
示例13: test_addTo
import java.time.chrono.ChronoPeriod; //导入依赖的package包/类
@Test(dataProvider="calendars")
public void test_addTo(Chronology chrono) {
ChronoPeriod period = chrono.period(1, 2, 3);
ChronoLocalDate date = chrono.dateNow();
Temporal result = period.addTo(date);
assertEquals(result, date.plus(14, MONTHS).plus(3, DAYS));
}
示例14: test_addTo_wrongChrono
import java.time.chrono.ChronoPeriod; //导入依赖的package包/类
@Test(dataProvider="calendars", expectedExceptions=DateTimeException.class)
public void test_addTo_wrongChrono(Chronology chrono) {
ChronoPeriod period = chrono.period(1, 2, 3);
ChronoLocalDate isoDate = LocalDate.of(2000, 1, 1);
ChronoLocalDate thaiDate = ThaiBuddhistChronology.INSTANCE.date(2000, 1, 1);
// one of these two will fail
period.addTo(isoDate);
period.addTo(thaiDate);
}
示例15: test_subtractFrom
import java.time.chrono.ChronoPeriod; //导入依赖的package包/类
@Test(dataProvider="calendars")
public void test_subtractFrom(Chronology chrono) {
ChronoPeriod period = chrono.period(1, 2, 3);
ChronoLocalDate date = chrono.dateNow();
Temporal result = period.subtractFrom(date);
assertEquals(result, date.minus(14, MONTHS).minus(3, DAYS));
}