Java Code Examples for org.threeten.bp.temporal.TemporalAccessor#query()

The following examples show how to use org.threeten.bp.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: LocalTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Obtains an instance of {@code LocalTime} from a temporal object.
 * <p>
 * A {@code TemporalAccessor} represents some form of date and time information.
 * This factory converts the arbitrary temporal object 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 in queries 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) {
    LocalTime time = temporal.query(TemporalQueries.localTime());
    if (time == null) {
        throw new DateTimeException("Unable to obtain LocalTime from TemporalAccessor: " +
                temporal + ", type " + temporal.getClass().getName());
    }
    return time;
}
 
Example 2
Source File: LocalDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Obtains an instance of {@code LocalDate} from a temporal object.
 * <p>
 * A {@code TemporalAccessor} represents some form of date and time information.
 * This factory converts the arbitrary temporal object 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) {
    LocalDate date = temporal.query(TemporalQueries.localDate());
    if (date == null) {
        throw new DateTimeException("Unable to obtain LocalDate from TemporalAccessor: " +
                temporal + ", type " + temporal.getClass().getName());
    }
    return date;
}
 
Example 3
Source File: ZoneId.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Obtains an instance of {@code ZoneId} from a temporal object.
 * <p>
 * A {@code TemporalAccessor} represents some form of date and time information.
 * This factory converts the arbitrary temporal object to an instance of {@code ZoneId}.
 * <p>
 * The conversion will try to obtain the zone in a way that favours region-based
 * zones over offset-based zones using {@link TemporalQueries#zone()}.
 * <p>
 * This method matches the signature of the functional interface {@link TemporalQuery}
 * allowing it to be used in queries via method reference, {@code ZoneId::from}.
 *
 * @param temporal  the temporal object to convert, not null
 * @return the zone ID, not null
 * @throws DateTimeException if unable to convert to a {@code ZoneId}
 */
public static ZoneId from(TemporalAccessor temporal) {
    ZoneId obj = temporal.query(TemporalQueries.zone());
    if (obj == null) {
        throw new DateTimeException("Unable to obtain ZoneId from TemporalAccessor: " +
                temporal + ", type " + temporal.getClass().getName());
    }
    return obj;
}
 
Example 4
Source File: ChronoZonedDateTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * 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)
 */
public static ChronoZonedDateTime<?> from(TemporalAccessor temporal) {
    Jdk8Methods.requireNonNull(temporal, "temporal");
    if (temporal instanceof ChronoZonedDateTime) {
        return (ChronoZonedDateTime<?>) temporal;
    }
    Chronology chrono = temporal.query(TemporalQueries.chronology());
    if (chrono == null) {
        throw new DateTimeException("No Chronology found to create ChronoZonedDateTime: " + temporal.getClass());
    }
    return chrono.zonedDateTime(temporal);
}
 
Example 5
Source File: ChronoLocalDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * 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)
 */
public static ChronoLocalDate from(TemporalAccessor temporal) {
    Jdk8Methods.requireNonNull(temporal, "temporal");
    if (temporal instanceof ChronoLocalDate) {
        return (ChronoLocalDate) temporal;
    }
    Chronology chrono = temporal.query(TemporalQueries.chronology());
    if (chrono == null) {
        throw new DateTimeException("No Chronology found to create ChronoLocalDate: " + temporal.getClass());
    }
    return chrono.date(temporal);
}
 
Example 6
Source File: ChronoLocalDateTime.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * 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)
 */
public static ChronoLocalDateTime<?> from(TemporalAccessor temporal) {
    Jdk8Methods.requireNonNull(temporal, "temporal");
    if (temporal instanceof ChronoLocalDateTime) {
        return (ChronoLocalDateTime<?>) temporal;
    }
    Chronology chrono = temporal.query(TemporalQueries.chronology());
    if (chrono == null) {
        throw new DateTimeException("No Chronology found to create ChronoLocalDateTime: " + temporal.getClass());
    }
    return chrono.localDateTime(temporal);
}
 
Example 7
Source File: ZoneOffset.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Obtains an instance of {@code ZoneOffset} from a temporal object.
 * <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 in queries 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) {
    ZoneOffset offset = temporal.query(TemporalQueries.offset());
    if (offset == null) {
        throw new DateTimeException("Unable to obtain ZoneOffset from TemporalAccessor: " +
                temporal + ", type " + temporal.getClass().getName());
    }
    return offset;
}
 
Example 8
Source File: Chronology.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Obtains an instance of {@code Chronology} from a temporal object.
 * <p>
 * A {@code TemporalAccessor} represents some form of date and time information.
 * This factory converts the arbitrary temporal object to an instance of {@code Chronology}.
 * If the specified temporal object does not have a chronology, {@link IsoChronology} is returned.
 * <p>
 * The conversion will obtain the chronology using {@link TemporalQueries#chronology()}.
 * <p>
 * This method matches the signature of the functional interface {@link TemporalQuery}
 * allowing it to be used in queries via method reference, {@code Chrono::from}.
 *
 * @param temporal  the temporal to convert, not null
 * @return the chronology, not null
 * @throws DateTimeException if unable to convert to an {@code Chronology}
 */
public static Chronology from(TemporalAccessor temporal) {
    Jdk8Methods.requireNonNull(temporal, "temporal");
    Chronology obj = temporal.query(TemporalQueries.chronology());
    return (obj != null ? obj : IsoChronology.INSTANCE);
}