Java的.time.ZoneId类在Java中
ZoneId 用于标识用于在 LocalDateTime 和 Instant of time 之间转换的规则。描述偏移量变化的时间和方式的实际规则由 ZoneRules 定义。这个类只是一个 ID,用来获取底层规则。之所以选择给定的方法,是因为政府定义了规则并经常更改,而 ID 是稳定的。
有两种不同类型的 ID:
- 固定偏移量是从 UTC/格林威治完全解析的偏移量,它对所有本地日期时间使用相同的偏移量
- 地理区域是一个社区,其中适用一组选定的用于定位 UTC/格林威治偏移量的规则
Declaration of java.time.ZoneId class
public abstract class ZoneId extends Object implements Serializable
区域映射覆盖以启用短时区名称。 Java.util.TimeZone 中已弃用短区域 ID。此映射允许仍通过 of(String, Map) 工厂方法使用 ID。此映射包含符合 TZDB 2005r 及更高版本的 ID 映射,其中“EST”、“MST”和“HST”映射到不包含夏令时的 ID。
这映射如下: EST HST MST ACT AET AGT ART AST BET BST CAT CNT CST CTT EAT ECT IET IST JST MIT NET NST PLT PNT PRT PST SST 05:00 10:00 07:00 Australia/Darwin Australia/Sydney America/Argentina/Buenos_Aires Africa/Cairo America/Anchorage America/Sao_Paulo Asia/Dhaka Africa/Harare America/St_Johns America/Chicago Asia/Shanghai Africa/Addis_Ababa Europe/Paris America/Indiana/Indianapolis Asia/Kolkata Asia/Tokyo Pacific/Apia Asia/Yerevan Pacific/Auckland Asia/Karachi America/Phoenix America/Puerto_Rico America/Los_Angeles Pacific/Guadalcanal VST Asia/Ho_Chi_Minh
注意:地图不可修改。
ZoneId 类的方法:Methods Description equals(Object obj) This method checks if this time-zone ID is equal to another time-zone ID. from(TemporalAccessor temporal) This method obtains an instance of ZoneId from a temporal object. getAvailableZoneIds() This method gets the set of available zone IDs. getDisplayName(TextStyle style, Locale locale) This method gets the textual representation of the zone, such as ‘British Time’ or ‘+02:00’. getId() This method gets the unique time-zone ID. getRules() This method gets the time-zone rules for this ID allowing calculations to be performed. hashCode() A hash code for this time-zone ID. normalized() This method normalizes the time-zone ID, returning a ZoneOffset where possible. of(String zoneId) This method obtains an instance of ZoneId from an ID ensuring that the ID is valid and available for use. of(String zoneId, Map This method obtains an instance of ZoneId using its ID using a map of aliases to supplement the standard zone IDs. ofOffset(String prefix, ZoneOffset offset) This method obtains an instance of ZoneId wrapping an offset. systemDefault() This method gets the system default time-zone. toString() This method outputs this zone as a String, using the ID.
下面是一些方法的实现示例:
Java
// java.time.ZoneId Class in Java with example
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// Setting Zone1
ZoneId zoneid1 = ZoneId.of("Asia/Kolkata");
// Setting Zone2
ZoneId zoneid2 = ZoneId.of("Europe/London");
LocalTime time1 = LocalTime.now(zoneid1);
LocalTime time2 = LocalTime.now(zoneid2);
System.out.println(time1);
System.out.println(time2);
// Checking if the time of zone1
// comes before time of second zone
System.out.println(time1.isBefore(time2));
}
}
22:34:11.044312
17:04:11.044385
false