Heim > Java > javaLernprogramm > Java OffsetDateTime

Java OffsetDateTime

WBOY
Freigeben: 2024-08-30 15:51:45
Original
397 Leute haben es durchsucht

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>
Nach dem Login kopieren

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’

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());
}
}
Nach dem Login kopieren

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());
}
}
Nach dem Login kopieren

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());
}
}
Nach dem Login kopieren

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)); } }
Nach dem Login kopieren

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());
}
}
Nach dem Login kopieren

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)); } }
Nach dem Login kopieren

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());
}
}
Nach dem Login kopieren

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());
}
}
Nach dem Login kopieren

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());
}
}
Nach dem Login kopieren

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.

Das obige ist der detaillierte Inhalt vonJava OffsetDateTime. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Quelle:php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage