public final class ZoneOffset extends java.lang.Object implements CalendricalObject, java.lang.Comparable<ZoneOffset>, java.io.Serializable
+02:00
.
A time-zone offset is the period of time that a time-zone differs from UTC. This is usually a fixed number of hours and minutes.
Different parts of the world have different time-zone offsets.
The rules for how offsets vary by place and time of year are captured in the
ZoneId
class.
For example, Paris is one hours ahead of UTC in winter and two hours ahead in
summer. The ZoneId
instance for Paris will reference two
ZoneOffset
instances - a +01:00
instance for winter,
and a +02:00
instance for summer.
In 2008, time-zone offsets around the world extended from -12:00 to +14:00. To prevent any problems with that range being extended, yet still provide validation, the range of offsets is restricted to -18:00 to 18:00 inclusive.
This class is designed for use with the ISO calendar system The fields of hours, minutes and seconds make assumptions that are valid for the standard ISO definitions of those fields. This class may be used with other calendar systems providing the definition of the time fields matches those of the ISO calendar system.
Instances of ZoneOffset
must be compared using equals(java.lang.Object)
.
Implementations may choose to cache certain common offsets, however
applications must not rely on such caching.
Modifier and Type | Field and Description |
---|---|
static ZoneOffset |
UTC
The time-zone offset for UTC, with an id of 'Z'.
|
Modifier and Type | Method and Description |
---|---|
int |
compareTo(ZoneOffset other)
Compares this offset to another offset in descending order.
|
boolean |
equals(java.lang.Object obj)
Checks if this offset is equal to another offset.
|
<R> R |
extract(java.lang.Class<R> type)
Extracts date-time information in a generic way.
|
static ZoneOffset |
from(CalendricalObject calendrical)
Obtains an instance of
ZoneOffset from a calendrical. |
int |
getHoursField()
Gets the hours field of the zone offset.
|
java.lang.String |
getID()
Gets the normalized zone offset id.
|
int |
getMinutesField()
Gets the minutes field of the zone offset.
|
int |
getSecondsField()
Gets the seconds field of the zone offset.
|
int |
getTotalSeconds()
Gets the total zone offset in seconds.
|
int |
hashCode()
A hash code for this offset.
|
static ZoneOffset |
of(java.lang.String offsetID)
Obtains an instance of
ZoneOffset using the id. |
static ZoneOffset |
ofHours(int hours)
Obtains an instance of
ZoneOffset using an offset in hours. |
static ZoneOffset |
ofHoursMinutes(int hours,
int minutes)
Obtains an instance of
ZoneOffset using an offset in
hours and minutes. |
static ZoneOffset |
ofHoursMinutesSeconds(int hours,
int minutes,
int seconds)
Obtains an instance of
ZoneOffset using an offset in
hours, minutes and seconds. |
static ZoneOffset |
ofTotalSeconds(int totalSeconds)
Obtains an instance of
ZoneOffset specifying the total offset in seconds |
java.lang.String |
toString()
Outputs this offset as a
String , using the normalized ID. |
ZoneId |
toZoneId()
Converts this offset to a time-zone.
|
ZoneOffset |
with(CalendricalAdjuster adjuster)
Returns a copy of this calendrical with the specified calendrical set.
|
public static final ZoneOffset UTC
public static ZoneOffset of(java.lang.String offsetID)
ZoneOffset
using the id.
This method parses the string id of a ZoneOffset
to
return an instance. The parsing accepts all the formats generated by
getID()
, plus some additional formats:
Z
- for UTC
+hh:mm
-hh:mm
+hhmm
-hhmm
+hh:mm:ss
-hh:mm:ss
+hhmmss
-hhmmss
The ID of the returned offset will be normalized to one of the formats
described by getID()
.
The maximum supported range is from +18:00 to -18:00 inclusive.
offsetID
- the offset id, not nulljava.lang.IllegalArgumentException
- if the offset id is invalidpublic static ZoneOffset ofHours(int hours)
ZoneOffset
using an offset in hours.hours
- the time-zone offset in hours, from -18 to +18java.lang.IllegalArgumentException
- if the offset is not in the required rangepublic static ZoneOffset ofHoursMinutes(int hours, int minutes)
ZoneOffset
using an offset in
hours and minutes.
The sign of the hours and minutes components must match. Thus, if the hours is negative, the minutes must be negative or zero. If the hours is zero, the minutes may be positive, negative or zero.
hours
- the time-zone offset in hours, from -18 to +18minutes
- the time-zone offset in minutes, from 0 to ±59, sign matches hoursjava.lang.IllegalArgumentException
- if the offset is not in the required rangepublic static ZoneOffset ofHoursMinutesSeconds(int hours, int minutes, int seconds)
ZoneOffset
using an offset in
hours, minutes and seconds.
The sign of the hours, minutes and seconds components must match. Thus, if the hours is negative, the minutes and seconds must be negative or zero.
hours
- the time-zone offset in hours, from -18 to +18minutes
- the time-zone offset in minutes, from 0 to ±59, sign matches hours and secondsseconds
- the time-zone offset in seconds, from 0 to ±59, sign matches hours and minutesjava.lang.IllegalArgumentException
- if the offset is not in the required rangepublic static ZoneOffset from(CalendricalObject calendrical)
ZoneOffset
from a calendrical.
A calendrical represents some form of date and time information.
This factory converts the arbitrary calendrical to an instance of ZoneOffset
.
calendrical
- the calendrical to convert, not nullCalendricalException
- if unable to convert to an ZoneOffset
public static ZoneOffset ofTotalSeconds(int totalSeconds)
ZoneOffset
specifying the total offset in seconds
The offset must be in the range -18:00
to +18:00
, which corresponds to -64800 to +64800.
totalSeconds
- the total time-zone offset in seconds, from -64800 to +64800java.lang.IllegalArgumentException
- if the offset is not in the required rangepublic int getTotalSeconds()
This is the primary way to access the offset amount. It returns the total of the hours, minutes and seconds fields as a single offset that can be added to a time.
public java.lang.String getID()
The id is minor variation to the standard ISO-8601 formatted string for the offset. There are three formats:
Z
- for UTC (ISO-8601)
+hh:mm
or -hh:mm
- if the seconds are zero (ISO-8601)
+hh:mm:ss
or -hh:mm:ss
- if the seconds are non-zero (not ISO-8601)
public int getHoursField()
The zone offset is divided into three fields - hours, minutes and seconds. This method returns the value of the hours field. The sign of the value returned by this method will match that of the minutes and seconds fields.
It is recommended to use the underlying representation of a zone offset, a count
of seconds accessed using getTotalSeconds()
, rather than this method.
public int getMinutesField()
The zone offset is divided into three fields - hours, minutes and seconds. This method returns the value of the minutes field. The sign of the value returned by this method will match that of the hours and seconds fields.
It is recommended to use the underlying representation of a zone offset, a count
of seconds accessed using getTotalSeconds()
, rather than this method.
public int getSecondsField()
The zone offset is divided into three fields - hours, minutes and seconds. This method returns the value of the seconds field. The sign of the value returned by this method will match that of the hours and minutes fields.
It is recommended to use the underlying representation of a zone offset, a count
of seconds accessed using getTotalSeconds()
, rather than this method.
public <R> R extract(java.lang.Class<R> type)
This method exists to fulfill the CalendricalObject
interface.
This implementation returns the following types:
ZoneOffset
extract
in interface CalendricalObject
R
- the type to extracttype
- the type to extract, null returns nullpublic ZoneOffset with(CalendricalAdjuster adjuster)
CalendricalObject
This allows the manipulation of a calendrical in a general way.
For example, if this calendrical is a LocalDateTime
and the specified calendrical
is a LocalDate
, then the implementation will return a new LocalDateTime
with the date changed.
An implementation must handle the following types if possible:
with
in interface CalendricalObject
adjuster
- the calendrical to set into this calendrical, null returns nullpublic ZoneId toZoneId()
The returned time-zone will use this offset for all instants.
public int compareTo(ZoneOffset other)
The offsets are compared in the order that they occur for the same time
of day around the world. Thus, an offset of +10:00
comes before an
offset of +09:00
and so on down to -18:00
.
compareTo
in interface java.lang.Comparable<ZoneOffset>
other
- the other date to compare to, not nulljava.lang.NullPointerException
- if other
is nullpublic boolean equals(java.lang.Object obj)
The comparison is based on the amount of the offset in seconds.
equals
in class java.lang.Object
obj
- the object to check, null returns falsepublic int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
String
, using the normalized ID.toString
in class java.lang.Object