org.threeten.bp.temporal.TemporalField Java Examples

The following examples show how to use org.threeten.bp.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: DateTimeFormatterBuilder.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Constructor.
 *
 * @param field  the field to output, not null
 * @param minWidth  the minimum width to output, from 0 to 9
 * @param maxWidth  the maximum width to output, from 0 to 9
 * @param decimalPoint  whether to output the localized decimal point symbol
 */
FractionPrinterParser(TemporalField field, int minWidth, int maxWidth, boolean decimalPoint) {
    Jdk8Methods.requireNonNull(field, "field");
    if (field.range().isFixed() == false) {
        throw new IllegalArgumentException("Field must have a fixed set of values: " + field);
    }
    if (minWidth < 0 || minWidth > 9) {
        throw new IllegalArgumentException("Minimum width must be from 0 to 9 inclusive but was " + minWidth);
    }
    if (maxWidth < 1 || maxWidth > 9) {
        throw new IllegalArgumentException("Maximum width must be from 1 to 9 inclusive but was " + maxWidth);
    }
    if (maxWidth < minWidth) {
        throw new IllegalArgumentException("Maximum width must exceed or equal the minimum width but " +
                maxWidth + " < " + minWidth);
    }
    this.field = field;
    this.minWidth = minWidth;
    this.maxWidth = maxWidth;
    this.decimalPoint = decimalPoint;
}
 
Example #2
Source File: TestLocalDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
protected List<TemporalField> validFields() {
    TemporalField[] array = {
        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 #3
Source File: MinguoDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 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 #4
Source File: LocalDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private int get0(TemporalField field) {
    switch ((ChronoField) field) {
        case DAY_OF_WEEK: return getDayOfWeek().getValue();
        case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((day - 1) % 7) + 1;
        case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((getDayOfYear() - 1) % 7) + 1;
        case DAY_OF_MONTH: return day;
        case DAY_OF_YEAR: return getDayOfYear();
        case EPOCH_DAY: throw new DateTimeException("Field too large for an int: " + field);
        case ALIGNED_WEEK_OF_MONTH: return ((day - 1) / 7) + 1;
        case ALIGNED_WEEK_OF_YEAR: return ((getDayOfYear() - 1) / 7) + 1;
        case MONTH_OF_YEAR: return month;
        case PROLEPTIC_MONTH: throw new DateTimeException("Field too large for an int: " + field);
        case YEAR_OF_ERA: return (year >= 1 ? year : 1 - year);
        case YEAR: return year;
        case ERA: return (year >= 1 ? 1 : 0);
    }
    throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
}
 
Example #5
Source File: ThaiBuddhistDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 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 #6
Source File: HijrahDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public long getLong(TemporalField field) {
    if (field instanceof ChronoField) {
        switch ((ChronoField) field) {
            case DAY_OF_WEEK: return dayOfWeek.getValue();
            case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((dayOfMonth - 1) % 7) + 1;
            case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((dayOfYear - 1) % 7) + 1;
            case DAY_OF_MONTH: return this.dayOfMonth;
            case DAY_OF_YEAR: return this.dayOfYear;
            case EPOCH_DAY: return toEpochDay();
            case ALIGNED_WEEK_OF_MONTH: return ((dayOfMonth - 1) / 7) + 1;
            case ALIGNED_WEEK_OF_YEAR: return ((dayOfYear - 1) / 7) + 1;
            case MONTH_OF_YEAR: return monthOfYear;
            case YEAR_OF_ERA: return yearOfEra;
            case YEAR: return yearOfEra;
            case ERA: return era.getValue();
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.getFrom(this);
}
 
Example #7
Source File: JapaneseDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public ValueRange range(TemporalField field) {
    if (field instanceof ChronoField) {
        if (isSupported(field)) {
            ChronoField f = (ChronoField) field;
            switch (f) {
                case DAY_OF_YEAR:
                    return actualRange(Calendar.DAY_OF_YEAR);
                case YEAR_OF_ERA:
                    return actualRange(Calendar.YEAR);
            }
            return getChronology().range(f);
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.rangeRefinedBy(this);
}
 
Example #8
Source File: HijrahDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public HijrahDate with(TemporalField field, long newValue) {
    if (field instanceof ChronoField) {
        ChronoField f = (ChronoField) field;
        f.checkValidValue(newValue);        // TODO: validate value
        int nvalue = (int) newValue;
        switch (f) {
            case DAY_OF_WEEK: return plusDays(newValue - dayOfWeek.getValue());
            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(yearOfEra, monthOfYear, nvalue);
            case DAY_OF_YEAR: return resolvePreviousValid(yearOfEra, ((nvalue - 1) / 30) + 1, ((nvalue - 1) % 30) + 1);
            case EPOCH_DAY: return new HijrahDate(nvalue);
            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(yearOfEra, nvalue, dayOfMonth);
            case YEAR_OF_ERA: return resolvePreviousValid(yearOfEra >= 1 ? nvalue : 1 - nvalue, monthOfYear, dayOfMonth);
            case YEAR: return resolvePreviousValid(nvalue, monthOfYear, dayOfMonth);
            case ERA: return resolvePreviousValid(1 - yearOfEra, monthOfYear, dayOfMonth);
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.adjustInto(this, newValue);
}
 
Example #9
Source File: TestOffsetTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 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,
        OFFSET_SECONDS,
    };
    return Arrays.asList(array);
}
 
Example #10
Source File: TestOffsetTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected List<TemporalField> invalidFields() {
    List<TemporalField> list = new ArrayList<TemporalField>(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;
}
 
Example #11
Source File: DefaultInterfaceTemporalAccessor.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public ValueRange range(TemporalField field) {
    if (field instanceof ChronoField) {
        if (isSupported(field)) {
            return field.range();
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.rangeRefinedBy(this);
}
 
Example #12
Source File: AbstractDateTimeTest.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void basicTest_getLong_DateTimeField_unsupported() {
    for (TemporalAccessor sample : samples()) {
        for (TemporalField field : invalidFields()) {
            try {
                sample.getLong(field);
                fail("Failed on " + sample + " " + field);
            } catch (DateTimeException ex) {
                // expected
            }
        }
    }
}
 
Example #13
Source File: DefaultInterfaceEra.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public int get(TemporalField field) {
    if (field == ERA) {
        return getValue();
    }
    return range(field).checkValidIntValue(getLong(field), field);
}
 
Example #14
Source File: DefaultInterfaceEra.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public long getLong(TemporalField field) {
    if (field == ERA) {
        return getValue();
    } else if (field instanceof ChronoField) {
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.getFrom(this);
}
 
Example #15
Source File: TestDayOfWeek.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected List<TemporalField> invalidFields() {
    List<TemporalField> list = new ArrayList<TemporalField>(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;
}
 
Example #16
Source File: DateTimeBuilder.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void resolveMakeChanges(TemporalField targetField, ChronoLocalDate date) {
    if (chrono.equals(date.getChronology()) == false) {
        throw new DateTimeException("ChronoLocalDate must use the effective parsed chronology: " + chrono);
    }
    long epochDay = date.toEpochDay();
    Long old = fieldValues.put(ChronoField.EPOCH_DAY, epochDay);
    if (old != null && old.longValue() != epochDay) {
        throw new DateTimeException("Conflict found: " + LocalDate.ofEpochDay(old) +
                " differs from " + LocalDate.ofEpochDay(epochDay) +
                " while resolving  " + targetField);
    }
}
 
Example #17
Source File: SimpleDateTimeTextProvider.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Object findStore(TemporalField field, Locale locale) {
    Entry<TemporalField, Locale> key = createEntry(field, locale);
    Object store = cache.get(key);
    if (store == null) {
        store = createStore(field, locale);
        cache.putIfAbsent(key, store);
        store = cache.get(key);
    }
    return store;
}
 
Example #18
Source File: TestDateTimeTextPrinting.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(dataProvider="printText")
public void test_appendText2arg_print(TemporalField field, TextStyle style, int value, String expected) throws Exception {
    DateTimeFormatter f = builder.appendText(field, style).toFormatter(Locale.ENGLISH);
    LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
    dt = dt.with(field, value);
    String text = f.format(dt);
    assertEquals(text, expected);
}
 
Example #19
Source File: MinguoDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public MinguoDate with(TemporalField field, long newValue) {
    if (field instanceof ChronoField) {
        ChronoField f = (ChronoField) field;
        if (getLong(f) == newValue) {
            return this;
        }
        switch (f) {
            case PROLEPTIC_MONTH:
                getChronology().range(f).checkValidValue(newValue, f);
                return plusMonths(newValue - getProlepticMonth());
            case YEAR_OF_ERA:
            case YEAR:
            case ERA: {
                int nvalue = getChronology().range(f).checkValidIntValue(newValue, f);
                switch (f) {
                    case YEAR_OF_ERA:
                        return with(isoDate.withYear(getProlepticYear() >= 1 ? nvalue + YEARS_DIFFERENCE : (1 - nvalue)  + YEARS_DIFFERENCE));
                    case YEAR:
                        return with(isoDate.withYear(nvalue + YEARS_DIFFERENCE));
                    case ERA:
                        return with(isoDate.withYear((1 - getProlepticYear()) + YEARS_DIFFERENCE));
                }
            }
        }
        return with(isoDate.with(field, newValue));
    }
    return field.adjustInto(this, newValue);
}
 
Example #20
Source File: TestPadParserDecorator.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void assertParsed(TemporalField field, Long value) {
    if (value == null) {
        assertEquals(parseContext.getParsed(field), null);
    } else {
        assertEquals(parseContext.getParsed(field), value);
    }
}
 
Example #21
Source File: DateTimeBuilder.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean isSupported(TemporalField field) {
    if (field == null) {
        return false;
    }
    return fieldValues.containsKey(field) ||
            (date != null && date.isSupported(field)) ||
            (time != null && time.isSupported(field));
}
 
Example #22
Source File: ChronoLocalDateTimeImpl.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public ChronoLocalDateTimeImpl<D> with(TemporalField field, long newValue) {
    if (field instanceof ChronoField) {
        if (field.isTimeBased()) {
            return with(date, time.with(field, newValue));
        } else {
            return with(date.with(field, newValue), time);
        }
    }
    return date.getChronology().ensureChronoLocalDateTime(field.adjustInto(this, newValue));
}
 
Example #23
Source File: DateTimeFormatter.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Constructor.
 *
 * @param printerParser  the printer/parser to use, not null
 * @param locale  the locale to use, not null
 * @param decimalStyle  the decimal style 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 = Jdk8Methods.requireNonNull(printerParser, "printerParser");
    this.locale = Jdk8Methods.requireNonNull(locale, "locale");
    this.decimalStyle = Jdk8Methods.requireNonNull(decimalStyle, "decimalStyle");
    this.resolverStyle = Jdk8Methods.requireNonNull(resolverStyle, "resolverStyle");
    this.resolverFields = resolverFields;
    this.chrono = chrono;
    this.zone = zone;
}
 
Example #24
Source File: AbstractDateTimeTest.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void basicTest_isSupported_DateTimeField_unsupported() {
    for (TemporalAccessor sample : samples()) {
        for (TemporalField field : invalidFields()) {
            assertEquals(sample.isSupported(field), false, "Failed on " + sample + " " + field);
        }
    }
}
 
Example #25
Source File: DateTimeParseContext.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public long getLong(TemporalField field) {
    if (fieldValues.containsKey(field) == false) {
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return fieldValues.get(field);
}
 
Example #26
Source File: ChronoLocalDateTimeImpl.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public int get(TemporalField field) {
    if (field instanceof ChronoField) {
        return (field.isTimeBased() ? time.get(field) : date.get(field));
    }
    return range(field).checkValidIntValue(getLong(field), field);
}
 
Example #27
Source File: TestDateTimeTextPrinting.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(dataProvider="printText")
public void test_appendText1arg_print(TemporalField field, TextStyle style, int value, String expected) throws Exception {
    if (style == TextStyle.FULL) {
        DateTimeFormatter f = builder.appendText(field).toFormatter(Locale.ENGLISH);
        LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
        dt = dt.with(field, value);
        String text = f.format(dt);
        assertEquals(text, expected);
    }
}
 
Example #28
Source File: TestZoneOffset.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected List<TemporalField> invalidFields() {
    List<TemporalField> list = new ArrayList<TemporalField>(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;
}
 
Example #29
Source File: AbstractDateTimeTest.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void basicTest_isSupported_DateTimeField_supported() {
    for (TemporalAccessor sample : samples()) {
        for (TemporalField field : validFields()) {
            assertEquals(sample.isSupported(field), true, "Failed on " + sample + " " + field);
        }
    }
}
 
Example #30
Source File: DateTimeFormatterBuilder.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Constructor.
 *
 * @param field  the field to print, not null
 * @param minWidth  the minimum field width, from 1 to 19
 * @param maxWidth  the maximum field width, from minWidth to 19
 * @param signStyle  the positive/negative sign style, not null
 */
NumberPrinterParser(TemporalField field, int minWidth, int maxWidth, SignStyle signStyle) {
    // validated by caller
    this.field = field;
    this.minWidth = minWidth;
    this.maxWidth = maxWidth;
    this.signStyle = signStyle;
    this.subsequentWidth = 0;
}