Java OffsetDateTime class

Java OffsetDateTime class

Java OffsetDateTime class is an immutable representation of a date-time with an offset. It inherits Object class and implements the Comparable interface.

OffsetDateTime class is used to store the date and time fields, to a precision of nanoseconds.


Java OffsetDateTime class declaration

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

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

Methods of Java OffsetDateTime

Method Description
int get(TemporalField field) It is used to get the value of the specified field from this date-time as an int.
int getDayOfMonth() It is used to get the day-of-month field.
iint getDayOfYear() It is used to get the day-of-year field.
DayOfWeek getDayOfWeek() It is used to get the day-of-week field, which is an enum DayOfWeek.
OffsetDateTime minusDays(long days) It is used to return a copy of this OffsetDateTime with the specified number of days subtracted.
static OffsetDateTime now() It is used to obtain the current date-time from the system clock in the default time-zone.
OffsetDateTime plusDays(long days) It is used to return a copy of this OffsetDateTime with the specified number of days added.
LocalDate toLocalDate() It is used to get the LocalDate part of this date-time.

 

Java OffsetDateTime class Example: getDayOfMonth()

  1. import java.time.OffsetDateTime;
  2. public class OffsetDateTimeExample {
  3.     public static void main(String[] args) {
  4.         OffsetDateTime offsetDT = OffsetDateTime.now();
  5.         System.out.println(offsetDT.getDayOfMonth());
  6.     }
  7. }

Output:

18

Java OffsetDateTime class Example: getDayOfYear()

  1. import java.time.OffsetDateTime;
  2. public class OffsetDateTimeExample {
  3.     public static void main(String[] args) {
  4.         OffsetDateTime offsetDT = OffsetDateTime.now();
  5.         System.out.println(offsetDT.getDayOfYear());
  6.     }
  7. }

Output:

18

Java OffsetDateTime class Example: getDayOfWeek()

  1. import java.time.OffsetDateTime;
  2. public class OffsetDateTimeExample {
  3.     public static void main(String[] args) {
  4.         OffsetDateTime offsetDT = OffsetDateTime.now();
  5.         System.out.println(offsetDT.getDayOfWeek());
  6.     }
  7. }

Output:

WEDNESDAY

Java OffsetDateTime class Example: toLocalDate()

  1. import java.time.OffsetDateTime;
  2. public class OffsetDateTimeExample {
  3.     public static void main(String[] args) {
  4.         OffsetDateTime offsetDT = OffsetDateTime.now();
  5.         System.out.println(offsetDT.toLocalDate());
  6.     }
  7. }

Output:

2017-01-18

Java OffsetDateTime class Example: minusDays()

  1. import java.time.OffsetDateTime;
  2. public class OffsetDateTimeExample {
  3.   public static void main(String[] args) {
  4.     OffsetDateTime offset = OffsetDateTime.now();
  5.     OffsetDateTime value =  offset.minusDays(240);
  6.     System.out.println(value);
  7.   }
  8. }

Output:

2016-05-23T12:12:31.642+05:30	

Java OffsetDateTime class Example: plusDays()

  1. import java.time.OffsetDateTime;
  2. public class OffsetDateTimeExample {
  3.   public static void main(String[] args) {
  4.     OffsetDateTime offset = OffsetDateTime.now();
  5.     OffsetDateTime value =  offset.plusDays(240);
  6.     System.out.println(value);
  7.   }
  8. }

Output:

2017-09-15T13:50:30.526+05:30
Next TopicJava Clock
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Java OffsetDateTime class Java OffsetDateTime class is an immutable representation of a date-time with an offset. It inherits Object class and implements the Comparable interface. OffsetDateTime class is used to store the date and time fields, to a precision of nanoseconds. Java OffsetDateTime class declaration Let’s see the declaration of java.time.OffsetDateTime class. public final class OffsetDateTime extends Object implements Temporal, TemporalAdjuster, Comparable<OffsetDateTime>, Serializable Methods […]

Tags:

....
...