Search in sources :

Example 21 with ChronoLocalDate

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);
}
Also used : ChronoLocalDate(java.time.chrono.ChronoLocalDate)

Example 22 with ChronoLocalDate

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
                ;
            }
        }
    }
}
Also used : ChronoLocalDate(java.time.chrono.ChronoLocalDate) ThaiBuddhistEra(java.time.chrono.ThaiBuddhistEra) Era(java.time.chrono.Era) ThaiBuddhistChronology(java.time.chrono.ThaiBuddhistChronology) ThaiBuddhistChronology(java.time.chrono.ThaiBuddhistChronology) IsoChronology(java.time.chrono.IsoChronology) Chronology(java.time.chrono.Chronology) MinguoChronology(java.time.chrono.MinguoChronology) Test(org.testng.annotations.Test)

Example 23 with ChronoLocalDate

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");
}
Also used : ChronoLocalDate(java.time.chrono.ChronoLocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 24 with ChronoLocalDate

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");
}
Also used : ChronoLocalDate(java.time.chrono.ChronoLocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 25 with ChronoLocalDate

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);
    }
}
Also used : ValueRange(java.time.temporal.ValueRange) ChronoLocalDate(java.time.chrono.ChronoLocalDate) Test(org.testng.annotations.Test)

Aggregations

ChronoLocalDate (java.time.chrono.ChronoLocalDate)43 Test (org.testng.annotations.Test)34 Chronology (java.time.chrono.Chronology)28 IsoChronology (java.time.chrono.IsoChronology)19 ThaiBuddhistChronology (java.time.chrono.ThaiBuddhistChronology)18 HijrahChronology (java.time.chrono.HijrahChronology)17 MinguoChronology (java.time.chrono.MinguoChronology)17 JapaneseChronology (java.time.chrono.JapaneseChronology)15 LocalDate (java.time.LocalDate)10 DateTimeException (java.time.DateTimeException)5 ChronoPeriod (java.time.chrono.ChronoPeriod)4 Era (java.time.chrono.Era)4 DateTimeFormatter (java.time.format.DateTimeFormatter)3 TemporalAccessor (java.time.temporal.TemporalAccessor)3 TemporalField (java.time.temporal.TemporalField)3 ChronoZonedDateTime (java.time.chrono.ChronoZonedDateTime)2 ThaiBuddhistDate (java.time.chrono.ThaiBuddhistDate)2 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)2 Temporal (java.time.temporal.Temporal)2 TemporalAmount (java.time.temporal.TemporalAmount)2