Introduction to Java ZoneOffset
ZoneOffset is a class in Java that denotes the fixed zone offset from the time zone UTC. This class inherits the class ZoneId and implements the interface Comparable.
This class states three fields:
- MAX: Maximum supported offset, which is a constant
- MIN: Minimum supported offset, which is a constant
- UTC: Offset constant of the time zone for UTC (ID: ‘Z’)
Let us see more about this class in the following sections.
Syntax
Below is the declaration for ZoneOffset class
public final class ZoneOffset extends ZoneId implements TemporalAccessor, TemporalAdjuster, Comparable<ZoneOffset>, Serializable
Methods of Java ZoneOffset
The following are the different methods for the ZoneOffset class that performs different functionalities:
1. adjustInto(Temporaltemp)
The temporal object temp mentioned will be adjusted in order to have offset same as this object.
Code:
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;
public class OffsetZoneEx {
public static void main(String[] args) {
ZonedDateTime dt = ZonedDateTime.now();
System.out.println(dt);
ZoneOffset off = ZoneOffset.of("Z");
dt = (ZonedDateTime)off.adjustInto(dt);
System.out.println(dt);
}
}
Output:
2. compareTo(ZoneOffsetotheroff)
This offset is compared with another one in descending order.
Code:
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;
public class OffsetZoneEx {
public static void main(String[] args) {
ZoneOffset off1 = ZoneOffset.of("Z");
ZoneOffset off2 = ZoneOffset.of("+03:05");
System.out.println("First offset :"+ off1);
System.out.println("Second offset :"+ off2);
int cmp = off1.compareTo(off2);
System.out.println(cmp > 1 ? "Offset 1 is greater" : "Offset 2 is greater");
} }
Output:
3. equals(Objectobj)
This offset is checked with another one whether they are equal.
Code:
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;
public class OffsetZoneEx {
public static void main(String[] args) {
ZoneOffset off1 = ZoneOffset.of("Z");
ZoneOffset off2 = ZoneOffset.of("+03:05");
System.out.println("First offset :"+ off1);
System.out.println("Second offset :"+ off2);
boolean cmp = off1.equals(off2);
System.out.println(cmp); } }
Output:
4. from(TemporalAccessortemp)
An instance of offset is from a temporal object.
Code:
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;
public class OffsetZoneEx {
public static void main(String[] args) {
ZoneOffset off1 = ZoneOffset.from(ZonedDateTime.now());
System.out.println(off1);
}
}
Output:
5. get(TemporalFieldf)
Value of field f will be retrieved from the offset as int.
Code:
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;
public class OffsetZoneEx {
public static void main(String[] args) {
ZoneOffset off1 = ZoneOffset.from(ZonedDateTime.now());
System.out.println(off1);
System.out.println(off1.get(ChronoField.OFFSET_SECONDS));
} }
Output:
6. getId( )
THE offset ID of the normalized zone will be retrieved.
Code:
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;
public class OffsetZoneEx {
public static void main(String[] args) {
ZoneOffset off1 = ZoneOffset.of("+03:50");
System.out.println(off1);
System.out.println(off1.getId());
}
}
Output:
7. getLong(TemporalFieldf)
Value of field f will be retrieved from the offset as long.
Code:
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;
public class OffsetZoneEx {
public static void main(String[] args) {
ZoneOffset off1 = ZoneOffset.from(ZonedDateTime.now());
System.out.println(off1);
System.out.println(off1.getLong(ChronoField.OFFSET_SECONDS));
} }
Output:
8. getRules()
Rules will be retrieved for the associated time zone.
Code:
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;
public class OffsetZoneEx {
public static void main(String[] args) {
ZoneOffset off1 = ZoneOffset.from(ZonedDateTime.now());
System.out.println(off1);
System.out.println(off1.getRules());
} }
Output:
9. getTotalSeconds( )
Total zone offset will be retrieved in seconds.
Code:
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;
public class OffsetZoneEx {
public static void main(String[] args) {
ZoneOffset off1 = ZoneOffset.from(ZonedDateTime.now());
System.out.println(off1);
System.out.println(off1.getTotalSeconds()); } }
Output:
10. ofHours(int hrs)
An instance of zoneoffset will be retrieved in hours.
Code:
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;
public class OffsetZoneEx {
public static void main(String[] args) {
ZoneOffset off1 = ZoneOffset.ofHours(3);
System.out.println(off1);
} }
Output:
11. ofHoursMinutes(int hrs, int mints)
An instance of zoneoffset will be retrieved in hours and minutes.
Code:
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;
public class OffsetZoneEx {
public static void main(String[] args) {
ZoneOffset off1 = ZoneOffset.ofHoursMinutes(3, 5);
System.out.println(off1); } }
Output:
12. ofHoursMinutesSeconds(int hrs, int mints, int sec)
An instance of zoneoffset will be retrieved in hours, minutes and seconds.
Code:
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;
public class OffsetZoneEx {
public static void main(String[] args) {
ZoneOffset off1 = ZoneOffset.ofHoursMinutesSeconds(3, 5, 7);
System.out.println(off1);
} }
Output:
13. hashCode()
The hashcode of the zoneoffset will be retrieved.
Code:
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;
public class OffsetZoneEx {
public static void main(String[] args) {
ZoneOffset off1 = ZoneOffset.from(ZonedDateTime.now());
System.out.println(off1);
System.out.println(off1.hashCode());
} }
Output:
14. ofTotalSeconds(int totalsecs)
An instance of zoneoffset will be retrieved in seconds.
Code:
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;
public class OffsetZoneEx {
public static void main(String[] args) {
ZoneOffset off1 = ZoneOffset.ofTotalSeconds(2800);
System.out.println(off1);
} }
Output:
15. isSupported(TemporalFieldf)
Checks whether the mentioned field is supported.
Code:
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;
public class OffsetZoneEx {
public static void main(String[] args) {
ZoneOffset off1 = ZoneOffset.of("Z");
System.out.println(off1);
System.out.println(off1.isSupported(ChronoField.OFFSET_SECONDS));
} }
Output:
16. of(Stringoffsetid)
An instance of zoneoffset will be retrieved using ID.
Code:
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;
public class OffsetZoneEx {
public static void main(String[] args) {
ZoneOffset off1 = ZoneOffset.of("Z");
System.out.println(off1);
} }
Output:
17. toString()
zoneoffset will be retrieved s string using an ID that is normalized.
Code:
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;
public class OffsetZoneEx {
public static void main(String[] args) {
String off1 = ZoneOffset.of("Z").toString();
System.out.println(off1);
} }
Output:
18. query(TemporalQuery<R> query)
zoneoffset will be queried using the query mentioned.
Code:
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;
public class OffsetZoneEx {
public static void main(String[] args) {
ZoneOffset off1 = ZoneOffset.of("+08:00");
System.out.println(off1);
System.out.println(off1.query(TemporalQueries.zone())); } }
Output:
19. range(TemporalFieldf)
The range of valid values will be retrieved for the field mentioned.
Code:
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;
public class OffsetZoneEx {
public static void main(String[] args) {
ZoneOffset off1 = ZoneOffset.of("+08:00");
System.out.println(off1);
System.out.println(off1.range(ChronoField.OFFSET_SECONDS ));
} }
Output:
Conclusion
In Java, ZoneOffset is a class that denotes the fixed zone offset from the time zone UTC. MAX, MIN, and UTC are the three fields of this class. In this article, different details such as declaration, methods, and examples of Java ZoneOffset class is explained in detail.
Recommended Articles
This is a guide to Java ZoneOffset. Here we discuss an introduction to Java ZoneOffset with syntax, different methods with programming examples. You can also go through our other related articles to learn more –