Search in sources :

Example 11 with ValueRange

use of java.time.temporal.ValueRange in project jdk8u_jdk by JetBrains.

the class AbstractChronology method resolveYMD.

ChronoLocalDate resolveYMD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
    int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR);
    if (resolverStyle == ResolverStyle.LENIENT) {
        long months = Math.subtractExact(fieldValues.remove(MONTH_OF_YEAR), 1);
        long days = Math.subtractExact(fieldValues.remove(DAY_OF_MONTH), 1);
        return date(y, 1, 1).plus(months, MONTHS).plus(days, DAYS);
    }
    int moy = range(MONTH_OF_YEAR).checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR), MONTH_OF_YEAR);
    ValueRange domRange = range(DAY_OF_MONTH);
    int dom = domRange.checkValidIntValue(fieldValues.remove(DAY_OF_MONTH), DAY_OF_MONTH);
    if (resolverStyle == ResolverStyle.SMART) {
        // previous valid
        try {
            return date(y, moy, dom);
        } catch (DateTimeException ex) {
            return date(y, moy, 1).with(TemporalAdjusters.lastDayOfMonth());
        }
    }
    return date(y, moy, dom);
}
Also used : ValueRange(java.time.temporal.ValueRange) DateTimeException(java.time.DateTimeException)

Example 12 with ValueRange

use of java.time.temporal.ValueRange in project jdk8u_jdk by JetBrains.

the class ChronoLocalDateImpl method monthsUntil.

private long monthsUntil(ChronoLocalDate end) {
    ValueRange range = getChronology().range(MONTH_OF_YEAR);
    if (range.getMaximum() != 12) {
        throw new IllegalStateException("ChronoLocalDateImpl only supports Chronologies with 12 months per year");
    }
    // no overflow
    long packed1 = getLong(PROLEPTIC_MONTH) * 32L + get(DAY_OF_MONTH);
    // no overflow
    long packed2 = end.getLong(PROLEPTIC_MONTH) * 32L + end.get(DAY_OF_MONTH);
    return (packed2 - packed1) / 32;
}
Also used : ValueRange(java.time.temporal.ValueRange)

Example 13 with ValueRange

use of java.time.temporal.ValueRange in project jdk8u_jdk by JetBrains.

the class TCKWeekFields method test_rangeWeekOfWeekBasedYear.

@Test(dataProvider = "weekFields")
public void test_rangeWeekOfWeekBasedYear(DayOfWeek firstDayOfWeek, int minDays) {
    WeekFields weekFields = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = weekFields.dayOfWeek();
    TemporalField wowByField = weekFields.weekOfWeekBasedYear();
    LocalDate day1 = LocalDate.of(2012, 1, weekFields.getMinimalDaysInFirstWeek());
    day1 = day1.with(wowByField, 1).with(dowField, 1);
    LocalDate day2 = LocalDate.of(2013, 1, weekFields.getMinimalDaysInFirstWeek());
    day2 = day2.with(wowByField, 1).with(dowField, 1);
    int expectedWeeks = (int) ChronoUnit.DAYS.between(day1, day2) / 7;
    ValueRange range = day1.range(wowByField);
    assertEquals(range.getMaximum(), expectedWeeks, "Range incorrect");
}
Also used : ValueRange(java.time.temporal.ValueRange) TemporalField(java.time.temporal.TemporalField) WeekFields(java.time.temporal.WeekFields) LocalDate(java.time.LocalDate) Test(org.testng.annotations.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest)

Example 14 with ValueRange

use of java.time.temporal.ValueRange in project jdk8u_jdk by JetBrains.

the class TestUmmAlQuraChronology method Test_UmmAlQuraRanges.

// Test to verify the min/max field ranges for given dates
@Test(dataProvider = "dates")
public void Test_UmmAlQuraRanges(HijrahDate date, int minYear, int maxYear, int minMonth, int maxMonth, int minDay, int maxDay, int maxChronoDay) {
    // Check the chronology ranges
    HijrahChronology chrono = date.getChronology();
    ValueRange yearRange = chrono.range(YEAR);
    assertEquals(yearRange.getMinimum(), minYear, "Minimum year for Hijrah chronology");
    assertEquals(yearRange.getLargestMinimum(), minYear, "Largest minimum year for Hijrah chronology");
    assertEquals(yearRange.getMaximum(), maxYear, "Maximum year for Hijrah chronology");
    assertEquals(yearRange.getSmallestMaximum(), maxYear, "Smallest Maximum year for Hijrah chronology");
    ValueRange monthRange = chrono.range(MONTH_OF_YEAR);
    assertEquals(monthRange.getMinimum(), minMonth, "Minimum month for Hijrah chronology");
    assertEquals(monthRange.getMaximum(), maxMonth, "Maximum month for Hijrah chronology");
    ValueRange daysRange = chrono.range(DAY_OF_MONTH);
    assertEquals(daysRange.getMinimum(), minDay, "Minimum day for chronology");
    assertEquals(daysRange.getMaximum(), maxChronoDay, "Maximum day for Hijrah chronology");
    // Check the date ranges
    yearRange = date.range(YEAR);
    assertEquals(yearRange.getMinimum(), minYear, "Minimum year for Hijrah date");
    assertEquals(yearRange.getLargestMinimum(), minYear, "Largest minimum  year for Hijrah date");
    assertEquals(yearRange.getMaximum(), maxYear, "Maximum year for Hijrah date");
    assertEquals(yearRange.getSmallestMaximum(), maxYear, "Smallest maximum year for Hijrah date");
    monthRange = date.range(MONTH_OF_YEAR);
    assertEquals(monthRange.getMinimum(), minMonth, "Minimum month for HijrahDate");
    assertEquals(monthRange.getMaximum(), maxMonth, "Maximum month for HijrahDate");
    daysRange = date.range(DAY_OF_MONTH);
    assertEquals(daysRange.getMinimum(), minDay, "Minimum day for HijrahDate");
    assertEquals(daysRange.getMaximum(), maxDay, "Maximum day for HijrahDate");
}
Also used : ValueRange(java.time.temporal.ValueRange) HijrahChronology(java.time.chrono.HijrahChronology) Test(org.testng.annotations.Test)

Example 15 with ValueRange

use of java.time.temporal.ValueRange 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

ValueRange (java.time.temporal.ValueRange)28 Test (org.testng.annotations.Test)14 AbstractTest (test.java.time.AbstractTest)6 LocalDate (java.time.LocalDate)3 HijrahChronology (java.time.chrono.HijrahChronology)3 TemporalField (java.time.temporal.TemporalField)3 WeekFields (java.time.temporal.WeekFields)3 AbstractTCKTest (tck.java.time.AbstractTCKTest)3 DateTimeException (java.time.DateTimeException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 ChronoLocalDate (java.time.chrono.ChronoLocalDate)1 HijrahDate (java.time.chrono.HijrahDate)1 JapaneseDate (java.time.chrono.JapaneseDate)1 JapaneseEra (java.time.chrono.JapaneseEra)1