Home > Java > javaTutorial > body text

Java OffsetDateTime

WBOY
Release: 2024-08-30 15:51:45
Original
357 people have browsed it

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>
Copy after login

Interfaces of Java OffsetDateTime

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

1. Serializable

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.

2. Temporal

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.

3. TemporalAdjuster

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.

4. Comparable

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:

  • Instant: It is the simplest of all three. OffsetDateTime class adds the offset from UTC/Greenwich to the instant, which helps to obtain local date-time. The zonedDateTime class adds full-time-zone rules.
  • ZonedDateTime: It is simpler to use and is fully DST-aware which helps in daylight saving adjustments much easier. It uses a local time offset from the ISO-8601 calendar system.
  • OffsetDateTime: This class is similar to ZonedDateTime but uses offset from UTC/Greenwich in the ISO-8601 calendar system. Mainly, this is the preferred method of communication when dealing with databases and networks.

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:

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’

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()

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());
}
}
Copy after login

Output:

Java OffsetDateTime

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());
}
}
Copy after login

Output:

Java OffsetDateTime

4. DayOfWeek getDayOfWeek()

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());
}
}
Copy after login

Output:

Java OffsetDateTime

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)); } }
Copy after login

Output:

Java OffsetDateTime

6. Static OffsetDateTime now()

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());
}
}
Copy after login

Output:

Java OffsetDateTime

7. OffsetDateTime plusDays(long days)

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)); } }
Copy after login

Output:

Java OffsetDateTime

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());
}
}
Copy after login

Output:

Java OffsetDateTime

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());
}
}
Copy after login

Output:

Java OffsetDateTime

10. ZonedDateTime toZonedDateTime()

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());
}
}
Copy after login

Output:

Java OffsetDateTime

Conclusion

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!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!