Java OffsetDateTime
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>
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:
|
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()); } }
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 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:
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 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:
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)); } }
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 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:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.
