src/share/classes/java/time/ZoneOffset.java

Print this page




  44  *    and/or other materials provided with the distribution.
  45  *
  46  *  * Neither the name of JSR-310 nor the names of its contributors
  47  *    may be used to endorse or promote products derived from this software
  48  *    without specific prior written permission.
  49  *
  50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  54  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  55  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  56  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  57  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  58  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  59  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  60  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61  */
  62 package java.time;
  63 
  64 import java.time.temporal.UnsupportedTemporalTypeException;
  65 import static java.time.LocalTime.MINUTES_PER_HOUR;
  66 import static java.time.LocalTime.SECONDS_PER_HOUR;
  67 import static java.time.LocalTime.SECONDS_PER_MINUTE;
  68 import static java.time.temporal.ChronoField.OFFSET_SECONDS;
  69 
  70 import java.io.DataInput;
  71 import java.io.DataOutput;
  72 import java.io.IOException;
  73 import java.io.InvalidObjectException;
  74 import java.io.ObjectStreamException;
  75 import java.io.Serializable;
  76 import java.time.temporal.ChronoField;
  77 import java.time.temporal.Temporal;
  78 import java.time.temporal.TemporalAccessor;
  79 import java.time.temporal.TemporalAdjuster;
  80 import java.time.temporal.TemporalField;
  81 import java.time.temporal.TemporalQuery;

  82 import java.time.temporal.ValueRange;
  83 import java.time.zone.ZoneRules;
  84 import java.util.Objects;
  85 import java.util.concurrent.ConcurrentHashMap;
  86 import java.util.concurrent.ConcurrentMap;
  87 
  88 /**
  89  * A time-zone offset from Greenwich/UTC, such as {@code +02:00}.
  90  * <p>
  91  * A time-zone offset is the period of time that a time-zone differs from Greenwich/UTC.
  92  * This is usually a fixed number of hours and minutes.
  93  * <p>
  94  * Different parts of the world have different time-zone offsets.
  95  * The rules for how offsets vary by place and time of year are captured in the
  96  * {@link ZoneId} class.
  97  * <p>
  98  * For example, Paris is one hour ahead of Greenwich/UTC in winter and two hours
  99  * ahead in summer. The {@code ZoneId} instance for Paris will reference two
 100  * {@code ZoneOffset} instances - a {@code +01:00} instance for winter,
 101  * and a {@code +02:00} instance for summer.


 564      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 565      * <p>
 566      * If the field is not a {@code ChronoField}, then the result of this method
 567      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 568      * passing {@code this} as the argument. Whether the value can be obtained,
 569      * and what the value represents, is determined by the field.
 570      *
 571      * @param field  the field to get, not null
 572      * @return the value for the field
 573      * @throws DateTimeException if a value for the field cannot be obtained or
 574      *         the value is outside the range of valid values for the field
 575      * @throws UnsupportedTemporalTypeException if the field is not supported or
 576      *         the range of values exceeds an {@code int}
 577      * @throws ArithmeticException if numeric overflow occurs
 578      */
 579     @Override  // override for Javadoc and performance
 580     public int get(TemporalField field) {
 581         if (field == OFFSET_SECONDS) {
 582             return totalSeconds;
 583         } else if (field instanceof ChronoField) {
 584             throw new UnsupportedTemporalTypeException("Unsupported field: " + field.getName());
 585         }
 586         return range(field).checkValidIntValue(getLong(field), field);
 587     }
 588 
 589     /**
 590      * Gets the value of the specified field from this offset as a {@code long}.
 591      * <p>
 592      * This queries this offset for the value for the specified field.
 593      * If it is not possible to return the value, because the field is not supported
 594      * or for some other reason, an exception is thrown.
 595      * <p>
 596      * If the field is a {@link ChronoField} then the query is implemented here.
 597      * The {@code OFFSET_SECONDS} field returns the value of the offset.
 598      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 599      * <p>
 600      * If the field is not a {@code ChronoField}, then the result of this method
 601      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 602      * passing {@code this} as the argument. Whether the value can be obtained,
 603      * and what the value represents, is determined by the field.
 604      *
 605      * @param field  the field to get, not null
 606      * @return the value for the field
 607      * @throws DateTimeException if a value for the field cannot be obtained
 608      * @throws UnsupportedTemporalTypeException if the field is not supported
 609      * @throws ArithmeticException if numeric overflow occurs
 610      */
 611     @Override
 612     public long getLong(TemporalField field) {
 613         if (field == OFFSET_SECONDS) {
 614             return totalSeconds;
 615         } else if (field instanceof ChronoField) {
 616             throw new UnsupportedTemporalTypeException("Unsupported field: " + field.getName());
 617         }
 618         return field.getFrom(this);
 619     }
 620 
 621     //-----------------------------------------------------------------------
 622     /**
 623      * Queries this offset using the specified query.
 624      * <p>
 625      * This queries this offset using the specified query strategy object.
 626      * The {@code TemporalQuery} object defines the logic to be used to
 627      * obtain the result. Read the documentation of the query to understand
 628      * what the result of this method will be.
 629      * <p>
 630      * The result of this method is obtained by invoking the
 631      * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
 632      * specified query passing {@code this} as the argument.
 633      *
 634      * @param <R> the type of the result
 635      * @param query  the query to invoke, not null
 636      * @return the query result, null may be returned (defined by the query)




  44  *    and/or other materials provided with the distribution.
  45  *
  46  *  * Neither the name of JSR-310 nor the names of its contributors
  47  *    may be used to endorse or promote products derived from this software
  48  *    without specific prior written permission.
  49  *
  50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  54  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  55  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  56  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  57  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  58  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  59  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  60  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61  */
  62 package java.time;
  63 

  64 import static java.time.LocalTime.MINUTES_PER_HOUR;
  65 import static java.time.LocalTime.SECONDS_PER_HOUR;
  66 import static java.time.LocalTime.SECONDS_PER_MINUTE;
  67 import static java.time.temporal.ChronoField.OFFSET_SECONDS;
  68 
  69 import java.io.DataInput;
  70 import java.io.DataOutput;
  71 import java.io.IOException;
  72 import java.io.InvalidObjectException;
  73 import java.io.ObjectStreamException;
  74 import java.io.Serializable;
  75 import java.time.temporal.ChronoField;
  76 import java.time.temporal.Temporal;
  77 import java.time.temporal.TemporalAccessor;
  78 import java.time.temporal.TemporalAdjuster;
  79 import java.time.temporal.TemporalField;
  80 import java.time.temporal.TemporalQuery;
  81 import java.time.temporal.UnsupportedTemporalTypeException;
  82 import java.time.temporal.ValueRange;
  83 import java.time.zone.ZoneRules;
  84 import java.util.Objects;
  85 import java.util.concurrent.ConcurrentHashMap;
  86 import java.util.concurrent.ConcurrentMap;
  87 
  88 /**
  89  * A time-zone offset from Greenwich/UTC, such as {@code +02:00}.
  90  * <p>
  91  * A time-zone offset is the period of time that a time-zone differs from Greenwich/UTC.
  92  * This is usually a fixed number of hours and minutes.
  93  * <p>
  94  * Different parts of the world have different time-zone offsets.
  95  * The rules for how offsets vary by place and time of year are captured in the
  96  * {@link ZoneId} class.
  97  * <p>
  98  * For example, Paris is one hour ahead of Greenwich/UTC in winter and two hours
  99  * ahead in summer. The {@code ZoneId} instance for Paris will reference two
 100  * {@code ZoneOffset} instances - a {@code +01:00} instance for winter,
 101  * and a {@code +02:00} instance for summer.


 564      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 565      * <p>
 566      * If the field is not a {@code ChronoField}, then the result of this method
 567      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 568      * passing {@code this} as the argument. Whether the value can be obtained,
 569      * and what the value represents, is determined by the field.
 570      *
 571      * @param field  the field to get, not null
 572      * @return the value for the field
 573      * @throws DateTimeException if a value for the field cannot be obtained or
 574      *         the value is outside the range of valid values for the field
 575      * @throws UnsupportedTemporalTypeException if the field is not supported or
 576      *         the range of values exceeds an {@code int}
 577      * @throws ArithmeticException if numeric overflow occurs
 578      */
 579     @Override  // override for Javadoc and performance
 580     public int get(TemporalField field) {
 581         if (field == OFFSET_SECONDS) {
 582             return totalSeconds;
 583         } else if (field instanceof ChronoField) {
 584             throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
 585         }
 586         return range(field).checkValidIntValue(getLong(field), field);
 587     }
 588 
 589     /**
 590      * Gets the value of the specified field from this offset as a {@code long}.
 591      * <p>
 592      * This queries this offset for the value for the specified field.
 593      * If it is not possible to return the value, because the field is not supported
 594      * or for some other reason, an exception is thrown.
 595      * <p>
 596      * If the field is a {@link ChronoField} then the query is implemented here.
 597      * The {@code OFFSET_SECONDS} field returns the value of the offset.
 598      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 599      * <p>
 600      * If the field is not a {@code ChronoField}, then the result of this method
 601      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 602      * passing {@code this} as the argument. Whether the value can be obtained,
 603      * and what the value represents, is determined by the field.
 604      *
 605      * @param field  the field to get, not null
 606      * @return the value for the field
 607      * @throws DateTimeException if a value for the field cannot be obtained
 608      * @throws UnsupportedTemporalTypeException if the field is not supported
 609      * @throws ArithmeticException if numeric overflow occurs
 610      */
 611     @Override
 612     public long getLong(TemporalField field) {
 613         if (field == OFFSET_SECONDS) {
 614             return totalSeconds;
 615         } else if (field instanceof ChronoField) {
 616             throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
 617         }
 618         return field.getFrom(this);
 619     }
 620 
 621     //-----------------------------------------------------------------------
 622     /**
 623      * Queries this offset using the specified query.
 624      * <p>
 625      * This queries this offset using the specified query strategy object.
 626      * The {@code TemporalQuery} object defines the logic to be used to
 627      * obtain the result. Read the documentation of the query to understand
 628      * what the result of this method will be.
 629      * <p>
 630      * The result of this method is obtained by invoking the
 631      * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
 632      * specified query passing {@code this} as the argument.
 633      *
 634      * @param <R> the type of the result
 635      * @param query  the query to invoke, not null
 636      * @return the query result, null may be returned (defined by the query)