ZoneId 是 Java 中的一個類別。 time 包,引入該包是為了指定 Instant 和 LocalDateTime 之間轉換所需的規則。該類別是 ZoneOffset 類別的子類,也可序列化,因為它實作了 Serialized 介面。此類別使用特定格式,僅用於儲存相對於 UTC/格林威治的偏移量。作為基於值的類,使用身份敏感操作可能會導致不可預測的結果。此類別代表大多數固定偏移 ID,它們對所有本地日期時間使用相同的偏移。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
以下是java zoneid的語法:
文法:
public abstract class ZoneId extends Object implements Serializable
欄位: public static final Map
這張地圖如下:
java zoneid 的方法如下:
此函數用於取得系統預設時區。這將呼叫 TimeZone.getDefault() 函數來檢索該系統預設時區的值,這表示對系統預設時區所做的任何更新都會反映在結果中。輸出轉換為 ZoneID 格式。必須處理此函數拋出的以下 2 個異常:-
代碼:
import java.time.ZoneId; public class Main { public static void main(String[] args) { ZoneId zone = ZoneId.systemDefault(); System.out.println("Output of systemDefault()-" +zone); } }
輸出:
此函數用於傳回所有可用的基於區域的 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) ); } } }
輸出:
此函數傳回 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()); } }
輸出:
當前綴和偏移量作為參數傳遞時,此方法用於取得 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:
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:
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:
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:
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:
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:
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:
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:
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:
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中文網其他相關文章!