Search in sources :

Example 16 with UnsupportedTemporalTypeException

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

the class TCKDateTimeFormatter method test_format_withChronology_nonChronoFieldMapLink.

@Test
public void test_format_withChronology_nonChronoFieldMapLink() {
    TemporalAccessor temporal = new TemporalAccessor() {

        @Override
        public boolean isSupported(TemporalField field) {
            return field == IsoFields.WEEK_BASED_YEAR;
        }

        @Override
        public long getLong(TemporalField field) {
            if (field == IsoFields.WEEK_BASED_YEAR) {
                return 2345;
            }
            throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
        }
    };
    DateTimeFormatter test = new DateTimeFormatterBuilder().appendValue(IsoFields.WEEK_BASED_YEAR, 4).toFormatter(Locale.ENGLISH).withChronology(IsoChronology.INSTANCE);
    String result = test.format(temporal);
    assertEquals(result, "2345");
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) TemporalField(java.time.temporal.TemporalField) UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 17 with UnsupportedTemporalTypeException

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

the class Instant method with.

/**
     * Returns a copy of this instant with the specified field set to a new value.
     * <p>
     * This returns an {@code Instant}, based on this one, with the value
     * for the specified field changed.
     * If it is not possible to set the value, because the field is not supported or for
     * some other reason, an exception is thrown.
     * <p>
     * If the field is a {@link ChronoField} then the adjustment is implemented here.
     * The supported fields behave as follows:
     * <ul>
     * <li>{@code NANO_OF_SECOND} -
     *  Returns an {@code Instant} with the specified nano-of-second.
     *  The epoch-second will be unchanged.
     * <li>{@code MICRO_OF_SECOND} -
     *  Returns an {@code Instant} with the nano-of-second replaced by the specified
     *  micro-of-second multiplied by 1,000. The epoch-second will be unchanged.
     * <li>{@code MILLI_OF_SECOND} -
     *  Returns an {@code Instant} with the nano-of-second replaced by the specified
     *  milli-of-second multiplied by 1,000,000. The epoch-second will be unchanged.
     * <li>{@code INSTANT_SECONDS} -
     *  Returns an {@code Instant} with the specified epoch-second.
     *  The nano-of-second will be unchanged.
     * </ul>
     * <p>
     * In all cases, if the new value is outside the valid range of values for the field
     * then a {@code DateTimeException} will be thrown.
     * <p>
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
     * <p>
     * If the field is not a {@code ChronoField}, then the result of this method
     * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
     * passing {@code this} as the argument. In this case, the field determines
     * whether and how to adjust the instant.
     * <p>
     * This instance is immutable and unaffected by this method call.
     *
     * @param field  the field to set in the result, not null
     * @param newValue  the new value of the field in the result
     * @return an {@code Instant} based on {@code this} with the specified field set, not null
     * @throws DateTimeException if the field cannot be set
     * @throws UnsupportedTemporalTypeException if the field is not supported
     * @throws ArithmeticException if numeric overflow occurs
     */
@Override
public Instant with(TemporalField field, long newValue) {
    if (field instanceof ChronoField) {
        ChronoField f = (ChronoField) field;
        f.checkValidValue(newValue);
        switch(f) {
            case MILLI_OF_SECOND:
                {
                    int nval = (int) newValue * 1000_000;
                    return (nval != nanos ? create(seconds, nval) : this);
                }
            case MICRO_OF_SECOND:
                {
                    int nval = (int) newValue * 1000;
                    return (nval != nanos ? create(seconds, nval) : this);
                }
            case NANO_OF_SECOND:
                return (newValue != nanos ? create(seconds, (int) newValue) : this);
            case INSTANT_SECONDS:
                return (newValue != seconds ? create(newValue, nanos) : this);
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.adjustInto(this, newValue);
}
Also used : ChronoField(java.time.temporal.ChronoField) UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException)

Example 18 with UnsupportedTemporalTypeException

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

the class YearMonth method with.

/**
     * Returns a copy of this year-month with the specified field set to a new value.
     * <p>
     * This returns a {@code YearMonth}, based on this one, with the value
     * for the specified field changed.
     * This can be used to change any supported field, such as the year or month.
     * If it is not possible to set the value, because the field is not supported or for
     * some other reason, an exception is thrown.
     * <p>
     * If the field is a {@link ChronoField} then the adjustment is implemented here.
     * The supported fields behave as follows:
     * <ul>
     * <li>{@code MONTH_OF_YEAR} -
     *  Returns a {@code YearMonth} with the specified month-of-year.
     *  The year will be unchanged.
     * <li>{@code PROLEPTIC_MONTH} -
     *  Returns a {@code YearMonth} with the specified proleptic-month.
     *  This completely replaces the year and month of this object.
     * <li>{@code YEAR_OF_ERA} -
     *  Returns a {@code YearMonth} with the specified year-of-era
     *  The month and era will be unchanged.
     * <li>{@code YEAR} -
     *  Returns a {@code YearMonth} with the specified year.
     *  The month will be unchanged.
     * <li>{@code ERA} -
     *  Returns a {@code YearMonth} with the specified era.
     *  The month and year-of-era will be unchanged.
     * </ul>
     * <p>
     * In all cases, if the new value is outside the valid range of values for the field
     * then a {@code DateTimeException} will be thrown.
     * <p>
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
     * <p>
     * If the field is not a {@code ChronoField}, then the result of this method
     * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
     * passing {@code this} as the argument. In this case, the field determines
     * whether and how to adjust the instant.
     * <p>
     * This instance is immutable and unaffected by this method call.
     *
     * @param field  the field to set in the result, not null
     * @param newValue  the new value of the field in the result
     * @return a {@code YearMonth} based on {@code this} with the specified field set, not null
     * @throws DateTimeException if the field cannot be set
     * @throws UnsupportedTemporalTypeException if the field is not supported
     * @throws ArithmeticException if numeric overflow occurs
     */
@Override
public YearMonth with(TemporalField field, long newValue) {
    if (field instanceof ChronoField) {
        ChronoField f = (ChronoField) field;
        f.checkValidValue(newValue);
        switch(f) {
            case MONTH_OF_YEAR:
                return withMonth((int) newValue);
            case PROLEPTIC_MONTH:
                return plusMonths(newValue - getProlepticMonth());
            case YEAR_OF_ERA:
                return withYear((int) (year < 1 ? 1 - newValue : newValue));
            case YEAR:
                return withYear((int) newValue);
            case ERA:
                return (getLong(ERA) == newValue ? this : withYear(1 - year));
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.adjustInto(this, newValue);
}
Also used : ChronoField(java.time.temporal.ChronoField) UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException)

Example 19 with UnsupportedTemporalTypeException

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

the class Year method with.

/**
     * Returns a copy of this year with the specified field set to a new value.
     * <p>
     * This returns a {@code Year}, based on this one, with the value
     * for the specified field changed.
     * If it is not possible to set the value, because the field is not supported or for
     * some other reason, an exception is thrown.
     * <p>
     * If the field is a {@link ChronoField} then the adjustment is implemented here.
     * The supported fields behave as follows:
     * <ul>
     * <li>{@code YEAR_OF_ERA} -
     *  Returns a {@code Year} with the specified year-of-era
     *  The era will be unchanged.
     * <li>{@code YEAR} -
     *  Returns a {@code Year} with the specified year.
     *  This completely replaces the date and is equivalent to {@link #of(int)}.
     * <li>{@code ERA} -
     *  Returns a {@code Year} with the specified era.
     *  The year-of-era will be unchanged.
     * </ul>
     * <p>
     * In all cases, if the new value is outside the valid range of values for the field
     * then a {@code DateTimeException} will be thrown.
     * <p>
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
     * <p>
     * If the field is not a {@code ChronoField}, then the result of this method
     * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
     * passing {@code this} as the argument. In this case, the field determines
     * whether and how to adjust the instant.
     * <p>
     * This instance is immutable and unaffected by this method call.
     *
     * @param field  the field to set in the result, not null
     * @param newValue  the new value of the field in the result
     * @return a {@code Year} based on {@code this} with the specified field set, not null
     * @throws DateTimeException if the field cannot be set
     * @throws UnsupportedTemporalTypeException if the field is not supported
     * @throws ArithmeticException if numeric overflow occurs
     */
@Override
public Year with(TemporalField field, long newValue) {
    if (field instanceof ChronoField) {
        ChronoField f = (ChronoField) field;
        f.checkValidValue(newValue);
        switch(f) {
            case YEAR_OF_ERA:
                return Year.of((int) (year < 1 ? 1 - newValue : newValue));
            case YEAR:
                return Year.of((int) newValue);
            case ERA:
                return (getLong(ERA) == newValue ? this : Year.of(1 - year));
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.adjustInto(this, newValue);
}
Also used : ChronoField(java.time.temporal.ChronoField) UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException)

Example 20 with UnsupportedTemporalTypeException

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

the class JapaneseDate method getLong.

@Override
public long getLong(TemporalField field) {
    if (field instanceof ChronoField) {
        // DAY_OF_YEAR, YEAR_OF_ERA, ERA
        switch((ChronoField) field) {
            case ALIGNED_DAY_OF_WEEK_IN_MONTH:
            case ALIGNED_DAY_OF_WEEK_IN_YEAR:
            case ALIGNED_WEEK_OF_MONTH:
            case ALIGNED_WEEK_OF_YEAR:
                throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
            case YEAR_OF_ERA:
                return yearOfEra;
            case ERA:
                return era.getValue();
            case DAY_OF_YEAR:
                Calendar jcal = Calendar.getInstance(JapaneseChronology.LOCALE);
                jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET);
                jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth());
                return jcal.get(Calendar.DAY_OF_YEAR);
        }
        return isoDate.getLong(field);
    }
    return field.getFrom(this);
}
Also used : Calendar(java.util.Calendar) LocalGregorianCalendar(sun.util.calendar.LocalGregorianCalendar) ChronoField(java.time.temporal.ChronoField) UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException)

Aggregations

UnsupportedTemporalTypeException (java.time.temporal.UnsupportedTemporalTypeException)37 ChronoField (java.time.temporal.ChronoField)22 Test (org.junit.Test)6 TemporalAccessor (java.time.temporal.TemporalAccessor)4 TimeColumn (tech.tablesaw.api.TimeColumn)3 DateTimeFormatter (java.time.format.DateTimeFormatter)2 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)2 TemporalField (java.time.temporal.TemporalField)2 Calendar (java.util.Calendar)2 LocalGregorianCalendar (sun.util.calendar.LocalGregorianCalendar)2 IOException (java.io.IOException)1 Duration (java.time.Duration)1 Instant (java.time.Instant)1 YearMonth (java.time.YearMonth)1 ZoneId (java.time.ZoneId)1 ZoneOffset (java.time.ZoneOffset)1 DateTimeParseException (java.time.format.DateTimeParseException)1 ChronoUnit (java.time.temporal.ChronoUnit)1 Date (java.util.Date)1 Map (java.util.Map)1