Java ゾーン ID

PHPz
リリース: 2024-08-30 15:50:07
オリジナル
684 人が閲覧しました

ZoneId は Java のクラスです。 time パッケージは、Instant と LocalDateTime の間の変換に必要なルールを指定するために導入されました。このクラスは ZoneOffset クラスのサブクラスであり、Serializable インターフェイスを実装しているためシリアル化も可能です。このクラスは、UTC/グリニッジからのオフセットのみを格納するために使用される特定の形式を使用します。値ベースのクラスであるため、ID に依存した操作を使用すると、予測できない結果が生じる可能性があります。このクラスは、すべてのローカル日時に対して同じオフセットを使用する固定オフセット ID のほとんどを表します。

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

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

Java ZoneId の構文

以下は Java ゾーン ID の構文です:

構文:

public abstract class ZoneId
extends Object
implements Serializable
ログイン後にコピー

フィールド: public static Final Map SHORT_IDS – これは、TZDB 2005r 以降に準拠した ID のマッピングで構成される変更不可能なマップです。

この地図は次のとおりです:

  • EST: -05:00
  • HST: -10:00
  • MST: -07:00
  • 行為: オーストラリア/ダーウィン
  • AET: オーストラリア/シドニー
  • AGT: アメリカ/アルゼンチン/ブエノスアイレス
  • アート: アフリカ/カイロ
  • AST: アメリカ/アンカレッジ
  • 賭け: アメリカ/サンパウロ
  • BST: アジア/ダッカ
  • 猫: アフリカ/ハラレ
  • CNT: アメリカ/セントジョンズ
  • CST: アメリカ/シカゴ
  • CTT: アジア/上海
  • 食べる: アフリカ/アディスアベバ
  • ECT: ヨーロッパ/パリ
  • IET: アメリカ/インディアナ/インディアナポリス
  • IST: アジア/コルカタ
  • JST: アジア/東京
  • MIT: パシフィック/アピア
  • NET: アジア/エレバン
  • NST: 太平洋/オークランド
  • PLT: アジア/カラチ
  • PNT: アメリカ/フェニックス
  • PRT: アメリカ/プエルトリコ
  • 太平洋標準時: アメリカ/ロサンゼルス
  • 海面標準時間: 太平洋/ガダルカナル
  • VST: アジア/ホーチミン

Java ZoneId のメソッド

Java ゾーン ID のメソッドを以下に示します:

1. public static ZoneId systemDefault()

この関数は、システムのデフォルトのタイムゾーンを取得するために使用されます。これにより、TimeZone.getDefault() 関数が呼び出され、そのシステムのデフォルトのタイムゾーンの値が取得されます。これは、システムのデフォルトのタイムゾーンに加えられた更新が結果に反映されることを意味します。出力は ZoneID 形式に変換されます。この関数によってスローされる以下の 2 つの例外を処理する必要があります:-

  • DateTimeException: これは、変換されたゾーン ID の形式が無効であることを示します
  • ZoneRulesException: これは、変換されたゾーン領域 ID が見つからないことを示します

コード:

import java.time.ZoneId;
public class Main {
public static void main(String[] args) {
ZoneId zone = ZoneId.systemDefault();
System.out.println("Output of systemDefault()-" +zone);
}
}
ログイン後にコピー

出力:

Java ゾーン ID

2. public static Set getAvailableZoneIds()

この関数は、利用可能なすべての地域ベースの ID を返すために使用されます。リストは文字列のセットとして返されます。返された文字列は、Of(String) 関数を使用してゾーン ID の形式に変換できます。 Offset-Id は文字列のセットの結果には含まれません。

コード:

import java.time.ZoneId;
import java.util.*;
public class Main {
public static void main(String[] args) {
Set<String> zoneIds = ZoneId.getAvailableZoneIds();
List<String> zoneList = new ArrayList<String>(zoneIds);
Collections.sort(zoneList);
for (int i = 0; i < 5; i++) {
System.out.println("ZoneId in list:" +zoneList.get(i) );
}
}
}
ログイン後にコピー

出力:

Java ゾーン ID

3. public static ZoneId of(StringzoneId)

この関数は、関数への出力として文字列として渡された有効な ID の ZoneID または ZoneOffset を返します。 ZoneOffset は、文字列の開始文字 (「Z」、「+」、「-」の場合) に基づいて返されます。返される ZoneID は常に ZoneRules に従います。この変換は、完全な解析アルゴリズムを使用して行われます。引数の形式が不正な場合はDateTimeExceptionがスローされ、リージョンの場合はIDが見つからない場合はZoneRulesExceptionがスローされます。

コード:

import java.time.*;
import java.util.*;
import java.time.format.TextStyle;
public class Main {
public static void main(String[] args) {
ZoneId zoneId  = ZoneId.of("Asia/Calcutta");
System.out.println("Output of zoneid obtained using of function: "+ zoneId.normalized());
}
}
ログイン後にコピー

出力:

Java ゾーン ID

4. public static ZoneId ofOffset(文字列プレフィックス,ZoneOffsetオフセット)

このメソッドは、プレフィックスとオフセットが引数として渡されるときに、ZoneID を取得するために使用されます。プレフィックスとゼロ以外のオフセット ID を含むゾーン ID が返されます。この場合、プレフィックスは「」ZoneOffset が返されます。

プレフィックスは「GMT」、「UTC」、「UT」、または「」である必要があります。それ以外の場合は、メソッドによって IllegalArgumentException がスローされます。

コード:

import java.time.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
ZoneId zoneId = ZoneId.ofOffset("GMT", ZoneOffset.MAX);
System.out.println("OfOffset on ZoneId: " + zoneId);
}
}
ログイン後にコピー

Ouput:

Java ゾーン ID

5. public static ZoneId from(TemporalAccessor temporal)

This method is used to convert a TemporalAccessor, an arbitrary set of date and time, object to an instance of ZoneId. This function works in a similar manner to TemporalQueries.zone() that extracts offset base zone id. Thus this method matches the signature of Temporal Query that is being used via ZoneId::from() function.

Code:

import java.time.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
ZonedDateTime zoneddatetime
= ZonedDateTime.parse("2020-02-25T23:12:31.123+02:00[Europe/Paris]");
ZoneId result = ZoneId.from(zoneddatetime);
System.out.println("Zone Id got from "+ "TemporalAccessor object \n"
+ zoneddatetime + "\nis " + result);
}
}
ログイン後にコピー

Output:

Java ゾーン ID

6. public abstract String getId()

This function is used to get a unique time-zone ID for a particular object. The format for an offset-based ID is defined by the getId() method in the Timezone class. The ID given as an output helps to uniquely define the object.

Code:

import java.time.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
ZoneId zoneId  = ZoneId.of("Asia/Calcutta");
System.out.println("Id using getID(): "+ zoneId.getId());
}
}
ログイン後にコピー

Output:

Java ゾーン ID

7. public String getDisplayName(TextStyle style,Locale locale)

This method helps to provide the textual representation of the zone. The style required as well as the locale of the output required is given as arguments. And thus, the suitable textual representation of the time-zone id is given as an output. In case no textual representation is present, a complete ID is returned as an output.

Code:

import java.time.*;
import java.util.*;
import java.time.format.TextStyle;
public class Main {
public static void main(String[] args) {
ZoneId zoneId  = ZoneId.of("Asia/Calcutta");
String result = zoneId.getDisplayName(TextStyle.SHORT, Locale.ENGLISH);
System.out.println("Name of ID using getDisplayName(): "+ result);
}
}
ログイン後にコピー

Output:

Java ゾーン ID

8. public abstract ZoneRules getRules()

This function is used to retrieve the rules that are applied for the given ID. These rules give an idea about the calculations to be performed as well as the functionality associated with time-zone like finding an offset for an instant. ZoneRulesProvider supplies these rules. An advanced provider can also make a dynamic update to these rules, but the results may change over time in this case. In case no rule is available or the rules of JRE is different from that of time-zone, a call made to this function may lead to ZoneRulesException.

Code:

import java.time.*;
import java.util.*;
import java.time.format.TextStyle;
public class Main {
public static void main(String[] args) {
ZoneId zoneId  = ZoneId.of("Asia/Calcutta");
System.out.println("Rules are: "+ zoneId.getRules());
}
}
ログイン後にコピー

Output:

Java ゾーン ID

9. public ZoneId normalized()

This method is used to check if the given zoneID contains a fixed Offset and if yes, a ZOneOffset equal to that fixed offset is returned; otherwise, this is returned. The returned ID will also have ZoneRules similar to the given offset, but the result we get from the getId() function is different from what we get here.

Code:

import java.time.*;
import java.util.*;
import java.time.format.TextStyle;
public class Main {
public static void main(String[] args) {
ZoneId zoneId  = ZoneId.of("Asia/Calcutta");
System.out.println("Normalized Id: "+ zoneId.normalized());
}
}
ログイン後にコピー

Output:

Java ゾーン ID

10. public boolean equals(Object obj)

This method helps to compare 2 different times ZoneId objects. This method overrides the equals method in the Object class, which is used to compare value stored in 2 objects. Similarly, here the value of these ZoneID is compared to see if 2 objects are equal or not. And, Accordingly, true and false is returned.

Code:

import java.time.*;
import java.util.*;
import java.time.format.TextStyle;
public class Main {
public static void main(String[] args) {
ZoneId zoneId  = ZoneId.of("Asia/Calcutta");
ZoneId zoneId2 = ZoneId.of("Europe/Paris");
System.out.println("Output of Equals: "+ zoneId.equals(zoneId2));
}
}
ログイン後にコピー

Output:

Java ゾーン ID

11. public int hashCode()

This function is used to get the hash code for a particular ZoneID object. This hashcode is returned in int format.

Code:

import java.time.*;
import java.util.*;
import java.time.format.TextStyle;
public class Main {
public static void main(String[] args) {
ZoneId zoneId  = ZoneId.of("Asia/Calcutta");
ZoneId zoneId2 = ZoneId.of("Europe/Paris");
System.out.println("Output of zoneid hashCode: "+ zoneId.hashCode());
System.out.println("Output of zoneid2 hashCode: "+ zoneId2.hashCode());
}
}
ログイン後にコピー

Output:

Java ゾーン ID

12. public String toString()

This function is used to get the string representation of the time -zone ID that is stored in the particular Offset ID. This function overrides the method toString() of Object class.

Code:

import java.time.*;
import java.util.*;
import java.time.format.TextStyle;
public class Main {
public static void main(String[] args) {
ZoneId zoneId  = ZoneId.of("Asia/Calcutta");
ZoneId zoneId2 = ZoneId.of("Europe/Paris");
System.out.println("Output of zoneid toString: "+ zoneId.toString());
System.out.println("Output of zoneid2 toString: "+ zoneId2.toString());
}
}
ログイン後にコピー

Output:

Java ゾーン ID

Conclusion

ZoneID is a serialized class that is used to store the IDs that identify different ZoneRules that the government frequently updates. Thus at the time of serialization, rules are used instead as they contain the entire data set. Also, calling normalized() on ZoneId returns fixed offset ID in the format of ZoneOffset.

以上がJava ゾーン IDの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!