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 無盡。

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SublimeText3 Linux新版
SublimeText3 Linux最新版

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

WebStorm Mac版
好用的JavaScript開發工具