ホームページ  >  記事  >  Java  >  Java オフセット日付時刻

Java オフセット日付時刻

WBOY
WBOYオリジナル
2024-08-30 15:51:45358ブラウズ

OffsetDateTime は、データと時刻のフィールドをより正確に保存するために Java8 で導入されました。これは、さまざまな媒体からデータを転送するときに役立ちます。これらのフィールドには、DateTime 値がナノ秒までの精度で保存されます。また、これらのフィールドには UTC/グリニッジからのオフセットがあります。これは、オフセットを伴う日付/時刻の不変表現です。このクラスは Java に属します。 Time パッケージであり、そのスーパークラスとして java.lang.Object があります。 UTC/グリニッジからのオフセットを追加すると、ローカルの日付/時刻を取得することもできます。したがって、データベースまたはネットワーク経由で通信する場合に最適であることがわかります。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

構文:

以下はJavaのメンバーであるOffsetDateTimeクラスの構文です。時間クラス。

public final class OffsetDateTime extends Object implements Serializable, Temporal, TemporalAdjuster, Comparable<OffsetDateTime>

Java OffsetDateTime のインターフェース

このクラスは、Java パッケージの Object クラスを継承します。これにより、以下に示す多くのインターフェースも実装されます:

1.シリアル化可能

これはメソッドを含まないマーカー インターフェイスです。このインターフェイスを実装すると、OffsetDateTime がシリアル化と逆シリアル化をサポートしていることを Java に伝えることができます。これは、そのオブジェクトをバイト ストリームに簡単に変換できることを意味します。また、バイト ストリームを実際の Java オブジェクトに変換することもできます。

2.時間的

これは、オブジェクトの読み取り/書き込みアクセスを定義するためのフレームワーク レベルのインターフェイスです。 OffsetDateTime は、このインターフェイスを使用して、プラスマイナス操作に十分な完全性を実現します。

3.時間調整者

このインターフェースは、月の末日に設定する必要がある日付を調整するなど、Temporal オブジェクトを変更するためのツールを提供します。このインターフェイスを実装すると、OffsetDateTime をビジネスの設計パターンに従って外部から調整できるようになります。

4.比較可能な

このインターフェースは、フィールドの 1 つに基づいてクラスのオブジェクトを順序付けるのに役立ちます。このために、オブジェクトを並べ替えることができる comapreTo(Object) 関数が提供されます。したがって、この関数を使用して OffsetDateTime オブジェクトをすばやく並べ替えることができます。
OffsetDateTime、ZonedDateTime、および Instant は、以下に示す最大ナノ秒の精度で時刻を保存するのに役立つ Java8 クラスです。

  • インスタント: 3 つの中で最も単純です。 OffsetDateTime クラスは、UTC/グリニッジからのオフセットをインスタントに追加します。これは、ローカルの日付/時間を取得するのに役立ちます。 zonedDateTime クラスは、フルタイムゾーンのルールを追加します。
  • ZonedDateTime: より使いやすく、DST を完全に認識しているため、夏時間の調整がはるかに簡単になります。 ISO-8601 カレンダー システムからの現地時間オフセットを使用します。
  • OffsetDateTime: このクラスは ZonedDateTime に似ていますが、ISO-8601 カレンダー システムの UTC/グリニッジからのオフセットを使用します。主に、これはデータベースやネットワークを扱うときに推奨される通信方法です。

このクラスにはアクセス可能なコンストラクターがありません。それは最終的であり、不変です。したがって (==)、そのオブジェクトでの ID ハッシュコードの使用は禁止されます。このようなタイプのクラスは、値ベースのクラスとも呼ばれます。したがって、そのようなクラスの比較には .equals() メソッドが推奨されます。たとえば、OffsetDateTime に格納されている値は、「2007 年 10 月 2 日 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’

フィールド名 説明 最大 これは 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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。