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
Method | Description |
---|---|
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
Leave a Reply