Java 类java.time.temporal.TemporalField 实例源码

项目:openjdk-jdk10    文件:LocalTime.java   
private int get0(TemporalField field) {
    switch ((ChronoField) field) {
        case NANO_OF_SECOND: return nano;
        case NANO_OF_DAY: throw new UnsupportedTemporalTypeException("Invalid field 'NanoOfDay' for get() method, use getLong() instead");
        case MICRO_OF_SECOND: return nano / 1000;
        case MICRO_OF_DAY: throw new UnsupportedTemporalTypeException("Invalid field 'MicroOfDay' for get() method, use getLong() instead");
        case MILLI_OF_SECOND: return nano / 1000_000;
        case MILLI_OF_DAY: return (int) (toNanoOfDay() / 1000_000);
        case SECOND_OF_MINUTE: return second;
        case SECOND_OF_DAY: return toSecondOfDay();
        case MINUTE_OF_HOUR: return minute;
        case MINUTE_OF_DAY: return hour * 60 + minute;
        case HOUR_OF_AMPM: return hour % 12;
        case CLOCK_HOUR_OF_AMPM: int ham = hour % 12; return (ham % 12 == 0 ? 12 : ham);
        case HOUR_OF_DAY: return hour;
        case CLOCK_HOUR_OF_DAY: return (hour == 0 ? 24 : hour);
        case AMPM_OF_DAY: return hour / 12;
    }
    throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
}
项目:jdk8u-jdk    文件:JapaneseDate.java   
@Override
public ValueRange range(TemporalField field) {
    if (field instanceof ChronoField) {
        if (isSupported(field)) {
            ChronoField f = (ChronoField) field;
            switch (f) {
                case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth());
                case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear());
                case YEAR_OF_ERA: {
                    Calendar jcal = Calendar.getInstance(JapaneseChronology.LOCALE);
                    jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET);
                    jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth());
                    return ValueRange.of(1, jcal.getActualMaximum(Calendar.YEAR));
                }
            }
            return getChronology().range(f);
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.rangeRefinedBy(this);
}
项目:openjdk-jdk10    文件:AbstractDateTimeTest.java   
@Test()
public void basicTest_get_TemporalField_supported() {
    for (TemporalAccessor sample : samples()) {
        for (TemporalField field : validFields()) {
            if (sample.range(field).isIntValue()) {
                sample.get(field);  // no exception
            } else {
                try {
                    sample.get(field);
                    fail("Failed on " + sample + " " + field);
                } catch (DateTimeException ex) {
                    // expected
                }
            }
        }
    }
}
项目:openjdk-jdk10    文件:TCKMinguoChronology.java   
@Test(dataProvider = "resolve_ymd")
public void test_resolve_ymd_strict(int y, int m, int d, MinguoDate 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) {
        MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
        assertEquals(date, expected);
        assertEquals(fieldValues.size(), 0);
    } else {
        try {
            MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
            fail("Should have failed");
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
项目:jdk8u-jdk    文件:AbstractDateTimeTest.java   
@Test()
public void basicTest_get_TemporalField_supported() {
    for (TemporalAccessor sample : samples()) {
        for (TemporalField field : validFields()) {
            if (sample.range(field).isIntValue()) {
                sample.get(field);  // no exception
            } else {
                try {
                    sample.get(field);
                    fail("Failed on " + sample + " " + field);
                } catch (DateTimeException ex) {
                    // expected
                }
            }
        }
    }
}
项目:jdk8u-jdk    文件:TCKMinguoChronology.java   
@Test(dataProvider = "resolve_ymaa")
public void test_resolve_ymaa_strict(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 (strict) {
        MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
        assertEquals(date, expected);
        assertEquals(fieldValues.size(), 0);
    } else {
        try {
            MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
            fail("Should have failed");
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
项目:jdk8u-jdk    文件:TCKIsoChronology.java   
@Test(dataProvider = "resolve_yearOfEra")
public void test_resolve_yearOfEra(ResolverStyle style, Integer e, Integer yoe, Integer y, ChronoField field, Integer expected) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    if (e != null) {
        fieldValues.put(ChronoField.ERA, (long) e);
    }
    if (yoe != null) {
        fieldValues.put(ChronoField.YEAR_OF_ERA, (long) yoe);
    }
    if (y != null) {
        fieldValues.put(ChronoField.YEAR, (long) y);
    }
    if (field != null) {
        LocalDate date = IsoChronology.INSTANCE.resolveDate(fieldValues, style);
        assertEquals(date, null);
        assertEquals(fieldValues.get(field), (Long) expected.longValue());
        assertEquals(fieldValues.size(), 1);
    } else {
        try {
            IsoChronology.INSTANCE.resolveDate(fieldValues, style);
            fail("Should have failed");
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
项目:openjdk-jdk10    文件:MinguoDate.java   
@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);
}
项目:openjdk-jdk10    文件:TCKLocalTime.java   
@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,
    };
    return Arrays.asList(array);
}
项目:OpenJSharp    文件:Parsed.java   
@Override
public long getLong(TemporalField field) {
    Objects.requireNonNull(field, "field");
    Long value = fieldValues.get(field);
    if (value != null) {
        return value;
    }
    if (date != null && date.isSupported(field)) {
        return date.getLong(field);
    }
    if (time != null && time.isSupported(field)) {
        return time.getLong(field);
    }
    if (field instanceof ChronoField) {
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.getFrom(this);
}
项目:jdk8u-jdk    文件:TCKIsoChronology.java   
@Test(dataProvider = "resolve_ymd")
public void test_resolve_ymd_strict(int y, int m, int d, LocalDate 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) {
        LocalDate date = IsoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
        assertEquals(date, expected);
        assertEquals(fieldValues.size(), 0);
    } else {
        try {
            IsoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
            fail("Should have failed");
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
项目:OpenJSharp    文件:HijrahDate.java   
@Override
public HijrahDate with(TemporalField field, long newValue) {
    if (field instanceof ChronoField) {
        ChronoField f = (ChronoField) field;
        // not using checkValidIntValue so EPOCH_DAY and PROLEPTIC_MONTH work
        chrono.range(f).checkValidValue(newValue, f);    // TODO: validate value
        int nvalue = (int) newValue;
        switch (f) {
            case DAY_OF_WEEK: return plusDays(newValue - getDayOfWeek());
            case ALIGNED_DAY_OF_WEEK_IN_MONTH: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_MONTH));
            case ALIGNED_DAY_OF_WEEK_IN_YEAR: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_YEAR));
            case DAY_OF_MONTH: return resolvePreviousValid(prolepticYear, monthOfYear, nvalue);
            case DAY_OF_YEAR: return plusDays(Math.min(nvalue, lengthOfYear()) - getDayOfYear());
            case EPOCH_DAY: return new HijrahDate(chrono, newValue);
            case ALIGNED_WEEK_OF_MONTH: return plusDays((newValue - getLong(ALIGNED_WEEK_OF_MONTH)) * 7);
            case ALIGNED_WEEK_OF_YEAR: return plusDays((newValue - getLong(ALIGNED_WEEK_OF_YEAR)) * 7);
            case MONTH_OF_YEAR: return resolvePreviousValid(prolepticYear, nvalue, dayOfMonth);
            case PROLEPTIC_MONTH: return plusMonths(newValue - getProlepticMonth());
            case YEAR_OF_ERA: return resolvePreviousValid(prolepticYear >= 1 ? nvalue : 1 - nvalue, monthOfYear, dayOfMonth);
            case YEAR: return resolvePreviousValid(nvalue, monthOfYear, dayOfMonth);
            case ERA: return resolvePreviousValid(1 - prolepticYear, monthOfYear, dayOfMonth);
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return super.with(field, newValue);
}
项目:openjdk-jdk10    文件:DateTimeTextProvider.java   
/**
 * Gets the text for the specified chrono, field, locale and style
 * for the purpose of formatting.
 * <p>
 * The text associated with the value is returned.
 * The null return value should be used if there is no applicable text, or
 * if the text would be a numeric representation of the value.
 *
 * @param chrono  the Chronology to get text for, not null
 * @param field  the field to get text for, not null
 * @param value  the field value to get text for, not null
 * @param style  the style to get text for, not null
 * @param locale  the locale to get text for, not null
 * @return the text for the field value, null if no text found
 */
public String getText(Chronology chrono, TemporalField field, long value,
                                TextStyle style, Locale locale) {
    if (chrono == IsoChronology.INSTANCE
            || !(field instanceof ChronoField)) {
        return getText(field, value, style, locale);
    }

    int fieldIndex;
    int fieldValue;
    if (field == ERA) {
        fieldIndex = Calendar.ERA;
        if (chrono == JapaneseChronology.INSTANCE) {
            if (value == -999) {
                fieldValue = 0;
            } else {
                fieldValue = (int) value + 2;
            }
        } else {
            fieldValue = (int) value;
        }
    } else if (field == MONTH_OF_YEAR) {
        fieldIndex = Calendar.MONTH;
        fieldValue = (int) value - 1;
    } else if (field == DAY_OF_WEEK) {
        fieldIndex = Calendar.DAY_OF_WEEK;
        fieldValue = (int) value + 1;
        if (fieldValue > 7) {
            fieldValue = Calendar.SUNDAY;
        }
    } else if (field == AMPM_OF_DAY) {
        fieldIndex = Calendar.AM_PM;
        fieldValue = (int) value;
    } else {
        return null;
    }
    return CalendarDataUtility.retrieveJavaTimeFieldValueName(
            chrono.getCalendarType(), fieldIndex, fieldValue, style.toCalendarStyle(), locale);
}
项目:jdk8u-jdk    文件:ThaiBuddhistDate.java   
@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);
}
项目:openjdk-jdk10    文件:DateTimeFormatter.java   
/**
 * Constructor.
 *
 * @param printerParser  the printer/parser to use, not null
 * @param locale  the locale to use, not null
 * @param decimalStyle  the DecimalStyle to use, not null
 * @param resolverStyle  the resolver style to use, not null
 * @param resolverFields  the fields to use during resolving, null for all fields
 * @param chrono  the chronology to use, null for no override
 * @param zone  the zone to use, null for no override
 */
DateTimeFormatter(CompositePrinterParser printerParser,
        Locale locale, DecimalStyle decimalStyle,
        ResolverStyle resolverStyle, Set<TemporalField> resolverFields,
        Chronology chrono, ZoneId zone) {
    this.printerParser = Objects.requireNonNull(printerParser, "printerParser");
    this.resolverFields = resolverFields;
    this.locale = Objects.requireNonNull(locale, "locale");
    this.decimalStyle = Objects.requireNonNull(decimalStyle, "decimalStyle");
    this.resolverStyle = Objects.requireNonNull(resolverStyle, "resolverStyle");
    this.chrono = chrono;
    this.zone = zone;
}
项目:jdk8u-jdk    文件:TCKWeekFields.java   
@Test(dataProvider="weekFields")
public void test_withWeekOfWeekBasedYear(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate day = LocalDate.of(2012, 12, 31);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField wowbyField = week.weekOfWeekBasedYear();
    TemporalField yowbyField = week.weekBasedYear();

    int dowExpected = (day.get(dowField) - 1) % 7 + 1;
    LocalDate dowDate = day.with(dowField, dowExpected);
    int dowResult = dowDate.get(dowField);
    assertEquals(dowResult, dowExpected, "Localized DayOfWeek not correct; " + day + " -->" + dowDate);

    int weekExpected = day.get(wowbyField) + 1;
    ValueRange range = day.range(wowbyField);
    weekExpected = ((weekExpected - 1) % (int)range.getMaximum()) + 1;
    LocalDate weekDate = day.with(wowbyField, weekExpected);
    int weekResult = weekDate.get(wowbyField);
    assertEquals(weekResult, weekExpected, "Localized WeekOfWeekBasedYear not correct; " + day + " -->" + weekDate);

    int yearExpected = day.get(yowbyField) + 1;

    LocalDate yearDate = day.with(yowbyField, yearExpected);
    int yearResult = yearDate.get(yowbyField);
    assertEquals(yearResult, yearExpected, "Localized WeekBasedYear not correct; " + day  + " --> " + yearDate);

    range = yearDate.range(wowbyField);
    weekExpected = Math.min(day.get(wowbyField), (int)range.getMaximum());

    int weekActual = yearDate.get(wowbyField);
    assertEquals(weekActual, weekExpected, "Localized WeekOfWeekBasedYear week should not change; " + day + " --> " + yearDate + ", actual: " + weekActual + ", weekExpected: " + weekExpected);
}
项目:jdk8u-jdk    文件:TCKWeekFields.java   
@Test(dataProvider="weekFields")
public void test_weekOfYearField(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 woyField = week.weekOfYear();

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

        // Verify that the combination of day of week and week of month can be used
        // to reconstruct the same date.
        LocalDate day1 = day.withDayOfYear(1);
        int offset = - (day1.get(dowField) - 1);
        int week1 = day1.get(woyField);
        if (week1 == 0) {
            // week of the 1st is partial; start with first full week
            offset += 7;
        }
        offset += actualDOW - 1;
        offset += (actualWOY - 1) * 7;
        LocalDate result = day1.plusDays(offset);

        assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear "
                + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n",
                week, day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result));
        day = day.plusDays(1);
    }
}
项目:jdk8u-jdk    文件:Parsed.java   
/**
 * Resolves the fields in this context.
 *
 * @param resolverStyle  the resolver style, not null
 * @param resolverFields  the fields to use for resolving, null for all fields
 * @return this, for method chaining
 * @throws DateTimeException if resolving one field results in a value for
 *  another field that is in conflict
 */
TemporalAccessor resolve(ResolverStyle resolverStyle, Set<TemporalField> resolverFields) {
    if (resolverFields != null) {
        fieldValues.keySet().retainAll(resolverFields);
    }
    this.resolverStyle = resolverStyle;
    resolveFields();
    resolveTimeLenient();
    crossCheck();
    resolvePeriod();
    resolveFractional();
    resolveInstant();
    return this;
}
项目:openjdk-jdk10    文件:TCKOffsetTime.java   
@Override
protected List<TemporalField> invalidFields() {
    List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values()));
    list.removeAll(validFields());
    list.add(JulianFields.JULIAN_DAY);
    list.add(JulianFields.MODIFIED_JULIAN_DAY);
    list.add(JulianFields.RATA_DIE);
    return list;
}
项目:openjdk-jdk10    文件:TCKLocalizedFieldParser.java   
@Test(dataProvider = "adjacentValuePatterns2")
public void test_adjacentValuePatterns2(String pattern, TemporalField field1, TemporalField field2,
        TemporalField field3, String text, int expected1, int expected2, int expected3) {
    DateTimeFormatter df = new DateTimeFormatterBuilder()
            .appendPattern(pattern).toFormatter(Locale.US);
    ParsePosition ppos = new ParsePosition(0);
    TemporalAccessor parsed = df.parseUnresolved(text, ppos);
    assertEquals(parsed.get(field1), expected1);
    assertEquals(parsed.get(field2), expected2);
    assertEquals(parsed.get(field3), expected3);
}
项目:jdk8u-jdk    文件:TestReducedParser.java   
@Test(dataProvider="error")
public void test_parse_error(TemporalField field, int width, int baseValue, String text, int pos, Class<?> expected) {
    try {
        getFormatter0(field, width, baseValue).parseUnresolved(text, new ParsePosition(pos));
    } catch (RuntimeException ex) {
        assertTrue(expected.isInstance(ex));
    }
}
项目:openjdk-jdk10    文件:TCKLocalTime.java   
@Override
protected List<TemporalField> invalidFields() {
    List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values()));
    list.removeAll(validFields());
    list.add(JulianFields.JULIAN_DAY);
    list.add(JulianFields.MODIFIED_JULIAN_DAY);
    list.add(JulianFields.RATA_DIE);
    return list;
}
项目:OpenJSharp    文件:ChronoLocalDateTimeImpl.java   
@Override
public ValueRange range(TemporalField field) {
    if (field instanceof ChronoField) {
        ChronoField f = (ChronoField) field;
        return (f.isTimeBased() ? time.range(field) : date.range(field));
    }
    return field.rangeRefinedBy(this);
}
项目:openjdk-jdk10    文件:TestFractionPrinterParser.java   
private void assertParsed(TemporalAccessor parsed, TemporalField field, Long value) {
    if (value == null) {
        assertEquals(parsed.isSupported(field), false);
    } else {
        assertEquals(parsed.isSupported(field), true);
        assertEquals(parsed.getLong(field), (long) value);
    }
}
项目:jdk8u-jdk    文件:TCKYear.java   
@Override
protected List<TemporalField> invalidFields() {
    List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values()));
    list.removeAll(validFields());
    list.add(JulianFields.JULIAN_DAY);
    list.add(JulianFields.MODIFIED_JULIAN_DAY);
    list.add(JulianFields.RATA_DIE);
    return list;
}
项目:openjdk-jdk10    文件:TCKMonth.java   
@Override
protected List<TemporalField> invalidFields() {
    List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values()));
    list.removeAll(validFields());
    list.add(JulianFields.JULIAN_DAY);
    list.add(JulianFields.MODIFIED_JULIAN_DAY);
    list.add(JulianFields.RATA_DIE);
    return list;
}
项目:openjdk-jdk10    文件:TCKJapaneseChronology.java   
@Test(dataProvider = "resolve_styleByEra")
public void test_resolve_yearOfEra_eraAndYearOnly_valid(ResolverStyle style, JapaneseEra era) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    fieldValues.put(ChronoField.ERA, (long) era.getValue());
    fieldValues.put(ChronoField.YEAR, 1L);
    JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, style);
    assertEquals(date, null);
    assertEquals(fieldValues.get(ChronoField.ERA), (Long) (long) era.getValue());
    assertEquals(fieldValues.get(ChronoField.YEAR), (Long) 1L);
    assertEquals(fieldValues.size(), 2);
}
项目:jdk8u-jdk    文件:TCKJapaneseChronology.java   
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
        }
    }
}
项目:openjdk-jdk10    文件:TestReducedParser.java   
@Test(dataProvider="ParseLenientSensitive")
public void test_parseLenient_baseDate(TemporalField field, int minWidth, int maxWidth, int baseValue, String input, int pos,
                              Pair strict, Pair lenient) {
    ParsePosition ppos = new ParsePosition(pos);
    setStrict(false);
    TemporalAccessor parsed = getFormatterBaseDate(field, minWidth, maxWidth, baseValue).parseUnresolved(input, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), lenient.parseLen, "error case parse position");
        assertEquals(parsed, lenient.parseVal, "unexpected parse result");
    } else {
        assertEquals(ppos.getIndex(), lenient.parseLen, "parse position");
        assertParsed(parsed, YEAR, lenient.parseVal != null ? (long) lenient.parseVal : null);
    }
}
项目:jdk8u-jdk    文件:TCKLocalizedFieldParser.java   
@Test(dataProvider="FieldPatterns")
public void test_parse_textField(String pattern, String text, int pos, int expectedPos, long expectedValue) {
    WeekFields weekDef = WeekFields.of(locale);
    TemporalField field = null;
    switch(pattern.charAt(0)) {
        case 'e' :
            field = weekDef.dayOfWeek();
            break;
        case 'w':
            field = weekDef.weekOfWeekBasedYear();
            break;
        case 'W':
            field = weekDef.weekOfMonth();
            break;
        case 'Y':
            field = weekDef.weekBasedYear();
            break;
        default:
            throw new IllegalStateException("bad format letter from pattern");
    }
    ParsePosition ppos = new ParsePosition(pos);
    DateTimeFormatterBuilder b
            = new DateTimeFormatterBuilder().appendPattern(pattern);
    DateTimeFormatter dtf = b.toFormatter(locale);
    TemporalAccessor parsed = dtf.parseUnresolved(text, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), expectedPos);
    } else {
        assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position");
        long value = parsed.getLong(field);
        assertEquals(value, expectedValue, "Value incorrect for " + field);
    }
}
项目:jdk8u-jdk    文件:TCKYear.java   
@Test
public void test_isSupported_TemporalField() {
    assertEquals(TEST_2008.isSupported((TemporalField) null), false);
    assertEquals(TEST_2008.isSupported(ChronoField.NANO_OF_SECOND), false);
    assertEquals(TEST_2008.isSupported(ChronoField.NANO_OF_DAY), false);
    assertEquals(TEST_2008.isSupported(ChronoField.MICRO_OF_SECOND), false);
    assertEquals(TEST_2008.isSupported(ChronoField.MICRO_OF_DAY), false);
    assertEquals(TEST_2008.isSupported(ChronoField.MILLI_OF_SECOND), false);
    assertEquals(TEST_2008.isSupported(ChronoField.MILLI_OF_DAY), false);
    assertEquals(TEST_2008.isSupported(ChronoField.SECOND_OF_MINUTE), false);
    assertEquals(TEST_2008.isSupported(ChronoField.SECOND_OF_DAY), false);
    assertEquals(TEST_2008.isSupported(ChronoField.MINUTE_OF_HOUR), false);
    assertEquals(TEST_2008.isSupported(ChronoField.MINUTE_OF_DAY), false);
    assertEquals(TEST_2008.isSupported(ChronoField.HOUR_OF_AMPM), false);
    assertEquals(TEST_2008.isSupported(ChronoField.CLOCK_HOUR_OF_AMPM), false);
    assertEquals(TEST_2008.isSupported(ChronoField.HOUR_OF_DAY), false);
    assertEquals(TEST_2008.isSupported(ChronoField.CLOCK_HOUR_OF_DAY), false);
    assertEquals(TEST_2008.isSupported(ChronoField.AMPM_OF_DAY), false);
    assertEquals(TEST_2008.isSupported(ChronoField.DAY_OF_WEEK), false);
    assertEquals(TEST_2008.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH), false);
    assertEquals(TEST_2008.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR), false);
    assertEquals(TEST_2008.isSupported(ChronoField.DAY_OF_MONTH), false);
    assertEquals(TEST_2008.isSupported(ChronoField.DAY_OF_YEAR), false);
    assertEquals(TEST_2008.isSupported(ChronoField.EPOCH_DAY), false);
    assertEquals(TEST_2008.isSupported(ChronoField.ALIGNED_WEEK_OF_MONTH), false);
    assertEquals(TEST_2008.isSupported(ChronoField.ALIGNED_WEEK_OF_YEAR), false);
    assertEquals(TEST_2008.isSupported(ChronoField.MONTH_OF_YEAR), false);
    assertEquals(TEST_2008.isSupported(ChronoField.PROLEPTIC_MONTH), false);
    assertEquals(TEST_2008.isSupported(ChronoField.YEAR), true);
    assertEquals(TEST_2008.isSupported(ChronoField.YEAR_OF_ERA), true);
    assertEquals(TEST_2008.isSupported(ChronoField.ERA), true);
    assertEquals(TEST_2008.isSupported(ChronoField.INSTANT_SECONDS), false);
    assertEquals(TEST_2008.isSupported(ChronoField.OFFSET_SECONDS), false);
}
项目:openjdk-jdk10    文件:AbstractDateTimeTest.java   
@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
            }
        }
    }
}
项目:OpenJSharp    文件:AbstractChronology.java   
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
}
项目:jdk8u-jdk    文件:AbstractDateTimeTest.java   
@Test()
public void basicTest_isSupported_TemporalField_unsupported() {
    for (TemporalAccessor sample : samples()) {
        for (TemporalField field : invalidFields()) {
            assertEquals(sample.isSupported(field), false, "Failed on " + sample + " " + field);
        }
    }
}
项目:jdk8u-jdk    文件:TestTextParser.java   
@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);
}
项目:openjdk-jdk10    文件:TCKJapaneseChronology.java   
@Test(dataProvider = "resolve_styleByEra")
public void test_resolve_yearOfEra_eraAndYearOfEraOnly_valid(ResolverStyle style, JapaneseEra era) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    fieldValues.put(ChronoField.ERA, (long) era.getValue());
    fieldValues.put(ChronoField.YEAR_OF_ERA, 1L);
    JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, style);
    assertEquals(date, null);
    assertEquals(fieldValues.get(ChronoField.ERA), (Long) (long) era.getValue());
    assertEquals(fieldValues.get(ChronoField.YEAR_OF_ERA), (Long) 1L);
    assertEquals(fieldValues.size(), 2);
}
项目:openjdk-jdk10    文件:TCKOffsetDateTime.java   
@Test
public void test_isSupported_TemporalField() {
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported((TemporalField) null), false);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.NANO_OF_SECOND), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.NANO_OF_DAY), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.MICRO_OF_SECOND), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.MICRO_OF_DAY), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.MILLI_OF_SECOND), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.MILLI_OF_DAY), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.SECOND_OF_MINUTE), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.SECOND_OF_DAY), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.MINUTE_OF_HOUR), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.MINUTE_OF_DAY), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.HOUR_OF_AMPM), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.CLOCK_HOUR_OF_AMPM), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.HOUR_OF_DAY), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.CLOCK_HOUR_OF_DAY), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.AMPM_OF_DAY), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.DAY_OF_WEEK), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.DAY_OF_MONTH), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.DAY_OF_YEAR), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.EPOCH_DAY), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.ALIGNED_WEEK_OF_MONTH), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.ALIGNED_WEEK_OF_YEAR), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.MONTH_OF_YEAR), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.PROLEPTIC_MONTH), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.YEAR), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.YEAR_OF_ERA), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.ERA), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.INSTANT_SECONDS), true);
    assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.OFFSET_SECONDS), true);
}
项目:jdk8u-jdk    文件:ChronoZonedDateTime.java   
@Override
default long getLong(TemporalField field) {
    if (field instanceof ChronoField) {
        switch ((ChronoField) field) {
            case INSTANT_SECONDS: return toEpochSecond();
            case OFFSET_SECONDS: return getOffset().getTotalSeconds();
        }
        return toLocalDateTime().getLong(field);
    }
    return field.getFrom(this);
}
项目:openjdk-jdk10    文件:TCKDayOfWeek.java   
@Override
protected List<TemporalField> validFields() {
    TemporalField[] array = {
        DAY_OF_WEEK,
    };
    return Arrays.asList(array);
}
项目:openjdk-jdk10    文件:TCKOffsetDateTime.java   
@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,
        OFFSET_SECONDS,
        INSTANT_SECONDS,
        JulianFields.JULIAN_DAY,
        JulianFields.MODIFIED_JULIAN_DAY,
        JulianFields.RATA_DIE,
    };
    return Arrays.asList(array);
}