java.time.chrono.ChronoPeriod Java Examples
The following examples show how to use
java.time.chrono.ChronoPeriod.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TCKChronoPeriod.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
@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); }
Example #2
Source File: TCKChronoPeriod.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
@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)); }
Example #3
Source File: TCKChronoPeriod.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
@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); }
Example #4
Source File: TCKMinguoChronology.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
@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)); }
Example #5
Source File: TCKChronoPeriod.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
@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)); }
Example #6
Source File: TCKChronoPeriod.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
@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); }
Example #7
Source File: TCKChronoPeriod.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="calendars") public void test_equals_notEqual(Chronology chrono) { ChronoPeriod a = chrono.period(1, 2, 3); ChronoPeriod b = chrono.period(2, 2, 3); assertEquals(a.equals(b), false); assertEquals(b.equals(a), false); assertEquals(a.equals(""), false); assertEquals(a.equals(null), false); }
Example #8
Source File: TCKMinguoChronology.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
@Test public void test_periodUntilDiffChrono() { MinguoDate mdate1 = MinguoDate.of(1970, 1, 1); MinguoDate mdate2 = MinguoDate.of(1971, 2, 2); ThaiBuddhistDate ldate2 = ThaiBuddhistChronology.INSTANCE.date(mdate2); ChronoPeriod period = mdate1.until(ldate2); assertEquals(period, MinguoChronology.INSTANCE.period(1, 1, 1)); }
Example #9
Source File: Period.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * 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); }
Example #10
Source File: TCKMinguoChronology.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
@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)); }
Example #11
Source File: TCKChronoPeriod.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
@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); }
Example #12
Source File: TCKChronoPeriod.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
@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)); }
Example #13
Source File: TCKChronoPeriod.java From hottub with GNU General Public License v2.0 | 5 votes |
@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); }
Example #14
Source File: TCKChronoPeriod.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
@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); }
Example #15
Source File: TCKChronoPeriod.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
@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); }
Example #16
Source File: TCKChronoPeriod.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="calendars") public void test_equals_notEqual(Chronology chrono) { ChronoPeriod a = chrono.period(1, 2, 3); ChronoPeriod b = chrono.period(2, 2, 3); assertEquals(a.equals(b), false); assertEquals(b.equals(a), false); assertEquals(a.equals(""), false); assertEquals(a.equals(null), false); }
Example #17
Source File: TCKChronoPeriod.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
@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); }
Example #18
Source File: TCKChronoPeriod.java From hottub with GNU General Public License v2.0 | 5 votes |
@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"); }
Example #19
Source File: TCKThaiBuddhistChronology.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
@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)); }
Example #20
Source File: TCKChronoPeriod.java From hottub with GNU General Public License v2.0 | 5 votes |
@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); }
Example #21
Source File: TCKChronoPeriod.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
@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)); }
Example #22
Source File: TCKMinguoChronology.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
@Test public void test_periodUntilDiffChrono() { MinguoDate mdate1 = MinguoDate.of(1970, 1, 1); MinguoDate mdate2 = MinguoDate.of(1971, 2, 2); ThaiBuddhistDate ldate2 = ThaiBuddhistChronology.INSTANCE.date(mdate2); ChronoPeriod period = mdate1.until(ldate2); assertEquals(period, MinguoChronology.INSTANCE.period(1, 1, 1)); }
Example #23
Source File: TCKChronoPeriod.java From hottub with GNU General Public License v2.0 | 5 votes |
@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); }
Example #24
Source File: Period.java From desugar_jdk_libs with GNU General Public License v2.0 | 5 votes |
/** * 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 = Math8.toIntExact(unitAmount); } else if (unit == ChronoUnit.MONTHS) { months = Math8.toIntExact(unitAmount); } else if (unit == ChronoUnit.DAYS) { days = Math8.toIntExact(unitAmount); } else { throw new DateTimeException("Unit must be Years, Months or Days, but was " + unit); } } return create(years, months, days); }
Example #25
Source File: TCKChronoPeriod.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
@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); }
Example #26
Source File: Period.java From JDKSourceCode1.8 with MIT License | 5 votes |
/** * 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); }
Example #27
Source File: TCKThaiBuddhistChronology.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
@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)); }
Example #28
Source File: TCKChronoPeriod.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
@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); }
Example #29
Source File: Period.java From Java8CN with Apache License 2.0 | 5 votes |
/** * 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); }
Example #30
Source File: TCKChronoPeriod.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
@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)); }