搜索
首页Javajava教程Java 区域 ID

Java 区域 ID

Aug 30, 2024 pm 03:50 PM
java

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 
<p><strong>输出:</strong></p>
<p><img  src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500421161710.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 区域 ID" ></p>
<h4 id="public-static-ZoneId-of-String-zoneId">3. public static ZoneId of(String zoneId)</h4>
<p>此函数返回 ZoneID 或 ZoneOffset,以获取作为字符串作为输出传递给函数的有效 ID。 ZoneOffset 根据字符串的起始字符(如果是“Z”或“+”或“-”)返回。返回的 ZoneID 始终遵循 ZoneRules。这种转换是使用完整的解析算法进行的。如果参数格式无效,则抛出 DateTimeException;如果是区域、ID 找不到,则抛出 ZoneRulesException。</p>
<p><strong>代码:</strong></p>
<pre class="brush:php;toolbar:false">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
如何将Maven或Gradle用于高级Java项目管理,构建自动化和依赖性解决方案?如何将Maven或Gradle用于高级Java项目管理,构建自动化和依赖性解决方案?Mar 17, 2025 pm 05:46 PM

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

如何使用适当的版本控制和依赖项管理创建和使用自定义Java库(JAR文件)?如何使用适当的版本控制和依赖项管理创建和使用自定义Java库(JAR文件)?Mar 17, 2025 pm 05:45 PM

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。

如何使用咖啡因或Guava Cache等库在Java应用程序中实现多层缓存?如何使用咖啡因或Guava Cache等库在Java应用程序中实现多层缓存?Mar 17, 2025 pm 05:44 PM

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

如何将JPA(Java持久性API)用于具有高级功能(例如缓存和懒惰加载)的对象相关映射?如何将JPA(Java持久性API)用于具有高级功能(例如缓存和懒惰加载)的对象相关映射?Mar 17, 2025 pm 05:43 PM

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

Java的类负载机制如何起作用,包括不同的类载荷及其委托模型?Java的类负载机制如何起作用,包括不同的类载荷及其委托模型?Mar 17, 2025 pm 05:35 PM

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。