Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Java OffsetDateTime

Introduction to Java OffsetDateTime

OffsetDateTime is introduced in Java8 to store data and time fields with more accuracy and precision that proves to be helpful while transferring data from different mediums. These fields are stored with a precision of DateTime up to nanoseconds also these fields have an offset from UTC/Greenwich. It is an immutable representation of a date-time along with an offset. This class belongs to the java. Time package and has java.lang.Object as its superclass. The addition of offset from UTC/Greenwich also allows us to obtain local date-time. Thus proves to be the best while communicating with the databases or over a network.

Syntax:

Below is the syntax of OffsetDateTime class which is a member of java. Time class.

public final class OffsetDateTime extends Object implements Serializable, Temporal, TemporalAdjuster, Comparable

Interfaces of Java OffsetDateTime

This class inherits the Object class of java package. With this, it also implements many interfaces given below:

1. Serializable

This is a marker interface that it does not contain any method. Implementing this interface helps to tell Java that OffsetDateTime supports serialization and deserialization that means its objects can be easily converted into a byte stream and also byte stream can be converted to actual Java objects.

2. Temporal

It is an interface used on the framework level to define read-write access for its Objects. OffsetDateTime uses this interface to make it complete enough for plus-minus manipulations.

3. TemporalAdjuster

This interface provides tools to modify the Temporal objects such as making adjustments to date that must be set to the last day of the month. Implementing this interface allows OffsetDateTime to provide a way for making adjustments to it externally as per the design pattern of business.

4. Comparable

This is an interface that helps to order the objects of the class on the basis of one of its fields. For this, it gives a function comapreTo(Object) that helps to sort the objects. Thus OffsetDateTime objects can easily be sorted using this function.
OffsetDateTime, ZonedDateTime, and Instant are Java8 classes that help to store instant of time with a precision up to nanoseconds that are given below:

  • Instant: It is the simplest of all three. OffsetDateTime class adds to the instant the offset from UTC/Greenwich to the instant which helps to obtain local date-time. ZonedDateTime class adds full time-zone rules.
  • ZonedDateTime: It is simpler to use and is fully DST-aware which helps in daylights saving adjustments much easier. It uses a local time-offset from the ISO-8601 calendar system.
  • OffsetDateTime: This class is much similar to ZonedDateTime but uses offset from UTC/Greenwich in the ISO-8601 calendar system. Thus is mostly preferred which communicates over databases as well as over the networks.

This class does not have accessible constructors as well as it is final and immutable. Thus (==), identity hashcode use on its objects is prohibited. Such type of classes is also known as value-based class. Thus .equals() method is preferred for the comparisons of such classes. For example, the value stored in OffsetDateTime can be represented as”2nd October 2007 at 13:45.30.123456789 +02:00″.

Fields:

Field Name Description
MAX It is a static field of OffsetDateTime type which stores the maximum supported value that is.

‘+999999999-12-31T23:59:59.999999999-18:00’

MIN It is a static field of OffsetDateTime type which stores the maximum supported value that is.

‘-999999999-01-01T00:00:00+18:00’

Methods of Java OffsetDateTime

Let’s see some of the Java OffsetDateTime methods.

1. int get(TemporalField field)

It is used to get the int value from a date-time field.

2. int getDayOfMonth()

It is used to extract the value of Day of the month field in its object. Return value – 1 to 31.

Code:

import java.time.OffsetDateTime;
public class Demo {
public static void main(String[] args) {
OffsetDateTime mydate = OffsetDateTime.parse("2020-01-26T12:30:30+01:00");
System.out.println("DayOfMonth output - "+mydate.getDayOfMonth());
}
}

Output:

3. int getDayOfYear()

This function gives the day of the year field from the value specified. Its output is an integer within the range of 1 to 365.

Code:

import java.time.OffsetDateTime;
public class HelloWorld{
public static void main(String []args){
OffsetDateTime mydate = OffsetDateTime.parse("2020-02-26T12:30:30+01:00");
System.out.println("DayOfYear output - "+mydate.getDayOfYear());
}
}

Output:

4. DayOfWeek getDayOfWeek()

This function returns an enum of DayOfWeek to tell which day of the week is specified. Enum consists of int value as well the names of the week that helps to avoid the confusion of what the number represents.

Code:

import java.time.OffsetDateTime;
public class Main{
public static void main(String []args){
OffsetDateTime mydate = OffsetDateTime.parse("2020-02-26T12:30:30+01:00");
System.out.println("DayOfWeek output - "+ mydate.getDayOfWeek());
}
}

Output:

5. OffsetDateTime minusDays(long days)

This function returns the OffsetDateTime object after subtracting the specified number of days from it.

Code:

import java.time.OffsetDateTime;
public class Main{
public static void main(String []args){
OffsetDateTime mydate = OffsetDateTime.parse("2020-02-26T12:30:30+01:00");
System.out.println("minusDays output - "+ mydate.minusDays(2)); } }

Output:

6. Static OffsetDateTime now()

This function returns the current date-time from system clock in time zone. Return type if OffsetDateTime only.

Code:

import java.time.OffsetDateTime;
public class Main{
public static void main(String []args){
OffsetDateTime mydate = OffsetDateTime.parse("2020-02-26T12:30:30+01:00");
System.out.println("now output - "+ mydate.now());
}
}

Output:

7. OffsetDateTime plusDays(long days)

This function returns the OffsetDateTime object after adding the specified number of days in it.

Code:

import java.time.OffsetDateTime;
public class Main{
public static void main(String []args){
OffsetDateTime mydate = OffsetDateTime.parse("2020-02-26T12:30:30+01:00");
System.out.println("plusDays output - "+ mydate.plusDays(5)); } }

Output:

8. LocalDate toLocalDate()

This function returns the LocalDate part of date-time.

Code:

import java.time.OffsetDateTime;
public class Main{
public static void main(String []args){
OffsetDateTime mydate = OffsetDateTime.parse("2020-02-26T12:30:30+01:00");
System.out.println("toLocalDate output - "+ mydate.toLocalDate());
}
}

Output:

9. Offset toOffsetTime()

This function helps to convert date-time to Offset Time.

 Code:

import java.time.OffsetDateTime;
public class Main{
public static void main(String []args){
OffsetDateTime mydate = OffsetDateTime.parse("2020-02-26T12:30:30+01:00");
System.out.println("toOffsetTime output - "+ mydate.toOffsetTime());
}
}

Output:

10. ZonedDateTime toZonedDateTime()

This function helps to convert the object to ZonedDateTime type which is a fully DST aware date-time representation that handles daylight saving conversion much easier.

Code:

import java.time.OffsetDateTime;
public class Main{
public static void main(String []args){
OffsetDateTime mydate = OffsetDateTime.parse("2020-02-26T12:30:30+01:00");
System.out.println("toZonedDateTime output - "+ mydate.toZonedDateTime());
}
}

Output:

Conclusion

OffsetDateTime class is introduced to store date-time field with precision up to nanoseconds and uses an offset of UTC/Greenwich in the ISO calendar system. These are most often preferred while working with databases or transfer of data over the network. It supports many functions to extract different information in different formats.

Recommended Articles

This is a guide to the Java OffsetDateTime. Here we discuss syntax, interfaces, and methods of Java OffsetDateTime along with code implementation. You may also look at the following articles to learn more-

  1. Examples of JavaScript Get Array Length
  2. Comparison Operators in Java
  3. How Private Constructor Works in Java?
  4. Instance Variable in Java

The post Java OffsetDateTime appeared first on EDUCBA.



This post first appeared on Free Online CFA Calculator Training Course | EduCB, please read the originial post: here

Share the post

Java OffsetDateTime

×

Subscribe to Free Online Cfa Calculator Training Course | Educb

Get updates delivered right to your inbox!

Thank you for your subscription

×