首页 >Java >java教程 >Java 区域 ID

Java 区域 ID

PHPz
PHPz原创
2024-08-30 15:50:07755浏览

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

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn