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中文網其他相關文章!