/** * 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); } }
@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)); }
@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)); }
/** * 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); }
@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"); }
@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); }
@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); }
@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); }
@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)); }
@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); }
@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)); }
@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); }
@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)); }
@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); }
@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)); }
@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); }
@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()); }
@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); }
@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"); } }
@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)); }
@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)); }
@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)); }
@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)); }
@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)); }
@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)); }