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中文网其他相关文章!