Search in sources :

Example 21 with UnsupportedTemporalTypeException

use of java.time.temporal.UnsupportedTemporalTypeException in project Bytecoder by mirkosertic.

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 22 with UnsupportedTemporalTypeException

use of java.time.temporal.UnsupportedTemporalTypeException in project Bytecoder by mirkosertic.

the class LocalDate method with.

/**
 * Returns a copy of this date with the specified field set to a new value.
 * <p>
 * This returns a {@code LocalDate}, 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, month or day-of-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>
 * In some cases, changing the specified field can cause the resulting date to become invalid,
 * such as changing the month from 31st January to February would make the day-of-month invalid.
 * In cases like this, the field is responsible for resolving the date. Typically it will choose
 * the previous valid date, which would be the last valid day of February in this example.
 * <p>
 * If the field is a {@link ChronoField} then the adjustment is implemented here.
 * The supported fields behave as follows:
 * <ul>
 * <li>{@code DAY_OF_WEEK} -
 *  Returns a {@code LocalDate} with the specified day-of-week.
 *  The date is adjusted up to 6 days forward or backward within the boundary
 *  of a Monday to Sunday week.
 * <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH} -
 *  Returns a {@code LocalDate} with the specified aligned-day-of-week.
 *  The date is adjusted to the specified month-based aligned-day-of-week.
 *  Aligned weeks are counted such that the first week of a given month starts
 *  on the first day of that month.
 *  This may cause the date to be moved up to 6 days into the following month.
 * <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR} -
 *  Returns a {@code LocalDate} with the specified aligned-day-of-week.
 *  The date is adjusted to the specified year-based aligned-day-of-week.
 *  Aligned weeks are counted such that the first week of a given year starts
 *  on the first day of that year.
 *  This may cause the date to be moved up to 6 days into the following year.
 * <li>{@code DAY_OF_MONTH} -
 *  Returns a {@code LocalDate} with the specified day-of-month.
 *  The month and year will be unchanged. If the day-of-month is invalid for the
 *  year and month, then a {@code DateTimeException} is thrown.
 * <li>{@code DAY_OF_YEAR} -
 *  Returns a {@code LocalDate} with the specified day-of-year.
 *  The year will be unchanged. If the day-of-year is invalid for the
 *  year, then a {@code DateTimeException} is thrown.
 * <li>{@code EPOCH_DAY} -
 *  Returns a {@code LocalDate} with the specified epoch-day.
 *  This completely replaces the date and is equivalent to {@link #ofEpochDay(long)}.
 * <li>{@code ALIGNED_WEEK_OF_MONTH} -
 *  Returns a {@code LocalDate} with the specified aligned-week-of-month.
 *  Aligned weeks are counted such that the first week of a given month starts
 *  on the first day of that month.
 *  This adjustment moves the date in whole week chunks to match the specified week.
 *  The result will have the same day-of-week as this date.
 *  This may cause the date to be moved into the following month.
 * <li>{@code ALIGNED_WEEK_OF_YEAR} -
 *  Returns a {@code LocalDate} with the specified aligned-week-of-year.
 *  Aligned weeks are counted such that the first week of a given year starts
 *  on the first day of that year.
 *  This adjustment moves the date in whole week chunks to match the specified week.
 *  The result will have the same day-of-week as this date.
 *  This may cause the date to be moved into the following year.
 * <li>{@code MONTH_OF_YEAR} -
 *  Returns a {@code LocalDate} with the specified month-of-year.
 *  The year will be unchanged. The day-of-month will also be unchanged,
 *  unless it would be invalid for the new month and year. In that case, the
 *  day-of-month is adjusted to the maximum valid value for the new month and year.
 * <li>{@code PROLEPTIC_MONTH} -
 *  Returns a {@code LocalDate} with the specified proleptic-month.
 *  The day-of-month will be unchanged, unless it would be invalid for the new month
 *  and year. In that case, the day-of-month is adjusted to the maximum valid value
 *  for the new month and year.
 * <li>{@code YEAR_OF_ERA} -
 *  Returns a {@code LocalDate} with the specified year-of-era.
 *  The era and month will be unchanged. The day-of-month will also be unchanged,
 *  unless it would be invalid for the new month and year. In that case, the
 *  day-of-month is adjusted to the maximum valid value for the new month and year.
 * <li>{@code YEAR} -
 *  Returns a {@code LocalDate} with the specified year.
 *  The month will be unchanged. The day-of-month will also be unchanged,
 *  unless it would be invalid for the new month and year. In that case, the
 *  day-of-month is adjusted to the maximum valid value for the new month and year.
 * <li>{@code ERA} -
 *  Returns a {@code LocalDate} with the specified era.
 *  The year-of-era and month will be unchanged. The day-of-month will also be unchanged,
 *  unless it would be invalid for the new month and year. In that case, the
 *  day-of-month is adjusted to the maximum valid value for the new month and year.
 * </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 LocalDate} 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 LocalDate with(TemporalField field, long newValue) {
    if (field instanceof ChronoField) {
        ChronoField f = (ChronoField) field;
        f.checkValidValue(newValue);
        switch(f) {
            case DAY_OF_WEEK:
                return plusDays(newValue - getDayOfWeek().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 withDayOfMonth((int) newValue);
            case DAY_OF_YEAR:
                return withDayOfYear((int) newValue);
            case EPOCH_DAY:
                return LocalDate.ofEpochDay(newValue);
            case ALIGNED_WEEK_OF_MONTH:
                return plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_MONTH));
            case ALIGNED_WEEK_OF_YEAR:
                return plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_YEAR));
            case MONTH_OF_YEAR:
                return withMonth((int) newValue);
            case PROLEPTIC_MONTH:
                return plusMonths(newValue - getProlepticMonth());
            case YEAR_OF_ERA:
                return withYear((int) (year >= 1 ? newValue : 1 - 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 23 with UnsupportedTemporalTypeException

use of java.time.temporal.UnsupportedTemporalTypeException in project Bytecoder by mirkosertic.

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 24 with UnsupportedTemporalTypeException

use of java.time.temporal.UnsupportedTemporalTypeException in project Bytecoder by mirkosertic.

the class HijrahDate method with.

@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
        // TODO: validate value
        chrono.range(f).checkValidValue(newValue, f);
        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);
}
Also used : ChronoField(java.time.temporal.ChronoField) UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException)

Example 25 with UnsupportedTemporalTypeException

use of java.time.temporal.UnsupportedTemporalTypeException in project j2objc by google.

the class OffsetTimeTest method test_plus_minus_invalidUnits.

@Test
public void test_plus_minus_invalidUnits() {
    for (ChronoUnit unit : EnumSet.range(ChronoUnit.DAYS, ChronoUnit.FOREVER)) {
        try {
            NOON_UTC.plus(1, unit);
            fail("Adding 1 " + unit + " should have failed.");
        } catch (UnsupportedTemporalTypeException expected) {
        }
        try {
            NOON_UTC.minus(1, unit);
            fail("Subtracting 1 " + unit + " should have failed.");
        } catch (UnsupportedTemporalTypeException expected) {
        }
    }
}
Also used : UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException) ChronoUnit(java.time.temporal.ChronoUnit) Test(org.junit.Test)

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