java8 java.time.ZoneOffset tutorials with examples
What is the ZoneOffset class in Java 8?
The ZoneOffset class is defined within the java.time package and was introduced in Java 8 as part of the Date and Time API enhancements.
It serves to represent the offset from the Coordinated Universal Time (UTC) time zone. The offset indicates the difference, in hours and minutes, between the specified time zone and UTC.
A time zone corresponds to a geographical region on Earth, such as a group of countries or a single country, which adheres to a standardized time.
Each time zone comprises two properties.
- id - Asia/Calcutta
- offset - time difference or offset from UTC/Greenwich - Example is +05:30
Features of ZoneOffset:
- It is immutable; once an instance is created, its state cannot be modified.
- It is thread-safe, designed to work reliably in a multi-threaded environment.
- It is a value-based class. When this class is checked for duplicates or identical instances using the
===
operator, the results may be inconsistent, and the behavior is specific to the Java Virtual Machine (JVM).
Class Signature:
public final class ZoneOffset
extends ZoneId
implements TemporalAccessor, TemporalAdjuster, Comparable<ZoneOffset>, Serializable
It extends the ZoneId
class, and this class represents time zone id like Europe/Paris.
Example Usage
The ZoneOffset
class offers several methods for creating instances, such as of(), ofHours(), ofHoursMinutes(), and ofHoursMinutesSeconds().
import java.time.ZoneOffset;
public class Test {
public static void main(String[] args) {
ZoneOffset zonedOffsetUtc = ZoneOffset.UTC;
ZoneOffset zonedOffsetMin = ZoneOffset.MIN;
ZoneOffset zonedOffsetMax = ZoneOffset.MAX;
System.out.println(zonedOffsetUtc);
System.out.println(zonedOffsetMin);
System.out.println(zonedOffsetMax);
ZoneOffset zoneOffSet = ZoneOffset.ofHours(5);
System.out.println(zoneOffSet);
}
}
Output:
Z
-18:00
+18:00
+05:00
In the output, ‘Z’ represents a time zone offset from UTC.
How to Convert ZoneId to ZoneOffset in Java 8?
When dealing with timezone conversions, Instant
is the correct instance to use as it provides accurate information.
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class Test {
public static void main(String[] args) {
Instant instantObject = Instant.now();
ZoneId systemDefaultZone = ZoneId.systemDefault();
ZoneOffset myzoneoffset = systemDefaultZone.getRules().getOffset(instantObject);
System.out.println(myzoneoffset);
}
}
How to Convert ZoneOffset to ZoneId in Java 8?
The java.time.ZoneId
class provides the ofOffset()
method, which creates a ZoneId
from a ZoneOffset
and a prefix.
Here is a Syntax
public static ZoneId ofOffset(String prefix, ZoneOffset offset)
The following code is the usage of ofOffset method
import java.time.ZoneId;
import java.time.ZoneOffset;
public class Test {
public static void main(String[] args) {
ZoneId zoneId = ZoneId.ofOffset("GMT", ZoneOffset.UTC);
System.out.println(zoneId); // GMT
}
}
How to Retrieve the Offset Value from the UTC Timezone?
Following are steps to get offset value.
- Create an instance of LocalDateTime.
- LocalDateTime provides the best available offset.
- Use ZoneId.systemDefault() to get the system’s default timezone.
- Retrieve the timezone rules and get the timezone offset using the current offset (Instant.now()).
- This provides the number of hours/minutes offset from the UTC timezone difference.
import java.time.Instant;
import java.time.ZoneId;
public class Test {
public static void main(String[] args) {
System.out.println(ZoneId.systemDefault().getRules().getOffset(Instant.now())); // +05:30
}
}
ZoneOffset Methods
Method | Description |
---|---|
adjustInto(Temporal temporal) | adjust the Offset of the caller with the same offset of the parameter object |
getId() | Return the offset id ie Z |
get(TemporalField field) | Return the field from an offset as Int value |
getLong() | Return the field from an offset as a Long value |
of(String offsetId) | It is used to create a ZoneOffset object with id |
ofHours(int hours) | It is used to create a ZoneOffset object with offset hours |
ofHoursMinutes(int offsetHours, int offsetMinutes) | It is used to create a ZoneOffset object with offset hours and minutes |
ofHoursMinutesSeconds(int hours, int minutes, int seconds) | It is used to create a ZoneOffset object with offset hours, minutes, and seconds |
ofTotalSeconds(int totalSeconds) | It is used to create a ZoneOffset object with total seconds |
range(TemporalField field) | It is used to the range of values using Temporal field |
Conclusion
Learn ZoneOffset class tutorials with below examples
- How to Convert ZoneOffset to ZoneId in Java 8?
- How to Retrieve the Offset Value from the UTC Timezone?
- How to Convert ZoneId to ZoneOffset in Java 8?
- Other methods available for use.