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:
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
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
The following are the different methods for the ZoneOffset class that performs different functionalities:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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.
The above is the detailed content of Java ZoneOffset. For more information, please follow other related articles on the PHP Chinese website!