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>
该类继承了java包的Object类。这样,它还实现了下面给出的许多接口:
这是一个不包含任何方法的标记接口。实现这个接口有助于告诉Java OffsetDateTime支持序列化和反序列化,这意味着它的对象可以轻松转换为字节流。此外,字节流可以转换为实际的 Java 对象。
它是框架级别的接口,用于定义其对象的读写访问。 OffsetDateTime 使用此接口使其足以完成加减操作。
该接口提供了修改 Temporal 对象的工具,例如调整必须设置为该月最后一天的日期。实现此接口允许 OffsetDateTime 根据业务的设计模式在外部调整它。
此接口有助于根据类的字段之一对类的对象进行排序。为此,它提供了一个 comapreTo(Object) 函数,允许对对象进行排序。因此,使用此函数可以快速对 OffsetDateTime 对象进行排序。
OffsetDateTime、ZonedDateTime 和 Instant 是 Java8 类,可帮助存储精度高达纳秒的时间瞬间,如下所示:
该类没有可访问的构造函数;它是最终的且不可改变的。因此(==),禁止在其对象上使用身份哈希码。这种类型的类也称为基于值的类。因此,.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’ |
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.
以上是Java 偏移日期时间的详细内容。更多信息请关注PHP中文网其他相关文章!