Serializable
public final class ZoneRules extends Object implements Serializable
The rules model all the historic and future transitions for a time-zone. ZoneOffsetTransition
is used for known transitions, typically historic. ZoneOffsetTransitionRule
is used for future transitions that are based on the result of an algorithm.
The rules are loaded via ZoneRulesProvider
using a ZoneId
. The same rules may be shared internally between multiple zone IDs.
Serializing an instance of ZoneRules
will store the entire set of rules. It does not store the zone ID as it is not part of the state of this object.
A rule implementation may or may not store full information about historic and future transitions, and the information stored is only as accurate as that supplied to the implementation by the rules provider. Applications should treat the data provided as representing the best information available to the implementation of this rule.
Modifier and Type | Method | Description |
---|---|---|
boolean |
equals |
Checks if this set of rules equals another. |
Duration |
getDaylightSavings |
Gets the amount of daylight savings in use for the specified instant in this zone. |
ZoneOffset |
getOffset |
Gets the offset applicable at the specified instant in these rules. |
ZoneOffset |
getOffset |
Gets a suitable offset for the specified local date-time in these rules. |
ZoneOffset |
getStandardOffset |
Gets the standard offset for the specified instant in this zone. |
ZoneOffsetTransition |
getTransition |
Gets the offset transition applicable at the specified local date-time in these rules. |
List |
getTransitionRules() |
Gets the list of transition rules for years beyond those defined in the transition list. |
List |
getTransitions() |
Gets the complete list of fully defined transitions. |
List |
getValidOffsets |
Gets the offset applicable at the specified local date-time in these rules. |
int |
hashCode() |
Returns a suitable hash code given the definition of #equals . |
boolean |
isDaylightSavings |
Checks if the specified instant is in daylight savings. |
boolean |
isFixedOffset() |
Checks of the zone rules are fixed, such that the offset never varies. |
boolean |
isValidOffset |
Checks if the offset date-time is valid for these rules. |
ZoneOffsetTransition |
nextTransition |
Gets the next transition after the specified instant. |
static ZoneRules |
of |
Obtains an instance of ZoneRules that has fixed zone rules. |
static ZoneRules |
of |
Obtains an instance of a ZoneRules. |
ZoneOffsetTransition |
previousTransition |
Gets the previous transition before the specified instant. |
String |
toString() |
Returns a string describing this object. |
public static ZoneRules of(ZoneOffset baseStandardOffset, ZoneOffset baseWallOffset, List<ZoneOffsetTransition> standardOffsetTransitionList, List<ZoneOffsetTransition> transitionList, List<ZoneOffsetTransitionRule> lastRules)
baseStandardOffset
- the standard offset to use before legal rules were set, not nullbaseWallOffset
- the wall offset to use before legal rules were set, not nullstandardOffsetTransitionList
- the list of changes to the standard offset, not nulltransitionList
- the list of transitions, not nulllastRules
- the recurring last rules, size 16 or less, not nullpublic static ZoneRules of(ZoneOffset offset)
offset
- the offset this fixed zone rules is based on, not nullpublic boolean isFixedOffset()
public ZoneOffset getOffset(Instant instant)
The mapping from an instant to an offset is simple, there is only one valid offset for each instant. This method returns that offset.
instant
- the instant to find the offset for, not null, but null may be ignored if the rules have a single offset for all instantspublic ZoneOffset getOffset(LocalDateTime localDateTime)
The mapping from a local date-time to an offset is not straightforward. There are three cases:
Since, in the case of Gap and Overlap, the offset returned is a "best" value, rather than the "correct" value, it should be treated with care. Applications that care about the correct offset should use a combination of this method, getValidOffsets(LocalDateTime)
and getTransition(LocalDateTime)
.
localDateTime
- the local date-time to query, not null, but null may be ignored if the rules have a single offset for all instantspublic List<ZoneOffset> getValidOffsets(LocalDateTime localDateTime)
The mapping from a local date-time to an offset is not straightforward. There are three cases:
There are various ways to handle the conversion from a LocalDateTime
. One technique, using this method, would be:
List<ZoneOffset> validOffsets = rules.getValidOffsets(localDT); if (validOffsets.size() == 1) { // Normal case: only one valid offset zoneOffset = validOffsets.get(0); } else { // Gap or Overlap: determine what to do from transition (which will be non-null) ZoneOffsetTransition trans = rules.getTransition(localDT); }
In theory, it is possible for there to be more than two valid offsets. This would happen if clocks to be put back more than once in quick succession. This has never happened in the history of time-zones and thus has no special handling. However, if it were to happen, then the list would return more than 2 entries.
localDateTime
- the local date-time to query for valid offsets, not null, but null may be ignored if the rules have a single offset for all instantspublic ZoneOffsetTransition getTransition(LocalDateTime localDateTime)
The mapping from a local date-time to an offset is not straightforward. There are three cases:
There are various ways to handle the conversion from a LocalDateTime
. One technique, using this method, would be:
ZoneOffsetTransition trans = rules.getTransition(localDT); if (trans != null) { // Gap or Overlap: determine what to do from transition } else { // Normal case: only one valid offset zoneOffset = rule.getOffset(localDT); }
localDateTime
- the local date-time to query for offset transition, not null, but null may be ignored if the rules have a single offset for all instantspublic ZoneOffset getStandardOffset(Instant instant)
This provides access to historic information on how the standard offset has changed over time. The standard offset is the offset before any daylight saving time is applied. This is typically the offset applicable during winter.
instant
- the instant to find the offset information for, not null, but null may be ignored if the rules have a single offset for all instantspublic Duration getDaylightSavings(Instant instant)
This provides access to historic information on how the amount of daylight savings has changed over time. This is the difference between the standard offset and the actual offset. Typically the amount is zero during winter and one hour during summer. Time-zones are second-based, so the nanosecond part of the duration will be zero.
This default implementation calculates the duration from the actual
and standard
offsets.
instant
- the instant to find the daylight savings for, not null, but null may be ignored if the rules have a single offset for all instantspublic boolean isDaylightSavings(Instant instant)
This checks if the standard offset and the actual offset are the same for the specified instant. If they are not, it is assumed that daylight savings is in operation.
This default implementation compares the actual
and standard
offsets.
instant
- the instant to find the offset information for, not null, but null may be ignored if the rules have a single offset for all instantspublic boolean isValidOffset(LocalDateTime localDateTime, ZoneOffset offset)
To be valid, the local date-time must not be in a gap and the offset must match one of the valid offsets.
This default implementation checks if getValidOffsets(java.time.LocalDateTime)
contains the specified offset.
localDateTime
- the date-time to check, not null, but null may be ignored if the rules have a single offset for all instantsoffset
- the offset to check, null returns falsepublic ZoneOffsetTransition nextTransition(Instant instant)
This returns details of the next transition after the specified instant. For example, if the instant represents a point where "Summer" daylight savings time applies, then the method will return the transition to the next "Winter" time.
instant
- the instant to get the next transition after, not null, but null may be ignored if the rules have a single offset for all instantspublic ZoneOffsetTransition previousTransition(Instant instant)
This returns details of the previous transition before the specified instant. For example, if the instant represents a point where "summer" daylight saving time applies, then the method will return the transition from the previous "winter" time.
instant
- the instant to get the previous transition after, not null, but null may be ignored if the rules have a single offset for all instantspublic List<ZoneOffsetTransition> getTransitions()
The complete set of transitions for this rules instance is defined by this method and getTransitionRules()
. This method returns those transitions that have been fully defined. These are typically historical, but may be in the future.
The list will be empty for fixed offset rules and for any time-zone where there has only ever been a single offset. The list will also be empty if the transition rules are unknown.
public List<ZoneOffsetTransitionRule> getTransitionRules()
The complete set of transitions for this rules instance is defined by this method and getTransitions()
. This method returns instances of ZoneOffsetTransitionRule
that define an algorithm for when transitions will occur.
For any given ZoneRules
, this list contains the transition rules for years beyond those years that have been fully defined. These rules typically refer to future daylight saving time rule changes.
If the zone defines daylight savings into the future, then the list will normally be of size two and hold information about entering and exiting daylight savings. If the zone does not have daylight savings, or information about future changes is uncertain, then the list will be empty.
The list will be empty for fixed offset rules and for any time-zone where there is no daylight saving time. The list will also be empty if the transition rules are unknown.
public boolean equals(Object otherRules)
Two rule sets are equal if they will always result in the same output for any given input instant or local date-time. Rules from two different groups may return false even if they are in fact the same.
This definition should result in implementations comparing their entire state.
public int hashCode()
#equals
.public String toString()
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/time/zone/ZoneRules.html