BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java ZoneOffset class explained with examples

By Chaitanya Singh | Filed Under: java

Java ZoneOffset class allows us to manage time-zones effectively. Each country follows a certain timezone on top of that there is a day-light saving concept that comes into picture. To manage time-zones effectively and correctly, ZoneOffset provides multiple useful methods. ZoneOffset represents the amount of time that a timezone differs from Greenwich/UTC, this is usually represented by hours and minutes such as a – or +01:00.

Java ZoneOffset class:

public final class ZoneOffset extends ZoneId
implements TemporalAccessor, TemporalAdjuster, Comparable<ZoneOffset>, Serializable

Java ZoneOffset – Method Summary

MethodDescription
int get(TemporalField field)This method returns the value of the specified field from this offset as an integer.
Temporal adjustInto(Temporal temporal)It adjusts the specified temporal object to have the same offset as this object.
static ZoneOffset of(String offsetId)It returns the instance of ZoneOffset using the specified offset Id.
static ZoneOffset ofHoursMinutes(int hours, int minutes)It returns an instance of ZoneOffset using the specified hours and minutes as an offset
static ZoneOffset ofHours(int hours)It returns an instance of ZoneOffset using the specified hours as offset.
boolean isSupported(TemporalField field)Checks if the specified temporal field is supported by this offset.
ValueRange range(TemporalField field)It returns the range of the valid values for the specified field.
int hashCode()Returns hash code of this offset.
String toString()Returns the offset as a String value.
int getTotalSeconds()Returns the time zone offset in seconds.
ZoneRules getRules()Returns the rules associated with the time-zone

Java ZoneOffset – ofHours() & ofHoursMinutes() methods example

These methods are used to specify an offset in hours & minutes. The method ofHours() is used to specify the offset in hours and to specify it in hour & minutes both use ofHoursMinutes() method.

import java.time.ZoneOffset;
public class JavaExample {
  public static void main(String[] args) {
    ZoneOffset zOffset = ZoneOffset.ofHours(2);
    System.out.println(zOffset);
    ZoneOffset zOffset2 = ZoneOffset.ofHoursMinutes(1,30);
    System.out.println(zOffset2);
  }
}

Output:

+02:00
+01:30

Java ZoneOffset example – Finding the zone offset of a Zone Id

In this example we are finding the zone offset of Indian Time (Zone id: Asia/Kolkata). This offset represents the time difference between the current time-zone (Indian Time) and UTC time zone.

import java.time.*;
public class JavaExample {
  public static void main(String[] args) {
    LocalDateTime datetime = LocalDateTime.now();
    ZoneId zone = ZoneId.of("Asia/Kolkata");
    ZoneOffset zoneoffset = zone.getRules().getOffset(datetime);
    System.out.println(zoneoffset);
  }
}

Output:

+05:30

Reference

Javadoc

❮ Java Tutorial

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap