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 OffsetTime Class explained with examples

By Chaitanya Singh | Filed Under: java

Java OffsetTime class represents time with an offset from UTC timezone such as 11:23:45+02:00. The time represented by an instance of this class is often viewed as hour-minute-second-offset, however this class can store time upto the precision of nanoseconds. For example a time value “13:45.30.123456789+02:00” can be stored as OffsetTime.

Java OffsetTime class:

public final class OffsetTime extends Object
implements Temporal, TemporalAdjuster, Comparable<OffsetTime>, Serializable

Java OffsetTime class – Method Summary

MethodDescription
OffsetDateTime atDate(LocalDate date)It combines this OffsetTime with date to create an instance of OffsetDateTime.
static OffsetTime now()It fetches current time from system in default time-zone.
int compareTo(OffsetTime other)It compares this OffsetTime with the other specified OffsetTime.
int get(TemporalField field)Gets the value of the specified field from this OffsetTime.
int getHour()Returns the hour-of-day field as an int value.
int getMinute()Returns the minute-of-hour field as an int value.
int getSecond()Returns the second-of-minute field as an int value.
int getNano()Returns the nano-of-second field as an int value
boolean isAfter(OffsetTime other)Checks if this OffsetTime is after the specified OffsetTime considering both time has common date.
boolean isBefore(OffsetTime other)Checks if this OffsetTime is before the specified OffsetTime considering both time has common date.
boolean isEqual(OffsetTime other)Checks if this OffsetTime is equal to the specified OffsetTime considering both time has common date.
ValueRange range(TemporalField field)Returns the range of the specified field in this OffsetTime.
OffsetTime withHour(int hour)Returns a copy of this OffsetTime after altering the hour with the specified value.
OffsetTime withMinute(int minute)Returns a copy of this OffsetTime after altering the minute with the specified value.
OffsetTime plusHours(long hours)Returns a copy of this OffsetTime after adding the specified hours to it
OffsetTime minusHours(long hours)Returns a copy of this OffsetTime after subtracting the specified hours from it

Java OffsetTime – now() method example.

Method now() is used to get the current OffsetTime from the system in default timezone.

import java.time.OffsetTime;
public class JavaExample {
    public static void main(String[] args) {
        OffsetTime offTime = OffsetTime.now();
        System.out.println("Current OffsetTime: "+offTime);
    }
}

Output:

Current OffsetTime: 08:54:32.633796+05:30

Java OffsetTime – get() method example

As we discussed in the method summary, the get() method is used to get the specified field from this OffsetTime.

import java.time.OffsetTime;
import java.time.temporal.ChronoField;
public class JavaExample {
    public static void main(String[] args) {
        //getting current offset time
        OffsetTime offTime = OffsetTime.now();
        System.out.println("Current OffsetTime: "+offTime);
        int hour = offTime.get(ChronoField.HOUR_OF_DAY);
        System.out.println("Hour of the day: "+hour);
        int minute = offTime.get(ChronoField.MINUTE_OF_HOUR);
        System.out.println("Minute of the day: "+minute);
        int second = offTime.get(ChronoField.SECOND_OF_MINUTE);
        System.out.println("Second of the minute:"+second);
        int nano = offTime.get(ChronoField.NANO_OF_SECOND);
        System.out.println("Nano of the second: "+nano);
    }
}

Output:

Current OffsetTime: 09:01:12.018145+05:30
Hour of the day: 9
Minute of the day: 1
Second of the minute:12
Nano of the second: 18145000

Java OffsetTime – withHour(), isBefore() & isAfter() methods example

In this example we used withHour() method to alter the hour field in the current OffsetTime. The methods ifBefore() and isAfter() are used to check if current OffsetTime is before or after the altered OffsetTime.

import java.time.OffsetTime;
public class JavaExample {
    public static void main(String[] args) {
        //getting current offset time
        OffsetTime offTime = OffsetTime.now();
        System.out.println("Current OffsetTime: "+offTime);

        //altering hour
        OffsetTime offTime2 = offTime.withHour(11);
        System.out.println("Altered OffsetTime: "+offTime2);

        //Is current offset time before altered time
        System.out.println(offTime.isBefore(offTime2));
        //Is current offset time after altered time
        System.out.println(offTime.isAfter(offTime2));
    }
}

Output:

Current OffsetTime: 09:06:54.884206+05:30
Altered OffsetTime: 11:06:54.884206+05:30
true
false
❮ 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