Java Date Time Zone








ZoneOffset represents a fixed zone offset from UTC time zone.

ZoneOffset doesn't track the changes in zone offset caused by the Daylight Saving Time.

The ZoneOffset class declares three constants:

  • UTC
  • MAX
  • MIN

UTC is the time zone offset constant for UTC.

MAX and MIN are the maximum and minimum supported zone offsets.

Z is used as the zone offset designator for UtC time zone.

We can create ZoneOffset with a combination of hour, minute, and second.

import java.time.ZoneOffset;
//from  ww  w. java  2  s.  com
public class Main {
  public static void main(String[] args) {
    ZoneOffset zoneOffset1  = ZoneOffset.ofHours(-1);
    System.out.println(zoneOffset1);
    ZoneOffset zoneOffset2  = ZoneOffset.ofHoursMinutes(6, 30);
    System.out.println(zoneOffset2);
    ZoneOffset zoneOffset3  = ZoneOffset.ofHoursMinutesSeconds(9, 30,  45); 
    System.out.println(zoneOffset3);
  }
}

The code above generates the following result.





Example 2

The following code shows how to create zone offset from offset.

import java.time.ZoneOffset;
//w ww.  j a  va 2 s.c o  m
public class Main {
  public static void main(String[] args) {
    ZoneOffset zoneOffset1  = ZoneOffset.of("+05:00");
    ZoneOffset zoneOffset2  = ZoneOffset.of("Z"); // Same as ZoneOffset.UTC 
    System.out.println(zoneOffset1);
    System.out.println(zoneOffset2);

  }
}

The code above generates the following result.





Example 3

The following code outputs the constant values from ZoneOffset.

import java.time.ZoneOffset;
//w w  w .  j a  va 2  s  .  c  o  m
public class Main {
  public static void main(String[] args) {
    System.out.println("ZoneOffset.UTC: "    + ZoneOffset.UTC); 
    System.out.println("ZoneOffset.MIN: "    + ZoneOffset.MIN); 
    System.out.println("ZoneOffset.MAX: "    + ZoneOffset.MAX);


  }
}

The code above generates the following result.

Note

Java Date-Time API supports zone offsets in hours and minutes, and seconds.

compareTo() from ZoneOffset allows us to compare two zone offsets.

A zone offset of +1:30 is before a zone offset of +1:00.

Java Date-Time API supports zone offsets between -18:00 to +18:00.

ZoneId

ZoneId represents a zone offset and its rules for changing the zone offset for Daylight Saving Time.

ZoneOffset represents a fixed zone offset from UtC time zone whereas ZoneId represents a variable zone offset.

What are the changes is defined by time zone rules.

Time Zone ID

Each time zone has an ID which can be defined in three formats:

  • In zone offset, which can be "Z", "+hh:mm:ss", or "-hh:mm:ss", for example, "+01:00".
  • Prefixed with "UTC", "GMT", or "UT" and followed by a zone offset, for example, "UTC+01:00".
  • In region name, for example, "America/Chicago".

The following code shows how to create a ZoneId using the of() factory method.

import java.time.ZoneId;
// ww  w  .ja v a 2s.c o  m
public class Main {
  public static void main(String[] args) {
    ZoneId  usChicago   = ZoneId.of("America/Chicago");
    System.out.println(usChicago);
    ZoneId  fixedZoneId = ZoneId.of("+01:00");
    System.out.println(fixedZoneId);
  }
}

The code above generates the following result.

Example 4

getAvailableZoneIds() from ZoneId returns all known time zone IDs.

import java.time.ZoneId;
import java.util.Set;
//  w w  w  .  java2  s  .  c  o  m
public class Main {
  public static void main(String[] args) {
    Set<String> zoneIds = ZoneId.getAvailableZoneIds();
    for  (String  zoneId: zoneIds) { 
      System.out.println(zoneId);
    }
  }
}

The code above generates the following result.

ZoneRules

ZoneRules tracks how the zone offset varies.

The following code shows how to use ZoneRules to get information about the time offset and time changes for a ZoneId.

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;
/*w  w w  .ja va 2s .c  om*/
public class Main {
  public static void main(String[] args) {
    LocalDateTime  now = LocalDateTime.now(); 
    ZoneId  usChicago   = ZoneId.of("America/Chicago");
    System.out.println("Zone ID:  "  + usChicago.getId());

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

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

  }
}

The code above generates the following result.