java.time.temporal.TemporalField Java Examples

The following examples show how to use java.time.temporal.TemporalField. 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: TCKDateTimeParseResolver.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="resolveOneToField")
public void test_resolveOneToField(TemporalField field1, long value1,
                                   TemporalField expectedField1, Long expectedValue1,
                                   TemporalField expectedField2, Long expectedValue2) {
    String str = Long.toString(value1);
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(field1).toFormatter();

    TemporalAccessor accessor = f.parse(str);
    assertEquals(accessor.query(TemporalQueries.localDate()), null);
    assertEquals(accessor.query(TemporalQueries.localTime()), null);
    if (expectedField1 != null) {
        assertEquals(accessor.isSupported(expectedField1), true);
        assertEquals(accessor.getLong(expectedField1), expectedValue1.longValue());
    }
    if (expectedField2 != null) {
        assertEquals(accessor.isSupported(expectedField2), true);
        assertEquals(accessor.getLong(expectedField2), expectedValue2.longValue());
    }
}
 
Example #2
Source File: TCKWeekFields.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWomDow_lenient(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate date = LocalDate.of(2012, 12, 15);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField womField = week.weekOfMonth();

    for (int i = 1; i <= 60; i++) {
        DateTimeFormatter f = new DateTimeFormatterBuilder()
                .appendValue(YEAR).appendLiteral(':')
                .appendValue(MONTH_OF_YEAR).appendLiteral(':')
                .appendValue(womField).appendLiteral(':')
                .appendValue(dowField).toFormatter().withResolverStyle(LENIENT);
        int wom = date.get(womField);
        int dow = date.get(dowField);
        for (int j = wom - 10; j < wom + 10; j++) {
            String str = date.getYear() + ":" + date.getMonthValue() + ":" + j + ":" + dow;
            LocalDate parsed = LocalDate.parse(str, f);
            assertEquals(parsed, date.plusWeeks(j - wom), " ::" + str + ": :" + i + "::" + j);
        }

        date = date.plusDays(1);
    }
}
 
Example #3
Source File: TCKMinguoChronology.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "resolve_yd")
public void test_resolve_yd_smart(int y, int d, MinguoDate expected, boolean smart, boolean strict) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    fieldValues.put(ChronoField.YEAR, (long) y);
    fieldValues.put(ChronoField.DAY_OF_YEAR, (long) d);
    if (smart) {
        MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART);
        assertEquals(date, expected);
        assertEquals(fieldValues.size(), 0);
    } else {
        try {
            MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART);
            fail("Should have failed");
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example #4
Source File: TCKWeekFields.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWoWBYDow_lenient(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate date = LocalDate.of(2012, 12, 31);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField wowbyField = week.weekOfWeekBasedYear();
    TemporalField yowbyField = week.weekBasedYear();

    for (int i = 1; i <= 60; i++) {
        DateTimeFormatter f = new DateTimeFormatterBuilder()
                .appendValue(yowbyField).appendLiteral(':')
                .appendValue(wowbyField).appendLiteral(':')
                .appendValue(dowField).toFormatter().withResolverStyle(LENIENT);
        int wowby = date.get(wowbyField);
        int dow = date.get(dowField);
        for (int j = wowby - 60; j < wowby + 60; j++) {
            String str = date.get(yowbyField) + ":" + j + ":" + dow;
            LocalDate parsed = LocalDate.parse(str, f);
            assertEquals(parsed, date.plusWeeks(j - wowby), " ::" + str + ": :" + i + "::" + j);
        }

        date = date.plusDays(1);
    }
}
 
Example #5
Source File: TCKWeekFields.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_withDayOfWeek(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate day = LocalDate.of(2012, 12, 15);  // Safely in the middle of a month
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField womField = week.weekOfMonth();
    TemporalField woyField = week.weekOfYear();

    int wom = day.get(womField);
    int woy = day.get(woyField);
    for (int dow = 1; dow <= 7; dow++) {
        LocalDate result = day.with(dowField, dow);
        assertEquals(result.get(dowField), dow, String.format("Incorrect new Day of week: %s", result));
        assertEquals(result.get(womField), wom, "Week of Month should not change");
        assertEquals(result.get(woyField), woy, "Week of Year should not change");
    }
}
 
Example #6
Source File: TCKChronoZonedDateTime.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="calendars")
public void test_badTemporalFieldChrono(Chronology chrono) {
    LocalDate refDate = LocalDate.of(2013, 1, 1);
    ChronoZonedDateTime<?> czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
        TemporalField adjuster = new FixedTemporalField(czdt2);
        if (chrono != chrono2) {
            try {
                czdt.with(adjuster, 1);
                Assert.fail("TemporalField doWith() should have thrown a ClassCastException, " + czdt.getClass()
                        + " can not be cast to " + czdt2.getClass());
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoZonedDateTime<?> result = czdt.with(adjuster, 1);
            assertEquals(result, czdt2, "TemporalField doWith() failed to replace date");
        }
    }
}
 
Example #7
Source File: TCKChronoLocalDateTime.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="calendars")
public void test_badTemporalFieldChrono(Chronology chrono) {
    LocalDate refDate = LocalDate.of(2013, 1, 1);
    ChronoLocalDateTime<?> cdt = chrono.date(refDate).atTime(LocalTime.NOON);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON);
        TemporalField adjuster = new FixedTemporalField(cdt2);
        if (chrono != chrono2) {
            try {
                cdt.with(adjuster, 1);
                Assert.fail("TemporalField doWith() should have thrown a ClassCastException" + cdt.getClass()
                        + ", can not be cast to " + cdt2.getClass());
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoLocalDateTime<?> result = cdt.with(adjuster, 1);
            assertEquals(result, cdt2, "TemporalField doWith() failed to replace date");
        }
    }
}
 
Example #8
Source File: CopticDate.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
public long getLong(TemporalField field) {
    if (field instanceof ChronoField) {
        switch ((ChronoField) field) {
            case DAY_OF_WEEK: return Math.floorMod(toEpochDay() + 3, 7) + 1;
            case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((day - 1) % 7) + 1;
            case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((get(ChronoField.DAY_OF_YEAR) - 1) % 7) + 1;
            case DAY_OF_MONTH: return day;
            case DAY_OF_YEAR: return (month - 1) * 30 + day;
            case EPOCH_DAY: return toEpochDay();
            case ALIGNED_WEEK_OF_MONTH: return ((day - 1) / 7) + 1;
            case ALIGNED_WEEK_OF_YEAR: return ((get(ChronoField.DAY_OF_YEAR) - 1) / 7) + 1;
            case MONTH_OF_YEAR: return month;
            case YEAR_OF_ERA: return (prolepticYear >= 1 ? prolepticYear : 1 - prolepticYear);
            case YEAR: return prolepticYear;
            case ERA: return (prolepticYear >= 1 ? 1 : 0);
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.getFrom(this);
}
 
Example #9
Source File: TCKMinguoChronology.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "resolve_ymaa")
public void test_resolve_ymaa_smart(int y, int m, int w, int d, MinguoDate expected, boolean smart, boolean strict) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    fieldValues.put(ChronoField.YEAR, (long) y);
    fieldValues.put(ChronoField.MONTH_OF_YEAR, (long) m);
    fieldValues.put(ChronoField.ALIGNED_WEEK_OF_MONTH, (long) w);
    fieldValues.put(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH, (long) d);
    if (smart) {
        MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART);
        assertEquals(date, expected);
        assertEquals(fieldValues.size(), 0);
    } else {
        try {
            MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART);
            fail("Should have failed");
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example #10
Source File: TCKChronoZonedDateTime.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="calendars")
public void test_badTemporalFieldChrono(Chronology chrono) {
    LocalDate refDate = LocalDate.of(2013, 1, 1);
    ChronoZonedDateTime<?> czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
        TemporalField adjuster = new FixedTemporalField(czdt2);
        if (chrono != chrono2) {
            try {
                czdt.with(adjuster, 1);
                Assert.fail("TemporalField doWith() should have thrown a ClassCastException, " + czdt.getClass()
                        + " can not be cast to " + czdt2.getClass());
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoZonedDateTime<?> result = czdt.with(adjuster, 1);
            assertEquals(result, czdt2, "TemporalField doWith() failed to replace date");
        }
    }
}
 
Example #11
Source File: TCKThaiBuddhistChronology.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "resolve_yd")
public void test_resolve_yd_strict(int y, int d, ThaiBuddhistDate expected, boolean smart, boolean strict) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    fieldValues.put(ChronoField.YEAR, (long) y);
    fieldValues.put(ChronoField.DAY_OF_YEAR, (long) d);
    if (strict) {
        ThaiBuddhistDate date = ThaiBuddhistChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
        assertEquals(date, expected);
        assertEquals(fieldValues.size(), 0);
    } else {
        try {
            ThaiBuddhistChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
            fail("Should have failed");
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example #12
Source File: TCKChronoLocalDateTime.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="calendars")
public void test_badTemporalFieldChrono(Chronology chrono) {
    LocalDate refDate = LocalDate.of(2013, 1, 1);
    ChronoLocalDateTime<?> cdt = chrono.date(refDate).atTime(LocalTime.NOON);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON);
        TemporalField adjuster = new FixedTemporalField(cdt2);
        if (chrono != chrono2) {
            try {
                cdt.with(adjuster, 1);
                Assert.fail("TemporalField doWith() should have thrown a ClassCastException" + cdt.getClass()
                        + ", can not be cast to " + cdt2.getClass());
            } catch (ClassCastException cce) {
                // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoLocalDateTime<?> result = cdt.with(adjuster, 1);
            assertEquals(result, cdt2, "TemporalField doWith() failed to replace date");
        }
    }
}
 
Example #13
Source File: TCKWeekFields.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWoy_lenient(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate date = LocalDate.of(2012, 12, 15);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField woyField = week.weekOfYear();

    for (int i = 1; i <= 60; i++) {
        DateTimeFormatter f = new DateTimeFormatterBuilder()
                .appendValue(YEAR).appendLiteral(':')
                .appendValue(woyField).appendLiteral(':')
                .appendValue(DAY_OF_WEEK).toFormatter().withResolverStyle(LENIENT);
        int woy = date.get(woyField);
        int dow = date.get(DAY_OF_WEEK);
        for (int j = woy - 60; j < woy + 60; j++) {
            String str = date.getYear() + ":" + j + ":" + dow;
            LocalDate parsed = LocalDate.parse(str, f);
            assertEquals(parsed, date.plusWeeks(j - woy), " ::" + str + ": :" + i + "::" + j);
        }

        date = date.plusDays(1);
    }
}
 
Example #14
Source File: TCKJapaneseChronology.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "resolve_ymd")
public void test_resolve_ymd_strict(int y, int m, int d, JapaneseDate expected, Object smart, boolean strict) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    fieldValues.put(ChronoField.YEAR, (long) y);
    fieldValues.put(ChronoField.MONTH_OF_YEAR, (long) m);
    fieldValues.put(ChronoField.DAY_OF_MONTH, (long) d);
    if (strict) {
        JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
        assertEquals(date, expected);
        assertEquals(fieldValues.size(), 0);
    } else {
        try {
            JapaneseChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
            fail("Should have failed");
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example #15
Source File: TCKJapaneseChronology.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "resolve_yd")
public void test_resolve_yd_smart(int y, int d, JapaneseDate expected, boolean smart, boolean strict) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    fieldValues.put(ChronoField.YEAR, (long) y);
    fieldValues.put(ChronoField.DAY_OF_YEAR, (long) d);
    if (smart) {
        JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART);
        assertEquals(date, expected);
        assertEquals(fieldValues.size(), 0);
    } else {
        try {
            JapaneseChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART);
            fail("Should have failed");
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example #16
Source File: TCKWeekFields.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWoy_lenient(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate date = LocalDate.of(2012, 12, 15);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField woyField = week.weekOfYear();

    for (int i = 1; i <= 60; i++) {
        DateTimeFormatter f = new DateTimeFormatterBuilder()
                .appendValue(YEAR).appendLiteral(':')
                .appendValue(woyField).appendLiteral(':')
                .appendValue(DAY_OF_WEEK).toFormatter().withResolverStyle(LENIENT);
        int woy = date.get(woyField);
        int dow = date.get(DAY_OF_WEEK);
        for (int j = woy - 60; j < woy + 60; j++) {
            String str = date.getYear() + ":" + j + ":" + dow;
            LocalDate parsed = LocalDate.parse(str, f);
            assertEquals(parsed, date.plusWeeks(j - woy), " ::" + str + ": :" + i + "::" + j);
        }

        date = date.plusDays(1);
    }
}
 
Example #17
Source File: TCKWeekFields.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWoWBYDow_lenient(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate date = LocalDate.of(2012, 12, 31);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField wowbyField = week.weekOfWeekBasedYear();
    TemporalField yowbyField = week.weekBasedYear();

    for (int i = 1; i <= 60; i++) {
        DateTimeFormatter f = new DateTimeFormatterBuilder()
                .appendValue(yowbyField).appendLiteral(':')
                .appendValue(wowbyField).appendLiteral(':')
                .appendValue(dowField).toFormatter().withResolverStyle(LENIENT);
        int wowby = date.get(wowbyField);
        int dow = date.get(dowField);
        for (int j = wowby - 60; j < wowby + 60; j++) {
            String str = date.get(yowbyField) + ":" + j + ":" + dow;
            LocalDate parsed = LocalDate.parse(str, f);
            assertEquals(parsed, date.plusWeeks(j - wowby), " ::" + str + ": :" + i + "::" + j);
        }

        date = date.plusDays(1);
    }
}
 
Example #18
Source File: MinguoDate.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public long getLong(TemporalField field) {
    if (field instanceof ChronoField) {
        switch ((ChronoField) field) {
            case PROLEPTIC_MONTH:
                return getProlepticMonth();
            case YEAR_OF_ERA: {
                int prolepticYear = getProlepticYear();
                return (prolepticYear >= 1 ? prolepticYear : 1 - prolepticYear);
            }
            case YEAR:
                return getProlepticYear();
            case ERA:
                return (getProlepticYear() >= 1 ? 1 : 0);
        }
        return isoDate.getLong(field);
    }
    return field.getFrom(this);
}
 
Example #19
Source File: TestTextParser.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="parseText")
public void test_parse_strict_caseSensitive_parseUpper(TemporalField field, TextStyle style, int value, String input) throws Exception {
    if (input.equals(input.toUpperCase(Locale.ROOT))) {
        // Skip if the given input is all upper case (e.g., "Q1")
        return;
    }
    setCaseSensitive(true);
    ParsePosition pos = new ParsePosition(0);
    getFormatter(field, style).parseUnresolved(input.toUpperCase(Locale.ROOT), pos);
    assertEquals(pos.getErrorIndex(), 0);
}
 
Example #20
Source File: AbstractDateTimeTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Test()
public void basicTest_range_TemporalField_unsupported() {
    for (TemporalAccessor sample : samples()) {
        for (TemporalField field : invalidFields()) {
            try {
                sample.range(field);
                fail("Failed on " + sample + " " + field);
            } catch (DateTimeException ex) {
                // expected
            }
        }
    }
}
 
Example #21
Source File: TCKLocalDateTime.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected List<TemporalField> validFields() {
    TemporalField[] array = {
        NANO_OF_SECOND,
        NANO_OF_DAY,
        MICRO_OF_SECOND,
        MICRO_OF_DAY,
        MILLI_OF_SECOND,
        MILLI_OF_DAY,
        SECOND_OF_MINUTE,
        SECOND_OF_DAY,
        MINUTE_OF_HOUR,
        MINUTE_OF_DAY,
        CLOCK_HOUR_OF_AMPM,
        HOUR_OF_AMPM,
        CLOCK_HOUR_OF_DAY,
        HOUR_OF_DAY,
        AMPM_OF_DAY,
        DAY_OF_WEEK,
        ALIGNED_DAY_OF_WEEK_IN_MONTH,
        ALIGNED_DAY_OF_WEEK_IN_YEAR,
        DAY_OF_MONTH,
        DAY_OF_YEAR,
        EPOCH_DAY,
        ALIGNED_WEEK_OF_MONTH,
        ALIGNED_WEEK_OF_YEAR,
        MONTH_OF_YEAR,
        PROLEPTIC_MONTH,
        YEAR_OF_ERA,
        YEAR,
        ERA,
        JulianFields.JULIAN_DAY,
        JulianFields.MODIFIED_JULIAN_DAY,
        JulianFields.RATA_DIE,
    };
    return Arrays.asList(array);
}
 
Example #22
Source File: TCKJapaneseChronology.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public void test_resolve_yearOfEra_eraOnly_invalidTooSmall() {
    for (ResolverStyle style : ResolverStyle.values()) {
        Map<TemporalField, Long> fieldValues = new HashMap<>();
        fieldValues.put(ChronoField.ERA, JapaneseEra.MEIJI.getValue() - 1L);
        try {
            JapaneseChronology.INSTANCE.resolveDate(fieldValues, style);
            fail("Should have failed: " + style);
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example #23
Source File: AbstractChronology.java    From j2objc with Apache License 2.0 5 votes vote down vote up
ChronoLocalDate resolveYD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
    int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR);
    if (resolverStyle == ResolverStyle.LENIENT) {
        long days = Math.subtractExact(fieldValues.remove(DAY_OF_YEAR), 1);
        return dateYearDay(y, 1).plus(days, DAYS);
    }
    int doy = range(DAY_OF_YEAR).checkValidIntValue(fieldValues.remove(DAY_OF_YEAR), DAY_OF_YEAR);
    return dateYearDay(y, doy);  // smart is same as strict
}
 
Example #24
Source File: TestTextParser.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="parseText")
public void test_parse_strict_caseInsensitive_parseLower(TemporalField field, TextStyle style, int value, String input) throws Exception {
    setCaseSensitive(false);
    ParsePosition pos = new ParsePosition(0);
    assertEquals(getFormatter(field, style).parseUnresolved(input.toLowerCase(Locale.ROOT), pos).getLong(field), (long) value);
    assertEquals(pos.getIndex(), input.length());
}
 
Example #25
Source File: TestReducedParser.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="ParseLenientSensitive")
public void test_parseStrict(TemporalField field, int minWidth, int maxWidth, int baseValue, String input, int pos,
    Pair strict, Pair lenient) {
    ParsePosition ppos = new ParsePosition(pos);
    setStrict(true);
    TemporalAccessor parsed = getFormatter0(field, minWidth, maxWidth, baseValue).parseUnresolved(input, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), strict.parseLen, "error case parse position");
        assertEquals(parsed, strict.parseVal, "unexpected parse result");
    } else {
        assertEquals(ppos.getIndex(), strict.parseLen, "parse position");
        assertParsed(parsed, YEAR, strict.parseVal != null ? (long) strict.parseVal : null);
    }
}
 
Example #26
Source File: TCKThaiBuddhistChronology.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "resolve_ymd")
public void test_resolve_ymd_lenient(int y, int m, int d, ThaiBuddhistDate expected, Object smart, boolean strict) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    fieldValues.put(ChronoField.YEAR, (long) y);
    fieldValues.put(ChronoField.MONTH_OF_YEAR, (long) m);
    fieldValues.put(ChronoField.DAY_OF_MONTH, (long) d);
    ThaiBuddhistDate date = ThaiBuddhistChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.LENIENT);
    assertEquals(date, expected);
    assertEquals(fieldValues.size(), 0);
}
 
Example #27
Source File: TCKJapaneseChronology.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "resolve_yd")
public void test_resolve_yd_lenient(int y, int d, JapaneseDate expected, boolean smart, boolean strict) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    fieldValues.put(ChronoField.YEAR, (long) y);
    fieldValues.put(ChronoField.DAY_OF_YEAR, (long) d);
    JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.LENIENT);
    assertEquals(date, expected);
    assertEquals(fieldValues.size(), 0);
}
 
Example #28
Source File: TestIsoWeekFields.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "fields")
public void test_WOWBY_basics(TemporalField weekField, TemporalField yearField) {
    assertEquals(weekField.isDateBased(), true);
    assertEquals(weekField.isTimeBased(), false);
    assertEquals(weekField.getBaseUnit(), ChronoUnit.WEEKS);
    assertEquals(weekField.getRangeUnit(), IsoFields.WEEK_BASED_YEARS);
}
 
Example #29
Source File: TCKHijrahChronology.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "resolve_yd")
public void test_resolve_yd_lenient(int y, int d, HijrahDate expected, boolean smart, boolean strict) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    fieldValues.put(ChronoField.YEAR, (long) y);
    fieldValues.put(ChronoField.DAY_OF_YEAR, (long) d);
    HijrahDate date = HijrahChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.LENIENT);
    assertEquals(date, expected);
    assertEquals(fieldValues.size(), 0);
}
 
Example #30
Source File: TCKWeekFields.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_weekOfMonthField(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate day = LocalDate.of(2012, 12, 31);  // Known to be ISO Monday
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField womField = week.weekOfMonth();

    for (int i = 1; i <= 15; i++) {
        int actualDOW = day.get(dowField);
        int actualWOM = day.get(womField);

        // Verify that the combination of day of week and week of month can be used
        // to reconstruct the same date.
        LocalDate day1 = day.withDayOfMonth(1);
        int offset = - (day1.get(dowField) - 1);

        int week1 = day1.get(womField);
        if (week1 == 0) {
            // week of the 1st is partial; start with first full week
            offset += 7;
        }

        offset += actualDOW - 1;
        offset += (actualWOM - 1) * 7;
        LocalDate result = day1.plusDays(offset);

        assertEquals(result, day, "Incorrect dayOfWeek or weekOfMonth: "
                + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n",
                week, day.getDayOfWeek(), offset, actualDOW, actualWOM, day, result));
        day = day.plusDays(1);
    }
}