Knowing the Time Change Rules (the ZoneRules) for a ZoneId - Java Date Time

Java examples for Date Time:Time Zone

Description

Knowing the Time Change Rules (the ZoneRules) for a ZoneId

Demo Code

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);
    //from  w  w w.  j  a va  2s. co  m
    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);

    ZoneOffset offset = rules.getOffset(now);
    System.out.println("Zone offset: " + offset);

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

Result


Related Tutorials