Java Reference
In-Depth Information
Asia/Aden
Africa/Cairo
Pacific/Honolulu
America/Chicago
Europe/Athens
...
A
ZoneId
object gives you access to zone rules for the time zone represented by the
ZoneId
. You can use the
getRules()
method of the
ZoneId
class to get an instance of the
ZoneRules
class to work with rules such as transitions
for Daylight Saving Time, the zone offset for a specified datetime, the amount of daylight saving, etc. Typically, you
will not use zone rules directly in your code. As a developer, you would use a
ZoneId
to create a
ZonedDateTime
, which
is discussed shortly. The program in Listing 12-5 shows how to query the
ZoneRules
object to get information about
the time offset and time changes for a
ZoneId
. The list of time transitions is very big and it is shown it partially in the
output.
Listing 12-5.
Knowing the Time Change Rules (the ZoneRules) for a ZoneId
// ZoneRulesTest.java
package com.jdojo.datetime;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.zone.ZoneOffsetTransition;
import java.time.zone.ZoneRules;
import java.util.List;
public class ZoneRulesTest {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println("Current Date Time: " + now);
ZoneId fixedZoneId = ZoneId.of("+06:00");
ZoneId bdDhaka = ZoneId.of("Asia/Dhaka");
ZoneId usChicago = ZoneId.of("America/Chicago");
// Print some zone rules for ZoneIds
printDetails(fixedZoneId, now);
printDetails(bdDhaka, now);
printDetails(usChicago, now);
}
public static void printDetails(ZoneId zoneId, LocalDateTime now) {
System.out.println("Zone ID: " + zoneId.getId());
ZoneRules rules = zoneId.getRules();
boolean isFixedOffset = rules.isFixedOffset();
System.out.println("isFixedOffset(): " + isFixedOffset);