首頁 > Java > java教程 > Java 區域 ID

Java 區域 ID

PHPz
發布: 2024-08-30 15:50:07
原創
755 人瀏覽過

ZoneId 是 Java 中的一個類別。 time 包,引入該包是為了指定 Instant 和 LocalDateTime 之間轉換所需的規則。該類別是 ZoneOffset 類別的子類,也可序列化,因為它實作了 Serialized 介面。此類別使用特定格式,僅用於儲存相對於 UTC/格林威治的偏移量。作為基於值的類,使用身份敏感操作可能會導致不可預測的結果。此類別代表大多數固定偏移 ID,它們對所有本地日期時間使用相同的偏移。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

Java ZoneId 語法

以下是java zoneid的語法:

文法:

public abstract class ZoneId
extends Object
implements Serializable
登入後複製

欄位: public static final Map; SHORT_IDS – 這是一個不可修改的映射,由符合 TZDB 2005r 及更高版本的 ID 映射組成。

這張地圖如下:

  • 東部時間: -05:00
  • HST: -10:00
  • MST: -07:00
  • ACT:澳洲/達爾文
  • AET:澳洲/雪梨
  • AGT:美國/阿根廷/布宜諾斯艾利斯
  • 藝術:非洲/開羅
  • AST: 美洲/安克雷奇
  • BET:美國/聖保羅
  • BST: 亞洲/達卡
  • 貓:非洲/哈拉雷
  • CNT: 美國/聖約翰斯
  • CST:美國/芝加哥
  • CTT: 亞洲/上海
  • 吃:非洲/亞的斯亞貝巴
  • ECT:歐洲/巴黎
  • IET:美國/印第安納州/印第安納波利斯
  • IST: 亞洲/加爾各答
  • JST:亞洲/東京
  • 麻省理工學院:太平洋/阿皮亞
  • NET: 亞洲/埃里溫
  • NST:太平洋/奧克蘭
  • PLT: 亞洲/卡拉奇
  • PNT:美國/鳳凰城
  • PRT: 美洲/波多黎各
  • 太平洋標準時間: 美洲/洛杉磯
  • SST:太平洋/瓜達爾卡納爾島
  • VST: 亞洲/胡志明

Java ZoneId 方法

java zoneid 的方法如下:

1.公共靜態 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.公用靜態集; getAvailableZoneIds()

此函數用於傳回所有可用的基於區域的 ID。該列表作為一組字串返回。傳回的字串可以使用 Of(String) 函數轉換為 Zone-Id 的格式。 Offset-Ids 不包含在一組字串的結果中。

代碼:

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(String zoneId)

此函數傳回 ZoneID 或 ZoneOffset,以取得作為字串作為輸出傳遞給函數的有效 ID。 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(String prefix,ZoneOffset offset)

當前綴和偏移量作為參數傳遞時,此方法用於取得 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中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板