java.time.OffsetTime Class in Java
Java OffsetTime class is an immutable date-time object that represents a time, often viewed as hour-minute-second offset. OffsetTime class represents a time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 18:30:45+08:00, often viewed as an hour-minute-second-offset. This class is immutable and thread-safe, stores all time fields, to a precision of nanoseconds, as well as a zone offset.
Syntax: Class declaration
public final class OffsetTime extends Object implements Temporal, TemporalAdjuster, Comparable<OffsetTime>, Serializable
Now let us come to the main eccentricity to gathering knowledge of the methods of this class, which is as depicted in tabular format below:
Method |
Action Performed |
---|---|
format(DateTimeFormatter formatter) | Formats this time using the specified formatter. |
of(LocalTime time, ZoneOffset offset) | Obtains an instance of OffsetTime from a local time and an offset. |
range(TemporalField field) | Gets the range of valid values for the specified field. |
toLocalTime() | Gets the LocalTime part of this date-time. |
adjustInto(Temporal temporal) | Adjust the specified temporal object to have the same offset and time as this object. |
atDate(LocalDate date) | Combine this time with a date to create an OffsetDateTime. |
compareTo(OffsetTime other) | Compare this OffsetTime to another time. |
equals(Object obj) | Check if this time is equal to another time. |
format(DateTimeFormatter formatter) | Formats this time using the specified formatter. |
from(TemporalAccessor temporal) | Obtains an instance of OffsetTime from a temporal object. |
get(TemporalField field) | Gets the value of the specified field from this time as an int. |
getHour() | Gets the hour-of-day field. |
getLong(TemporalField field) | Gets the value of the specified field from this time as a long. |
getMinute() | Gets the minute-of-hour field. |
getNano() | Gets the nano-of-second field. |
getOffset() | Gets the zone offset, such as ‘+01:00’. |
getSecond() | Gets the second-in-minute field. |
hashCode() | A hash code for this time. |
isAfter(OffsetTime other) | Check if the instant of this OffsetTime is after that of the specified time applying both times to a common date. |
isBefore(OffsetTime other) | Check if the instant of this OffsetTime is before that of the specified time, applying both times to a common date. |
isEqual(OffsetTime other) | Check if the instant of this OffsetTime is equal to that of the specified time, applying both times to a common date. |
isSupported(TemporalField field) | Check if the specified field is supported. |
isSupported(TemporalUnit unit) | Check if the specified unit is supported. |
minus(long amountToSubtract, TemporalUnit unit) | Return a copy of this time with the specified amount subtracted. |
minus(TemporalAmount amountToSubtract) | Return a copy of this time with the specified amount subtracted. |
minusHours(long hours) | Return a copy of this OffsetTime with the specified number of hours subtracted. |
minusMinutes(long minutes) | Return a copy of this OffsetTime with the specified number of minutes subtracted. |
minusNanos(long nanos) | Return a copy of this OffsetTime with the specified number of nanoseconds subtracted. |
minusSeconds(long seconds) | Return a copy of this OffsetTime with the specified number of seconds subtracted. |
now() | Obtains the current time from the system clock in the default time-zone. |
now(Clock clock) | Obtains the current time from the specified clock. |
now(ZoneId zone) | Obtains the current time from the system clock in the specified time-zone. |
of(int hour, int minute, int second, int nanoOfSecond, ZoneOffset offset) | Obtains an instance of OffsetTime from an hour, minute, second and nanosecond. |
of(LocalTime time, ZoneOffset offset) | Obtains an instance of OffsetTime from a local time and an offset. |
ofInstant(Instant instant, ZoneId zone) | Obtains an instance of OffsetTime from an Instant and zone ID. |
parse(CharSequence text) | Obtains an instance of OffsetTime from a text string such as 10:15:30+01:00. |
parse(CharSequence text, DateTimeFormatter formatter) | Obtains an instance of OffsetTime from a text string using a specific formatter. |
plus(long amountToAdd, TemporalUnit unit) | Return a copy of this time with the specified amount added. |
plus(TemporalAmount amountToAdd) | Return a copy of this time with the specified amount added. |
plusHours(long hours) | Return a copy of this OffsetTime with the specified number of hours added. |
plusMinutes(long minutes) | Return a copy of this OffsetTime with the specified number of minutes added. |
plusNanos(long nanos) | Return a copy of this OffsetTime with the specified number of nanoseconds added. |
plusSeconds(long seconds) | Return a copy of this OffsetTime with the specified number of seconds added. |
query(TemporalQuery<R> query) | Queries this time using the specified query. |
range(TemporalField field) | Gets the range of valid values for the specified field. |
toLocalTime() | Gets the LocalTime part of this date-time. |
toString() | Outputs this time as a string, such as 10:15:30+01:00. |
truncatedTo(TemporalUnit unit) | Return a copy of this OffsetTime with the time truncated. |
until(Temporal endExclusive, TemporalUnit unit) | Calculates the amount of time until another time in terms of the specified unit. |
with(TemporalAdjuster adjuster) | Return an adjusted copy of this time. |
with(TemporalField field, long newValue) | Return a copy of this time with the specified field set to a new value. |
withHour(int hour) | Return a copy of this OffsetTime with the hour-of-day altered. |
withMinute(int minute) | Return a copy of this OffsetTime with the minute-of-hour altered. |
withNano(int nanoOfSecond) | Return a copy of this OffsetTime with the nano-of-second altered. |
withOffsetSameInstant(ZoneOffset offset) | Return a copy of this OffsetTime with the specified offset ensuring that the result is at the same instant on an implied day. |
withOffsetSameLocal(ZoneOffset offset) | Return a copy of this OffsetTime with the specified offset ensuring that the result is the same local time. |
withSecond(int second) | Return a copy of this OffsetTime with the second-minute altered. |
Now let us implement a few of the above methods that are listed in the table above to get a depth understanding of how the methods work internally.
Example 1: now() method
Java
// Java Program to Implement OffsetTime class // via now() method // Importing required classes import java.time.OffsetTime; import java.time.temporal.ChronoField; // Main class public class GFG { // Main driver method public static void main(String[] args) { // Creating an object of class in main() method OffsetTime offset = OffsetTime.now(); int h = offset.get(ChronoField.HOUR_OF_DAY); System.out.println(h); int m = offset.get(ChronoField.MINUTE_OF_DAY); System.out.println(m); int s = offset.get(ChronoField.SECOND_OF_DAY); System.out.println(s); } } |
4 253 15211
Example 2: getHour() method
Java
// Java Program to Implement OffsetTime Class // via getHour() method // Importing the class from java.time package import java.time.OffsetTime; // Main class public class GFG { // Main driver method public static void main(String[] args) { // Creating an instance of class and // operation now() method over it to // get the current time from clock OffsetTime offset = OffsetTime.now(); // Getting the hour of day field int h = offset.getHour(); // Print and display the hours System.out.println(h + " hours" ); } } |
4 hours
Example 3: getMinute() method
Java
// Java Program to Implement OffsetTime Class // via getMinute() Method // Importing the class from java.time package import java.time.OffsetTime; // Main class public class GFG { // Main driver method public static void main(String[] args) { // Creating an instance of the class OffsetTime offset = OffsetTime.now(); // Getting the minutes of the day field int m = offset.getMinute(); // Print and display the minutes System.out.println(m + " minutes" ); } } |
12 minutes
Example 4: getSecond() method
Java
// Java Program to Implement OffsetTime Class // via getSecond() Method // Importing the class from java.time package import java.time.OffsetTime; // Main class public class GFG { // Main driver method public static void main(String[] args) { // Creating an instance of the class OffsetTime offset = OffsetTime.now(); // Getting the second from the day field int s = offset.getSecond(); // Print and display the seconds System.out.println(s + " seconds" ); } } |
40 seconds
Example 5: getNano() method
Java
// Java Program to Implement OffsetTime Class // via getNano() Method // Importing the class from java.time package import java.time.OffsetTime; // Main class public class GFG { // Main driver method public static void main(String[] args) { // Creating an instance of the class // in the main() method OffsetTime offset = OffsetTime.now(); // Getting the nano-of-second field int n = offset.getNano(); // Print and display the nanoseconds System.out.println(n + " nanoseconds" ); } } |
464297000 nanoseconds
Example 6: of() method
Java
// Java Program to Implement OffsetTime Class // via of() Method // Importing desired classes from java.time package import java.time.LocalTime; import java.time.OffsetTime; import java.time.ZoneOffset; // Main class public class GFG { // Main driver method public static void main(String[] args) { // Obtaining an instance of OffsetTime // from a local time and an offset. OffsetTime time = OffsetTime.of(LocalTime.now(), ZoneOffset.UTC); // Print and display the time System.out.println(time); } } |
04:12:05.574520Z
Example 7: toLocalTime() method
Java
// Java Program to Implement OffsetTime Class // via toLocalTime() Method // Importing the class from java.time package import java.time.OffsetTime; // Main class public class GFG { // Main driver method public static void main(String[] args) { // Getting the current time from the system clock // in the default time zone OffsetTime time = OffsetTime.now(); // Getting the LocalTime part of this date-time System.out.println(time.toLocalTime()); } } |
04:11:47.834567