OffsetDateTime is introduced in Java8 to store data and time fields with more accuracy and precision, which is helpful while transferring data from different mediums. These fields store DateTime values with precision 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 java. Time package and has java.lang.Object as its superclass. Adding offset from UTC/Greenwich also allows us to obtain local date-time. Thus proves to be the best while communicating with databases or over a network.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
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<OffsetDateTime>
This class inherits the Object class of the java package. With this, it also implements many interfaces given below:
This is a marker interface that does not contain any method. Implementing this interface helps to tell Java that OffsetDateTime supports serialization and deserialization, which means its objects can be easily converted into a byte stream. Also, byte streams can be converted to actual Java objects.
It is an interface 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.
This interface provides tools to modify the Temporal objects, such as adjusting the date that must be set to the last day of the month. Implementing this interface allows OffsetDateTime to adjust it externally per the business’s design pattern.
This interface helps to order the objects of the class based on one of its fields. For this, it gives a comapreTo(Object) function that allows sorting the objects. Thus OffsetDateTime objects can quickly be sorted using this function.
OffsetDateTime, ZonedDateTime, and Instant are Java8 classes that help to store instants of time with a precision of up to nanoseconds that are given below:
This class does not have accessible constructors; it is final and immutable. Thus (==), identity hashcode use on its objects is prohibited. Such a type of class is also known as a 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:
|
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’ |
Let’s see some of the Java OffsetDateTime methods.
It is used to get the int value from a date-time field.
You can use this function to retrieve the numerical value of the day in a given month, and it will return a value between – 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:
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:
This function returns an enum of DayOfWeek to tell which day of the week is specified. Enum consists of int value and the names of the week that help avoid confusion about 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:
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:
This function returns the current date-time from the system clock in the 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:
This function returns the OffsetDateTime object after adding the specified number of days to 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:
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:
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:
This function helps convert the object to ZonedDateTime type, 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:
The OffsetDateTime class introduces the storage of date-time fields with precision up to nanoseconds. It utilizes an offset of UTC/Greenwich in the ISO calendar system. These options find the highest preference when working with databases or transferring data over the network. It supports many functions to extract different information in different formats.
The above is the detailed content of Java OffsetDateTime. For more information, please follow other related articles on the PHP Chinese website!