类java.time.chrono.ChronoPeriod源码实例Demo

下面列出了怎么用java.time.chrono.ChronoPeriod的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: jdk8u60   文件: TCKThaiBuddhistChronology.java
@Test
public void test_periodUntilDiffChrono() {
    ThaiBuddhistDate mdate1 = ThaiBuddhistDate.of(1, 1, 1);
    ThaiBuddhistDate mdate2 = ThaiBuddhistDate.of(2, 2, 2);
    MinguoDate ldate2 = MinguoChronology.INSTANCE.date(mdate2);
    ChronoPeriod period = mdate1.until(ldate2);
    assertEquals(period, ThaiBuddhistChronology.INSTANCE.period(1, 1, 1));
}
 
源代码2 项目: jdk8u-jdk   文件: TCKChronoPeriod.java
@Test(dataProvider="calendars")
public void test_equals_equal(Chronology chrono) {
    ChronoPeriod a1 = chrono.period(1, 2, 3);
    ChronoPeriod a2 = chrono.period(1, 2, 3);
    assertEquals(a1, a1);
    assertEquals(a1, a2);
    assertEquals(a2, a1);
    assertEquals(a2, a2);
    assertEquals(a1.hashCode(), a2.hashCode());
}
 
源代码3 项目: jdk8u-jdk   文件: TCKChronoPeriod.java
@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));
}
 
源代码4 项目: openjdk-jdk8u   文件: TCKChronoPeriod.java
@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);
}
 
源代码5 项目: jdk8u_jdk   文件: TCKChronoPeriod.java
@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 项目: j2objc   文件: TCKChronoPeriod.java
@Test()
@UseDataProvider("data_of_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));
}
 
源代码7 项目: dragonwell8_jdk   文件: TCKChronoPeriod.java
@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);
}
 
源代码8 项目: hottub   文件: TCKChronoPeriod.java
@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));
}
 
源代码9 项目: j2objc   文件: TCKChronoPeriod.java
@Test()
@UseDataProvider("data_of_calendars")
public void test_equals_equal(Chronology chrono) {
    ChronoPeriod a1 = chrono.period(1, 2, 3);
    ChronoPeriod a2 = chrono.period(1, 2, 3);
    assertEquals(a1, a1);
    assertEquals(a1, a2);
    assertEquals(a2, a1);
    assertEquals(a2, a2);
    assertEquals(a1.hashCode(), a2.hashCode());
}
 
源代码10 项目: openjdk-jdk9   文件: TCKChronoPeriod.java
@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);
}
 
源代码11 项目: dragonwell8_jdk   文件: TCKChronoPeriod.java
@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);
}
 
源代码12 项目: dragonwell8_jdk   文件: TCKChronoPeriod.java
@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));
}
 
源代码13 项目: dragonwell8_jdk   文件: TCKChronoPeriod.java
@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);
}
 
源代码14 项目: hottub   文件: TCKThaiBuddhistChronology.java
@Test
public void test_periodUntilDiffChrono() {
    ThaiBuddhistDate mdate1 = ThaiBuddhistDate.of(1, 1, 1);
    ThaiBuddhistDate mdate2 = ThaiBuddhistDate.of(2, 2, 2);
    MinguoDate ldate2 = MinguoChronology.INSTANCE.date(mdate2);
    ChronoPeriod period = mdate1.until(ldate2);
    assertEquals(period, ThaiBuddhistChronology.INSTANCE.period(1, 1, 1));
}
 
源代码15 项目: openjdk-jdk8u   文件: TCKChronoPeriod.java
@Test(dataProvider="calendars")
public void test_equals_equal(Chronology chrono) {
    ChronoPeriod a1 = chrono.period(1, 2, 3);
    ChronoPeriod a2 = chrono.period(1, 2, 3);
    assertEquals(a1, a1);
    assertEquals(a1, a2);
    assertEquals(a2, a1);
    assertEquals(a2, a2);
    assertEquals(a1.hashCode(), a2.hashCode());
}
 
源代码16 项目: j2objc   文件: TCKChronoPeriod.java
@Test()
@UseDataProvider("data_of_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);
}
 
源代码17 项目: openjdk-jdk8u   文件: TCKChronoPeriod.java
@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");
}
 
源代码18 项目: jdk8u-jdk   文件: TCKThaiBuddhistChronology.java
@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));
}
 
源代码19 项目: dragonwell8_jdk   文件: TCKChronoPeriod.java
@Test(dataProvider="calendars")
public void test_toString(Chronology chrono) {
    ChronoPeriod period = chrono.period(1, 2, 3);
    if (period instanceof Period == false) {
        assertEquals(period.toString(), chrono.getId() + " P1Y2M3D");
    }
}
 
源代码20 项目: jdk8u-dev-jdk   文件: TCKThaiBuddhistChronology.java
@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));
}
 
源代码21 项目: dragonwell8_jdk   文件: TCKJapaneseChronology.java
@Test
public void test_periodUntilDiffChrono() {
    JapaneseDate mdate1 = JapaneseDate.of(1970, 1, 1);
    JapaneseDate mdate2 = JapaneseDate.of(1971, 2, 2);
    MinguoDate ldate2 = MinguoChronology.INSTANCE.date(mdate2);
    ChronoPeriod period = mdate1.until(ldate2);
    assertEquals(period, JapaneseChronology.INSTANCE.period(1, 1, 1));
}
 
源代码22 项目: dragonwell8_jdk   文件: TCKMinguoChronology.java
@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));
}
 
源代码23 项目: jdk8u_jdk   文件: TCKChronoPeriod.java
@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);
}
 
源代码24 项目: jdk8u_jdk   文件: TCKChronoPeriod.java
@Test(dataProvider="calendars", expectedExceptions=DateTimeException.class)
public void test_subtractFrom_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.subtractFrom(isoDate);
    period.subtractFrom(thaiDate);
}
 
源代码25 项目: openjdk-jdk9   文件: TCKChronoPeriod.java
@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));
}
 
源代码26 项目: jdk8u-jdk   文件: TCKChronoPeriod.java
@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));
}
 
源代码27 项目: jdk8u_jdk   文件: TCKJapaneseChronology.java
@Test
public void test_periodUntilDate() {
    JapaneseDate mdate1 = JapaneseDate.of(1970, 1, 1);
    JapaneseDate mdate2 = JapaneseDate.of(1971, 2, 2);
    ChronoPeriod period = mdate1.until(mdate2);
    assertEquals(period, JapaneseChronology.INSTANCE.period(1, 1, 1));
}
 
源代码28 项目: openjdk-jdk8u-backup   文件: TCKChronoPeriod.java
@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));
}
 
源代码29 项目: jdk8u-jdk   文件: TCKChronoPeriod.java
@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);
}
 
源代码30 项目: Bytecoder   文件: 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);
}
 
 类所在包
 同包方法