Java OffsetTime class

Java OffsetTime class

Java OffsetTime class is an immutable date-time object that represents a time, often viewed as hour-minute-second offset. It inherits Object class and implements the Comparable interface.


Java OffsetTime class declaration

Let’s see the declaration of java.time.OffsetTime class.

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

Methods of Java OffsetTime

Method Description
String format(DateTimeFormatter formatter) It is used to format this time using the specified formatter.
int get(TemporalField field) It is used to get the value of the specified field from this time as an int.
int getHour() It is used to get the hour-of-day field.
int getMinute() It is used to get the minute-of-hour field.
int getSecond() It is used to get the second-of-minute field.
static OffsetTime now() It is used to obtain the current time from the system clock in the default time-zone.
static OffsetTime of(LocalTime time, ZoneOffset offset) It is used to obtain an instance of OffsetTime from a local time and an offset.
ValueRange range(TemporalField field) It is used to get the range of valid values for the specified field.
VLocalTime toLocalTime() It is used to get the LocalTime part of this date-time.

 

Java OffsetTime class Example

  1. import java.time.OffsetTime;
  2. import java.time.temporal.ChronoField;
  3. public class OffsetTimeExample {
  4.   public static void main(String[] args) {
  5.     OffsetTime offset = OffsetTime.now();
  6.     int h = offset.get(ChronoField.HOUR_OF_DAY);
  7.     System.out.println(h);
  8.     int m = offset.get(ChronoField.MINUTE_OF_DAY);
  9.     System.out.println(m);
  10.     int s = offset.get(ChronoField.SECOND_OF_DAY);
  11.     System.out.println(s);
  12.   }
  13. }

Output:

16
970
58224

Java OffsetTime class Example: getHour()

  1. import java.time.OffsetTime;
  2. public class OffsetTimeClass {
  3.   public static void main(String[] args) {
  4.     OffsetTime offset = OffsetTime.now();
  5.     int h = offset.getHour();
  6.     System.out.println(h+ ” hour”);
  7.   }
  8. }

Output:

15 hour

Java OffsetTime class Example: getMinute()

  1. import java.time.OffsetTime;
  2. public class OffsetTimeExample {
  3.   public static void main(String[] args) {
  4.     OffsetTime offset = OffsetTime.now();
  5.     int m = offset.getMinute();
  6.     System.out.println(m+ ” minute”);
  7.   }
  8. }

Output:

24 minute

Java OffsetTime class Example: getSecond()

  1. import java.time.OffsetTime;
  2. public class OffsetTimeExample {
  3.   public static void main(String[] args) {
  4.     OffsetTime offset = OffsetTime.now();
  5.     int s = offset.getSecond();
  6.     System.out.println(s+ ” second”);
  7.   }
  8. }

Output:

8 second
Next TopicJava OffsetDateTime
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Java OffsetTime class Java OffsetTime class is an immutable date-time object that represents a time, often viewed as hour-minute-second offset. It inherits Object class and implements the Comparable interface. Java OffsetTime class declaration Let’s see the declaration of java.time.OffsetTime class. public final class OffsetTime extends Object implements Temporal, TemporalAdjuster, Comparable<OffsetTime>, Serializable Methods of Java OffsetTime Method Description String format(DateTimeFormatter formatter) It is used to […]

Tags:

....
...