Java8 中引入了 OffsetDateTime 来更准确和精确地存储数据和时间字段,这在从不同介质传输数据时很有帮助。这些字段存储精度高达纳秒的日期时间值;此外,这些字段与 UTC/格林威治有偏移。它是日期时间和偏移量的不可变表示。这个类属于java。 Time 包并以 java.lang.Object 作为其超类。添加与 UTC/格林威治的偏移量还允许我们获取本地日期时间。因此,在与数据库或通过网络通信时,事实证明这是最好的。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
语法:
下面是 OffsetDateTime 类的语法,它是 java.lang.OffsetDateTime 的成员。时间课。
public final class OffsetDateTime extends Object implements Serializable, Temporal, TemporalAdjuster, Comparable<offsetdatetime></offsetdatetime>
Java OffsetDateTime 接口
该类继承了java包的Object类。这样,它还实现了下面给出的许多接口:
1.可序列化
这是一个不包含任何方法的标记接口。实现这个接口有助于告诉Java OffsetDateTime支持序列化和反序列化,这意味着它的对象可以轻松转换为字节流。此外,字节流可以转换为实际的 Java 对象。
2.颞部
它是框架级别的接口,用于定义其对象的读写访问。 OffsetDateTime 使用此接口使其足以完成加减操作。
3.时间调整器
该接口提供了修改 Temporal 对象的工具,例如调整必须设置为该月最后一天的日期。实现此接口允许 OffsetDateTime 根据业务的设计模式在外部调整它。
4.可比较
此接口有助于根据类的字段之一对类的对象进行排序。为此,它提供了一个 comapreTo(Object) 函数,允许对对象进行排序。因此,使用此函数可以快速对 OffsetDateTime 对象进行排序。
OffsetDateTime、ZonedDateTime 和 Instant 是 Java8 类,可帮助存储精度高达纳秒的时间瞬间,如下所示:
- 即时:这是三者中最简单的。 OffsetDateTime 类将 UTC/格林威治的偏移量添加到即时,这有助于获取本地日期时间。 zonedDateTime 类添加了全时区规则。
- ZonedDateTime:使用更简单,并且完全支持夏令时,这有助于更轻松地进行夏令时调整。它使用 ISO-8601 日历系统的本地时间偏移。
- OffsetDateTime:此类与 ZonedDateTime 类似,但使用 ISO-8601 日历系统中 UTC/格林威治的偏移量。主要是,这是处理数据库和网络时的首选通信方法。
该类没有可访问的构造函数;它是最终的且不可改变的。因此(==),禁止在其对象上使用身份哈希码。这种类型的类也称为基于值的类。因此,.equals() 方法是比较此类类的首选方法。例如,OffsetDateTime 中存储的值可以表示为“2nd October 2007 at 13:45.30.123456789 +02: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()); } }
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.
以上是Java 偏移日期时间的详细内容。更多信息请关注PHP中文网其他相关文章!

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

WebStorm Mac版
好用的JavaScript开发工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Dreamweaver Mac版
视觉化网页开发工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。