Java - Query the ZoneRules object to get information about the time offset and time changes for a ZoneId.

Description

Query the ZoneRules object to get information about the time offset and time changes for a ZoneId.

Demo

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 Main {
        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);
        }/*from   www.  j  a  v  a2  s  .com*/

        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);
                ZoneOffset offset = rules.getOffset(now);
                System.out.println("Zone offset: " + offset);

                List<ZoneOffsetTransition> transitions = rules.getTransitions();
                System.out.println(transitions);
        }
}

Result

Related Topic