use of java.time.chrono.ChronoLocalDate in project jdk8u_jdk by JetBrains.
the class LocalDate method until.
/**
* Calculates the period between this date and another date as a {@code Period}.
* <p>
* This calculates the period between two dates in terms of years, months and days.
* The start and end points are {@code this} and the specified date.
* The result will be negative if the end is before the start.
* The negative sign will be the same in each of year, month and day.
* <p>
* The calculation is performed using the ISO calendar system.
* If necessary, the input date will be converted to ISO.
* <p>
* The start date is included, but the end date is not.
* The period is calculated by removing complete months, then calculating
* the remaining number of days, adjusting to ensure that both have the same sign.
* The number of months is then normalized into years and months based on a 12 month year.
* A month is considered to be complete if the end day-of-month is greater
* than or equal to the start day-of-month.
* For example, from {@code 2010-01-15} to {@code 2011-03-18} is "1 year, 2 months and 3 days".
* <p>
* There are two equivalent ways of using this method.
* The first is to invoke this method.
* The second is to use {@link Period#between(LocalDate, LocalDate)}:
* <pre>
* // these two lines are equivalent
* period = start.until(end);
* period = Period.between(start, end);
* </pre>
* The choice should be made based on which makes the code more readable.
*
* @param endDateExclusive the end date, exclusive, which may be in any chronology, not null
* @return the period between this date and the end date, not null
*/
@Override
public Period until(ChronoLocalDate endDateExclusive) {
LocalDate end = LocalDate.from(endDateExclusive);
// safe
long totalMonths = end.getProlepticMonth() - this.getProlepticMonth();
int days = end.day - this.day;
if (totalMonths > 0 && days < 0) {
totalMonths--;
LocalDate calcDate = this.plusMonths(totalMonths);
// safe
days = (int) (end.toEpochDay() - calcDate.toEpochDay());
} else if (totalMonths < 0 && days > 0) {
totalMonths++;
days -= end.lengthOfMonth();
}
// safe
long years = totalMonths / 12;
// safe
int months = (int) (totalMonths % 12);
return Period.of(Math.toIntExact(years), months, days);
}
use of java.time.chrono.ChronoLocalDate in project jdk8u_jdk by JetBrains.
the class TCKThaiBuddhistChronology method test_InvalidEras.
//-----------------------------------------------------------------------
// Bad Era for Chronology.date(era,...) and Chronology.prolepticYear(Era,...)
//-----------------------------------------------------------------------
@Test
public void test_InvalidEras() {
// Verify that the eras from every other Chronology are invalid
for (Chronology chrono : Chronology.getAvailableChronologies()) {
if (chrono instanceof ThaiBuddhistChronology) {
continue;
}
List<Era> eras = chrono.eras();
for (Era era : eras) {
try {
ChronoLocalDate date = ThaiBuddhistChronology.INSTANCE.date(era, 1, 1, 1);
fail("ThaiBuddhistChronology.date did not throw ClassCastException for Era: " + era);
} catch (ClassCastException cex) {
// ignore expected exception
;
}
try {
@SuppressWarnings("unused") int year = ThaiBuddhistChronology.INSTANCE.prolepticYear(era, 1);
fail("ThaiBuddhistChronology.prolepticYear did not throw ClassCastException for Era: " + era);
} catch (ClassCastException cex) {
// ignore expected exception
;
}
}
}
}
use of java.time.chrono.ChronoLocalDate in project jdk8u_jdk by JetBrains.
the class TCKDateTimeParseResolver method test_fieldResolvesToChronoLocalDate_overrideChrono_wrongChrono.
@Test(expectedExceptions = DateTimeParseException.class)
public void test_fieldResolvesToChronoLocalDate_overrideChrono_wrongChrono() {
ChronoLocalDate cld = ThaiBuddhistChronology.INSTANCE.dateNow();
DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(cld)).toFormatter();
f = f.withChronology(MinguoChronology.INSTANCE);
f.parse("1234567890");
}
use of java.time.chrono.ChronoLocalDate in project jdk8u_jdk by JetBrains.
the class TCKDateTimeParseResolver method test_fieldResolvesToChronoLocalDate_noOverrideChrono_wrongChrono.
@Test(expectedExceptions = DateTimeParseException.class)
public void test_fieldResolvesToChronoLocalDate_noOverrideChrono_wrongChrono() {
ChronoLocalDate cld = ThaiBuddhistChronology.INSTANCE.dateNow();
DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(cld)).toFormatter();
f.parse("1234567890");
}
use of java.time.chrono.ChronoLocalDate in project jdk8u_jdk by JetBrains.
the class TestUmmAlQuraChronology method test_valueRange_monthDays.
// Test to verify the maximum number of days by adding one month to a given date
@Test(dataProvider = "monthDays")
public void test_valueRange_monthDays(int year, int month, int maxlength) {
ChronoLocalDate date = HijrahChronology.INSTANCE.date(year, month, 1);
ValueRange range = null;
for (int i = 1; i <= 12; i++) {
range = date.range(ChronoField.DAY_OF_MONTH);
date = date.plus(1, ChronoUnit.MONTHS);
assertEquals(range.getMaximum(), month, maxlength);
}
}
Aggregations