use of java.time.chrono.ChronoPeriod in project jdk8u_jdk by JetBrains.
the class TCKChronoPeriod method test_plus_wrongChrono.
@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);
}
use of java.time.chrono.ChronoPeriod in project jdk8u_jdk by JetBrains.
the class TCKChronoPeriod method test_minus.
@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));
}
use of java.time.chrono.ChronoPeriod in project jdk8u_jdk by JetBrains.
the class Period method from.
//-----------------------------------------------------------------------
/**
* 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);
}
use of java.time.chrono.ChronoPeriod in project jdk8u_jdk by JetBrains.
the class TCKMinguoChronology method test_periodUntilDate.
//-----------------------------------------------------------------------
// PeriodUntil()
//-----------------------------------------------------------------------
@Test
public void test_periodUntilDate() {
MinguoDate mdate1 = MinguoDate.of(1970, 1, 1);
MinguoDate mdate2 = MinguoDate.of(1971, 2, 2);
ChronoPeriod period = mdate1.until(mdate2);
assertEquals(period, MinguoChronology.INSTANCE.period(1, 1, 1));
}
use of java.time.chrono.ChronoPeriod in project jdk8u_jdk by JetBrains.
the class TCKThaiBuddhistChronology method test_periodUntilDate.
//-----------------------------------------------------------------------
// PeriodUntil()
//-----------------------------------------------------------------------
@Test
public void test_periodUntilDate() {
ThaiBuddhistDate mdate1 = ThaiBuddhistDate.of(1, 1, 1);
ThaiBuddhistDate mdate2 = ThaiBuddhistDate.of(2, 2, 2);
ChronoPeriod period = mdate1.until(mdate2);
assertEquals(period, ThaiBuddhistChronology.INSTANCE.period(1, 1, 1));
}
Aggregations