首頁  >  文章  >  Java  >  Java 偏移日期時間

Java 偏移日期時間

WBOY
WBOY原創
2024-08-30 15:51:45360瀏覽

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 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’

欄位名稱 描述 MAX 它是一個 OffsetDateTime 類型的靜態字段,儲存的是支援的最大值。 ‘+999999999-12-31T23:59:59.999999999-18:00’ MIN 它是一個 OffsetDateTime 類型的靜態字段,儲存的是支援的最大值。 ‘-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:

Java 偏移日期時間

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:

Java 偏移日期時間

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:

Java 偏移日期時間

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:

Java 偏移日期時間

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:

Java 偏移日期時間

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:

Java 偏移日期時間

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:

Java 偏移日期時間

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:

Java 偏移日期時間

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:

Java 偏移日期時間

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

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:Java 本機日期下一篇:Java 本機日期