예제 #1
0
 /**
  * Queries this offset using the specified query.
  *
  * <p>This queries this offset using the specified query strategy object. The {@code
  * TemporalQuery} object defines the logic to be used to obtain the result. Read the documentation
  * of the query to understand what the result of this method will be.
  *
  * <p>The result of this method is obtained by invoking the {@link
  * TemporalQuery#queryFrom(TemporalAccessor)} method on the specified query passing {@code this}
  * as the argument.
  *
  * @param <R> the type of the result
  * @param query the query to invoke, not null
  * @return the query result, null may be returned (defined by the query)
  * @throws DateTimeException if unable to query (defined by the query)
  * @throws ArithmeticException if numeric overflow occurs (defined by the query)
  */
 @SuppressWarnings("unchecked")
 @Override
 public <R> R query(TemporalQuery<R> query) {
   if (query == TemporalQueries.offset() || query == TemporalQueries.zone()) {
     return (R) this;
   }
   return TemporalAccessor.super.query(query);
 }
예제 #2
0
 /**
  * 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 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) {
   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;
 }