Search in sources :

Example 11 with UnsupportedTemporalTypeException

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

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.junit.Test)

Example 12 with UnsupportedTemporalTypeException

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

the class UnsupportedTemporalTypeExceptionTest method test_constructor_message_cause.

@Test
public void test_constructor_message_cause() {
    Throwable cause = new Exception();
    UnsupportedTemporalTypeException ex = new UnsupportedTemporalTypeException("message", cause);
    assertEquals("message", ex.getMessage());
    assertSame(cause, ex.getCause());
}
Also used : UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException) UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException) Test(org.junit.Test)

Example 13 with UnsupportedTemporalTypeException

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

the class YearMonthTest method test_with_TemporalField_long_invalidField.

@Test
public void test_with_TemporalField_long_invalidField() {
    ChronoField[] invalidFields = new ChronoField[] { ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH, ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR, ChronoField.ALIGNED_WEEK_OF_MONTH, ChronoField.ALIGNED_WEEK_OF_YEAR, ChronoField.AMPM_OF_DAY, ChronoField.CLOCK_HOUR_OF_AMPM, ChronoField.CLOCK_HOUR_OF_DAY, ChronoField.DAY_OF_MONTH, ChronoField.DAY_OF_WEEK, ChronoField.DAY_OF_YEAR, ChronoField.EPOCH_DAY, ChronoField.HOUR_OF_AMPM, ChronoField.HOUR_OF_DAY, ChronoField.INSTANT_SECONDS, ChronoField.MICRO_OF_DAY, ChronoField.MICRO_OF_SECOND, ChronoField.MILLI_OF_DAY, ChronoField.MILLI_OF_SECOND, ChronoField.MINUTE_OF_DAY, ChronoField.MINUTE_OF_HOUR, ChronoField.NANO_OF_DAY, ChronoField.NANO_OF_SECOND, ChronoField.OFFSET_SECONDS, ChronoField.SECOND_OF_DAY, ChronoField.SECOND_OF_MINUTE };
    YearMonth ym = YearMonth.of(2000, Month.JANUARY);
    for (ChronoField invalidField : invalidFields) {
        // Get a valid value to ensure we fail to due invalid field, not due to invalid value.
        long value = invalidField.range().getMinimum();
        try {
            ym.with(invalidField, value);
            fail("TemporalField.with() should not accept " + invalidField);
        } catch (UnsupportedTemporalTypeException expected) {
        }
    }
}
Also used : YearMonth(java.time.YearMonth) ChronoField(java.time.temporal.ChronoField) UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException) Test(org.junit.Test)

Example 14 with UnsupportedTemporalTypeException

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

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

use of java.time.temporal.UnsupportedTemporalTypeException in project CoreNLP by stanfordnlp.

the class SUTime method parseInstant.

/**
 * Try parsing a given string into an {@link Instant} in as many ways as we know how.
 * Dates will be normalized to the start of their days.
 *
 * @param value The instant we are parsing.
 * @param timezone The timezone, if none is given in the instant.
 *
 * @return An instant corresponding to the value, if it could be parsed.
 */
public static Optional<java.time.Instant> parseInstant(String value, Optional<ZoneId> timezone) {
    for (java.time.format.DateTimeFormatter formatter : DATE_TIME_FORMATS) {
        try {
            TemporalAccessor datetime = formatter.parse(value);
            ZoneId parsedTimezone = datetime.query(TemporalQueries.zoneId());
            ZoneOffset parsedOffset = datetime.query(TemporalQueries.offset());
            if (parsedTimezone != null) {
                return Optional.of(java.time.Instant.from(datetime));
            } else if (parsedOffset != null) {
                try {
                    return Optional.of(java.time.Instant.ofEpochSecond(datetime.getLong(ChronoField.INSTANT_SECONDS)));
                } catch (UnsupportedTemporalTypeException e) {
                    return Optional.of(java.time.LocalDate.of(datetime.get(ChronoField.YEAR), datetime.get(ChronoField.MONTH_OF_YEAR), datetime.get(ChronoField.DAY_OF_MONTH)).atStartOfDay().toInstant(parsedOffset));
                }
            } else {
                if (timezone.isPresent()) {
                    java.time.Instant reference = java.time.LocalDate.of(datetime.get(ChronoField.YEAR), datetime.get(ChronoField.MONTH_OF_YEAR), datetime.get(ChronoField.DAY_OF_MONTH)).atStartOfDay().toInstant(ZoneOffset.UTC);
                    ZoneOffset currentOffsetForMyZone = timezone.get().getRules().getOffset(reference);
                    try {
                        return Optional.of(java.time.LocalDateTime.of(datetime.get(ChronoField.YEAR), datetime.get(ChronoField.MONTH_OF_YEAR), datetime.get(ChronoField.DAY_OF_MONTH), datetime.get(ChronoField.HOUR_OF_DAY), datetime.get(ChronoField.MINUTE_OF_HOUR), datetime.get(ChronoField.SECOND_OF_MINUTE)).toInstant(currentOffsetForMyZone));
                    } catch (UnsupportedTemporalTypeException e) {
                        return Optional.of(java.time.LocalDate.of(datetime.get(ChronoField.YEAR), datetime.get(ChronoField.MONTH_OF_YEAR), datetime.get(ChronoField.DAY_OF_MONTH)).atStartOfDay().toInstant(currentOffsetForMyZone));
                    }
                }
            }
        } catch (DateTimeParseException ignored) {
        }
    }
    return Optional.empty();
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) TemporalAccessor(java.time.temporal.TemporalAccessor) ZoneId(java.time.ZoneId) UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException) ZoneOffset(java.time.ZoneOffset)

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