📅  最后修改于: 2020-10-01 06:37:52             🧑  作者: Mango
Java ZoneId类指定时区标识符,并提供在Instant和LocalDateTime之间进行转换的规则。它继承了Object类并实现了Serializable接口。
我们来看一下java.time.ZoneId类的声明。
public abstract class ZoneId extends Object implements Serializable
Method | Description |
---|---|
String getDisplayName(TextStyle style, Locale locale) | It is used to get the textual representation of the zone, such as ‘India Time’ or ‘+05:30’. |
abstract String getId() | It is used to get the unique time-zone ID. |
static ZoneId of(String zoneId) | It is used to obtain an instance of ZoneId from an ID ensuring that the ID is valid and available for use. |
static ZoneId systemDefault() | It is used to get the system default time-zone. |
boolean equals(Object obj) | It is used to check if this time-zone ID is equal to another time-zone ID. |
import java.time.*;
public class ZoneIdExample1 {
public static void main(String... args) {
ZoneId zoneid1 = ZoneId.of("Asia/Kolkata");
ZoneId zoneid2 = ZoneId.of("Asia/Tokyo");
LocalTime id1 = LocalTime.now(zoneid1);
LocalTime id2 = LocalTime.now(zoneid2);
System.out.println(id1);
System.out.println(id2);
System.out.println(id1.isBefore(id2));
}
}
输出:
14:28:58.230
17:58:58.230
true
import java.time.ZoneId;
public class ZoneIdExample2 {
public static void main(String[] args) {
ZoneId zone = ZoneId.systemDefault();
System.out.println(zone);
}
}
输出:
Asia/Kolkata
import java.time.ZoneId;
public class ZoneIdExample3 {
public static void main(String[] args) {
ZoneId z = ZoneId.systemDefault();
String s = z.getId();
System.out.println(s);
}
}
输出:
Asia/Kolkata
import java.util.Locale;
import java.time.ZoneId;
import java.time.format.TextStyle;
public class ZoneIdExample4 {
public static void main(String[] args) {
ZoneId z = ZoneId.systemDefault();
System.out.println(z.getDisplayName(TextStyle.FULL, Locale.ROOT));
}
}
输出:
India Time