Java Code Examples for java.time.temporal.TemporalAccessor#query()
The following examples show how to use
java.time.temporal.TemporalAccessor#query() .
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: TCKZoneIdPrinterParser.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
@Test(dataProvider="parseSuccess") public void test_parseSuccess_caseInsensitive(String text, int expectedIndex, int expectedErrorIndex, ZoneId expected) { builder.parseCaseInsensitive().appendZoneId(); String lcText = text.toLowerCase(Locale.ENGLISH); TemporalAccessor parsed = builder.toFormatter().parseUnresolved(lcText, pos); assertEquals(pos.getErrorIndex(), expectedErrorIndex, "Incorrect error index parsing: " + lcText); assertEquals(pos.getIndex(), expectedIndex, "Incorrect index parsing: " + lcText); if (expected != null) { ZoneId zid = parsed.query(TemporalQueries.zoneId()); assertEquals(parsed.query(TemporalQueries.zoneId()), expected, "Incorrect zoneId parsing: " + lcText); assertEquals(parsed.query(TemporalQueries.offset()), null, "Incorrect offset parsing: " + lcText); assertEquals(parsed.query(TemporalQueries.zone()), expected, "Incorrect zone parsing: " + lcText); } else { assertEquals(parsed, null); } }
Example 2
Source File: Period.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
/** * Validates that the temporal has the correct chronology. */ private void validateChrono(TemporalAccessor temporal) { Objects.requireNonNull(temporal, "temporal"); Chronology temporalChrono = temporal.query(TemporalQueries.chronology()); if (temporalChrono != null && IsoChronology.INSTANCE.equals(temporalChrono) == false) { throw new DateTimeException("Chronology mismatch, expected: ISO, actual: " + temporalChrono.getId()); } }
Example 3
Source File: ChronoPeriodImpl.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
/** * Validates that the temporal has the correct chronology. */ private void validateChrono(TemporalAccessor temporal) { Objects.requireNonNull(temporal, "temporal"); Chronology temporalChrono = temporal.query(TemporalQueries.chronology()); if (temporalChrono != null && chrono.equals(temporalChrono) == false) { throw new DateTimeException("Chronology mismatch, expected: " + chrono.getId() + ", actual: " + temporalChrono.getId()); } }
Example 4
Source File: ChronoPeriodImpl.java From desugar_jdk_libs with GNU General Public License v2.0 | 5 votes |
/** * Validates that the temporal has the correct chronology. */ private void validateChrono(TemporalAccessor temporal) { Objects.requireNonNull(temporal, "temporal"); Chronology temporalChrono = temporal.query(TemporalQueries.chronology()); if (temporalChrono != null && chrono.equals(temporalChrono) == false) { throw new DateTimeException("Chronology mismatch, expected: " + chrono.getId() + ", actual: " + temporalChrono.getId()); } }
Example 5
Source File: OffsetDateTime.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
/** * Obtains an instance of {@code OffsetDateTime} from a temporal object. * <p> * This obtains an offset date-time based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code OffsetDateTime}. * <p> * The conversion will first obtain a {@code ZoneOffset} from the temporal object. * It will then try to obtain a {@code LocalDateTime}, falling back to an {@code Instant} if necessary. * The result will be the combination of {@code ZoneOffset} with either * with {@code LocalDateTime} or {@code Instant}. * Implementations are permitted to perform optimizations such as accessing * those fields that are equivalent to the relevant objects. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code OffsetDateTime::from}. * * @param temporal the temporal object to convert, not null * @return the offset date-time, not null * @throws DateTimeException if unable to convert to an {@code OffsetDateTime} */ public static OffsetDateTime from(TemporalAccessor temporal) { if (temporal instanceof OffsetDateTime) { return (OffsetDateTime) temporal; } try { ZoneOffset offset = ZoneOffset.from(temporal); LocalDate date = temporal.query(TemporalQueries.localDate()); LocalTime time = temporal.query(TemporalQueries.localTime()); if (date != null && time != null) { return OffsetDateTime.of(date, time, offset); } else { Instant instant = Instant.from(temporal); return OffsetDateTime.ofInstant(instant, offset); } } catch (DateTimeException ex) { throw new DateTimeException("Unable to obtain OffsetDateTime from TemporalAccessor: " + temporal + " of type " + temporal.getClass().getName(), ex); } }
Example 6
Source File: OffsetDateTime.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
/** * Obtains an instance of {@code OffsetDateTime} from a temporal object. * <p> * This obtains an offset date-time based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code OffsetDateTime}. * <p> * The conversion will first obtain a {@code ZoneOffset} from the temporal object. * It will then try to obtain a {@code LocalDateTime}, falling back to an {@code Instant} if necessary. * The result will be the combination of {@code ZoneOffset} with either * with {@code LocalDateTime} or {@code Instant}. * Implementations are permitted to perform optimizations such as accessing * those fields that are equivalent to the relevant objects. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code OffsetDateTime::from}. * * @param temporal the temporal object to convert, not null * @return the offset date-time, not null * @throws DateTimeException if unable to convert to an {@code OffsetDateTime} */ public static OffsetDateTime from(TemporalAccessor temporal) { if (temporal instanceof OffsetDateTime) { return (OffsetDateTime) temporal; } try { ZoneOffset offset = ZoneOffset.from(temporal); LocalDate date = temporal.query(TemporalQueries.localDate()); LocalTime time = temporal.query(TemporalQueries.localTime()); if (date != null && time != null) { return OffsetDateTime.of(date, time, offset); } else { Instant instant = Instant.from(temporal); return OffsetDateTime.ofInstant(instant, offset); } } catch (DateTimeException ex) { throw new DateTimeException("Unable to obtain OffsetDateTime from TemporalAccessor: " + temporal + " of type " + temporal.getClass().getName(), ex); } }
Example 7
Source File: LocalDate.java From jdk1.8-source-analysis with Apache License 2.0 | 3 votes |
/** * Obtains an instance of {@code LocalDate} from a temporal object. * <p> * This obtains a local date based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code LocalDate}. * <p> * The conversion uses the {@link TemporalQueries#localDate()} query, which relies * on extracting the {@link ChronoField#EPOCH_DAY EPOCH_DAY} field. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code LocalDate::from}. * * @param temporal the temporal object to convert, not null * @return the local date, not null * @throws DateTimeException if unable to convert to a {@code LocalDate} */ public static LocalDate from(TemporalAccessor temporal) { Objects.requireNonNull(temporal, "temporal"); LocalDate date = temporal.query(TemporalQueries.localDate()); if (date == null) { throw new DateTimeException("Unable to obtain LocalDate from TemporalAccessor: " + temporal + " of type " + temporal.getClass().getName()); } return date; }
Example 8
Source File: ChronoLocalDateTime.java From TencentKona-8 with GNU General Public License v2.0 | 3 votes |
/** * Obtains an instance of {@code ChronoLocalDateTime} from a temporal object. * <p> * This obtains a local date-time based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code ChronoLocalDateTime}. * <p> * The conversion extracts and combines the chronology and the date-time * from the temporal object. The behavior is equivalent to using * {@link Chronology#localDateTime(TemporalAccessor)} with the extracted chronology. * Implementations are permitted to perform optimizations such as accessing * those fields that are equivalent to the relevant objects. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code ChronoLocalDateTime::from}. * * @param temporal the temporal object to convert, not null * @return the date-time, not null * @throws DateTimeException if unable to convert to a {@code ChronoLocalDateTime} * @see Chronology#localDateTime(TemporalAccessor) */ static ChronoLocalDateTime<?> from(TemporalAccessor temporal) { if (temporal instanceof ChronoLocalDateTime) { return (ChronoLocalDateTime<?>) temporal; } Objects.requireNonNull(temporal, "temporal"); Chronology chrono = temporal.query(TemporalQueries.chronology()); if (chrono == null) { throw new DateTimeException("Unable to obtain ChronoLocalDateTime from TemporalAccessor: " + temporal.getClass()); } return chrono.localDateTime(temporal); }
Example 9
Source File: ZoneOffset.java From jdk8u60 with GNU General Public License v2.0 | 3 votes |
/** * Obtains an instance of {@code ZoneOffset} from a temporal object. * <p> * This obtains an offset based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code ZoneOffset}. * <p> * A {@code TemporalAccessor} represents some form of date and time information. * This factory converts the arbitrary temporal object to an instance of {@code ZoneOffset}. * <p> * The conversion uses the {@link TemporalQueries#offset()} query, which relies * on extracting the {@link ChronoField#OFFSET_SECONDS OFFSET_SECONDS} field. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code ZoneOffset::from}. * * @param temporal the temporal object to convert, not null * @return the zone-offset, not null * @throws DateTimeException if unable to convert to an {@code ZoneOffset} */ public static ZoneOffset from(TemporalAccessor temporal) { Objects.requireNonNull(temporal, "temporal"); ZoneOffset offset = temporal.query(TemporalQueries.offset()); if (offset == null) { throw new DateTimeException("Unable to obtain ZoneOffset from TemporalAccessor: " + temporal + " of type " + temporal.getClass().getName()); } return offset; }
Example 10
Source File: ChronoZonedDateTime.java From desugar_jdk_libs with GNU General Public License v2.0 | 3 votes |
/** * Obtains an instance of {@code ChronoZonedDateTime} from a temporal object. * <p> * This creates a zoned date-time based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code ChronoZonedDateTime}. * <p> * The conversion extracts and combines the chronology, date, time and zone * from the temporal object. The behavior is equivalent to using * {@link Chronology#zonedDateTime(TemporalAccessor)} with the extracted chronology. * Implementations are permitted to perform optimizations such as accessing * those fields that are equivalent to the relevant objects. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code ChronoZonedDateTime::from}. * * @param temporal the temporal object to convert, not null * @return the date-time, not null * @throws DateTimeException if unable to convert to a {@code ChronoZonedDateTime} * @see Chronology#zonedDateTime(TemporalAccessor) */ static ChronoZonedDateTime<?> from(TemporalAccessor temporal) { if (temporal instanceof ChronoZonedDateTime) { return (ChronoZonedDateTime<?>) temporal; } Objects.requireNonNull(temporal, "temporal"); Chronology chrono = temporal.query(TemporalQueries.chronology()); if (chrono == null) { throw new DateTimeException("Unable to obtain ChronoZonedDateTime from TemporalAccessor: " + temporal.getClass()); } return chrono.zonedDateTime(temporal); }
Example 11
Source File: LocalTime.java From openjdk-jdk8u with GNU General Public License v2.0 | 3 votes |
/** * Obtains an instance of {@code LocalTime} from a temporal object. * <p> * This obtains a local time based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code LocalTime}. * <p> * The conversion uses the {@link TemporalQueries#localTime()} query, which relies * on extracting the {@link ChronoField#NANO_OF_DAY NANO_OF_DAY} field. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code LocalTime::from}. * * @param temporal the temporal object to convert, not null * @return the local time, not null * @throws DateTimeException if unable to convert to a {@code LocalTime} */ public static LocalTime from(TemporalAccessor temporal) { Objects.requireNonNull(temporal, "temporal"); LocalTime time = temporal.query(TemporalQueries.localTime()); if (time == null) { throw new DateTimeException("Unable to obtain LocalTime from TemporalAccessor: " + temporal + " of type " + temporal.getClass().getName()); } return time; }
Example 12
Source File: ZoneOffset.java From openjdk-jdk9 with GNU General Public License v2.0 | 3 votes |
/** * Obtains an instance of {@code ZoneOffset} from a temporal object. * <p> * This obtains an offset based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code ZoneOffset}. * <p> * A {@code TemporalAccessor} represents some form of date and time information. * This factory converts the arbitrary temporal object to an instance of {@code ZoneOffset}. * <p> * The conversion uses the {@link TemporalQueries#offset()} query, which relies * on extracting the {@link ChronoField#OFFSET_SECONDS OFFSET_SECONDS} field. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code ZoneOffset::from}. * * @param temporal the temporal object to convert, not null * @return the zone-offset, not null * @throws DateTimeException if unable to convert to an {@code ZoneOffset} */ public static ZoneOffset from(TemporalAccessor temporal) { Objects.requireNonNull(temporal, "temporal"); ZoneOffset offset = temporal.query(TemporalQueries.offset()); if (offset == null) { throw new DateTimeException("Unable to obtain ZoneOffset from TemporalAccessor: " + temporal + " of type " + temporal.getClass().getName()); } return offset; }
Example 13
Source File: LocalDate.java From openjdk-jdk8u with GNU General Public License v2.0 | 3 votes |
/** * Obtains an instance of {@code LocalDate} from a temporal object. * <p> * This obtains a local date based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code LocalDate}. * <p> * The conversion uses the {@link TemporalQueries#localDate()} query, which relies * on extracting the {@link ChronoField#EPOCH_DAY EPOCH_DAY} field. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code LocalDate::from}. * * @param temporal the temporal object to convert, not null * @return the local date, not null * @throws DateTimeException if unable to convert to a {@code LocalDate} */ public static LocalDate from(TemporalAccessor temporal) { Objects.requireNonNull(temporal, "temporal"); LocalDate date = temporal.query(TemporalQueries.localDate()); if (date == null) { throw new DateTimeException("Unable to obtain LocalDate from TemporalAccessor: " + temporal + " of type " + temporal.getClass().getName()); } return date; }
Example 14
Source File: LocalTime.java From dragonwell8_jdk with GNU General Public License v2.0 | 3 votes |
/** * Obtains an instance of {@code LocalTime} from a temporal object. * <p> * This obtains a local time based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code LocalTime}. * <p> * The conversion uses the {@link TemporalQueries#localTime()} query, which relies * on extracting the {@link ChronoField#NANO_OF_DAY NANO_OF_DAY} field. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code LocalTime::from}. * * @param temporal the temporal object to convert, not null * @return the local time, not null * @throws DateTimeException if unable to convert to a {@code LocalTime} */ public static LocalTime from(TemporalAccessor temporal) { Objects.requireNonNull(temporal, "temporal"); LocalTime time = temporal.query(TemporalQueries.localTime()); if (time == null) { throw new DateTimeException("Unable to obtain LocalTime from TemporalAccessor: " + temporal + " of type " + temporal.getClass().getName()); } return time; }
Example 15
Source File: ChronoLocalDateTime.java From JDKSourceCode1.8 with MIT License | 3 votes |
/** * Obtains an instance of {@code ChronoLocalDateTime} from a temporal object. * <p> * This obtains a local date-time based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code ChronoLocalDateTime}. * <p> * The conversion extracts and combines the chronology and the date-time * from the temporal object. The behavior is equivalent to using * {@link Chronology#localDateTime(TemporalAccessor)} with the extracted chronology. * Implementations are permitted to perform optimizations such as accessing * those fields that are equivalent to the relevant objects. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code ChronoLocalDateTime::from}. * * @param temporal the temporal object to convert, not null * @return the date-time, not null * @throws DateTimeException if unable to convert to a {@code ChronoLocalDateTime} * @see Chronology#localDateTime(TemporalAccessor) */ static ChronoLocalDateTime<?> from(TemporalAccessor temporal) { if (temporal instanceof ChronoLocalDateTime) { return (ChronoLocalDateTime<?>) temporal; } Objects.requireNonNull(temporal, "temporal"); Chronology chrono = temporal.query(TemporalQueries.chronology()); if (chrono == null) { throw new DateTimeException("Unable to obtain ChronoLocalDateTime from TemporalAccessor: " + temporal.getClass()); } return chrono.localDateTime(temporal); }
Example 16
Source File: ChronoLocalDate.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 3 votes |
/** * Obtains an instance of {@code ChronoLocalDate} from a temporal object. * <p> * This obtains a local date based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code ChronoLocalDate}. * <p> * The conversion extracts and combines the chronology and the date * from the temporal object. The behavior is equivalent to using * {@link Chronology#date(TemporalAccessor)} with the extracted chronology. * Implementations are permitted to perform optimizations such as accessing * those fields that are equivalent to the relevant objects. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code ChronoLocalDate::from}. * * @param temporal the temporal object to convert, not null * @return the date, not null * @throws DateTimeException if unable to convert to a {@code ChronoLocalDate} * @see Chronology#date(TemporalAccessor) */ static ChronoLocalDate from(TemporalAccessor temporal) { if (temporal instanceof ChronoLocalDate) { return (ChronoLocalDate) temporal; } Objects.requireNonNull(temporal, "temporal"); Chronology chrono = temporal.query(TemporalQueries.chronology()); if (chrono == null) { throw new DateTimeException("Unable to obtain ChronoLocalDate from TemporalAccessor: " + temporal.getClass()); } return chrono.date(temporal); }
Example 17
Source File: ChronoLocalDate.java From TencentKona-8 with GNU General Public License v2.0 | 3 votes |
/** * Obtains an instance of {@code ChronoLocalDate} from a temporal object. * <p> * This obtains a local date based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code ChronoLocalDate}. * <p> * The conversion extracts and combines the chronology and the date * from the temporal object. The behavior is equivalent to using * {@link Chronology#date(TemporalAccessor)} with the extracted chronology. * Implementations are permitted to perform optimizations such as accessing * those fields that are equivalent to the relevant objects. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code ChronoLocalDate::from}. * * @param temporal the temporal object to convert, not null * @return the date, not null * @throws DateTimeException if unable to convert to a {@code ChronoLocalDate} * @see Chronology#date(TemporalAccessor) */ static ChronoLocalDate from(TemporalAccessor temporal) { if (temporal instanceof ChronoLocalDate) { return (ChronoLocalDate) temporal; } Objects.requireNonNull(temporal, "temporal"); Chronology chrono = temporal.query(TemporalQueries.chronology()); if (chrono == null) { throw new DateTimeException("Unable to obtain ChronoLocalDate from TemporalAccessor: " + temporal.getClass()); } return chrono.date(temporal); }
Example 18
Source File: Chronology.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 2 votes |
/** * Obtains an instance of {@code Chronology} from a temporal object. * <p> * This obtains a chronology based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code Chronology}. * <p> * The conversion will obtain the chronology using {@link TemporalQueries#chronology()}. * If the specified temporal object does not have a chronology, {@link IsoChronology} is returned. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code Chronology::from}. * * @param temporal the temporal to convert, not null * @return the chronology, not null * @throws DateTimeException if unable to convert to an {@code Chronology} */ static Chronology from(TemporalAccessor temporal) { Objects.requireNonNull(temporal, "temporal"); Chronology obj = temporal.query(TemporalQueries.chronology()); return (obj != null ? obj : IsoChronology.INSTANCE); }
Example 19
Source File: Chronology.java From dragonwell8_jdk with GNU General Public License v2.0 | 2 votes |
/** * Obtains an instance of {@code Chronology} from a temporal object. * <p> * This obtains a chronology based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code Chronology}. * <p> * The conversion will obtain the chronology using {@link TemporalQueries#chronology()}. * If the specified temporal object does not have a chronology, {@link IsoChronology} is returned. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code Chronology::from}. * * @param temporal the temporal to convert, not null * @return the chronology, not null * @throws DateTimeException if unable to convert to an {@code Chronology} */ static Chronology from(TemporalAccessor temporal) { Objects.requireNonNull(temporal, "temporal"); Chronology obj = temporal.query(TemporalQueries.chronology()); return (obj != null ? obj : IsoChronology.INSTANCE); }
Example 20
Source File: Chronology.java From Bytecoder with Apache License 2.0 | 2 votes |
/** * Obtains an instance of {@code Chronology} from a temporal object. * <p> * This obtains a chronology based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code Chronology}. * <p> * The conversion will obtain the chronology using {@link TemporalQueries#chronology()}. * If the specified temporal object does not have a chronology, {@link IsoChronology} is returned. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code Chronology::from}. * * @param temporal the temporal to convert, not null * @return the chronology, not null * @throws DateTimeException if unable to convert to a {@code Chronology} */ static Chronology from(TemporalAccessor temporal) { Objects.requireNonNull(temporal, "temporal"); Chronology obj = temporal.query(TemporalQueries.chronology()); return Objects.requireNonNullElse(obj, IsoChronology.INSTANCE); }