Java 8 中 GMT 的区域 ID 是什么
What is the Zone Id for GMT in Java 8
我想获得以秒为单位的差异,以确定系统时区是领先还是落后于远程时区。这里的远程时区值是我从数据库中获取的“GMT”。它可能是“US/Eastern”,我将其转换为“America/New_York”。但是对于格林威治标准时间,我收到错误。
ZoneId.systemDefault().getRules().getOffset(Instant.now()).getTotalSeconds()
- ZoneId.of("GMT").getRules().getOffset(Instant.now()).getTotalSeconds()
但是报如下错误,
Exception in thread "main" java.time.DateTimeException: Invalid ID for ZoneOffset, invalid format:
at java.time.ZoneOffset.of(Unknown Source)
at java.time.ZoneId.of(Unknown Source)
at java.time.ZoneId.of(Unknown Source)
如何解决这个错误?
用什么代替 GMT ??
这是ZoneId.of("UTC")
...
证据如下:
public static void main(String[] args) {
// define the ZoneId
ZoneId utc = ZoneId.of("UTC");
// get the current date and time using that zone
ZonedDateTime utcNow = ZonedDateTime.now(utc);
// define a formatter that uses O for GMT in the output
DateTimeFormatter gmtStyleFormatter = DateTimeFormatter
.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSS O");
// and print the datetime using the default DateTimeFormatter and the one defined above
System.out.println(utcNow + " == " + utcNow.format(gmtStyleFormatter));
}
输出(不久前):
2020-09-16T08:02:34.717Z[UTC] == 2020-09-16T08:02:34.717 GMT
通过
获得此区域的the total zone offset in seconds
utc.getRules().getOffset(Instant.now()).getTotalSeconds();
将导致 0
,因为该区域没有偏移量。
使用Etc/UTC
import java.time.Instant;
import java.time.ZoneId;
public class Main {
public static void main(String args[]) {
System.out.println(ZoneId.of("Etc/UTC").getRules().getOffset(Instant.now()).getTotalSeconds());
}
}
输出:
0
官方答案是:
Etc/GMT
除名称外,与其他答案中建议的Etc/UTC相同。
为了完整起见,有许多相同的别名,其中许多已弃用,但不是全部。您可以在 link.
中找到它们
而且我并不反对告诉您更喜欢 UTC 的评论。我只是想按要求回答问题。
对于你的情况,你根本不需要问。 ZoneId.of("GMT").getRules().getOffset(Instant.now()).getTotalSeconds()
应始终产生 0,因此无需减去任何内容。我要么插入一个注释为什么我不这样做,要么用值 0.
减去一个 well-named 常量
我想获得以秒为单位的差异,以确定系统时区是领先还是落后于远程时区。这里的远程时区值是我从数据库中获取的“GMT”。它可能是“US/Eastern”,我将其转换为“America/New_York”。但是对于格林威治标准时间,我收到错误。
ZoneId.systemDefault().getRules().getOffset(Instant.now()).getTotalSeconds()
- ZoneId.of("GMT").getRules().getOffset(Instant.now()).getTotalSeconds()
但是报如下错误,
Exception in thread "main" java.time.DateTimeException: Invalid ID for ZoneOffset, invalid format:
at java.time.ZoneOffset.of(Unknown Source)
at java.time.ZoneId.of(Unknown Source)
at java.time.ZoneId.of(Unknown Source)
如何解决这个错误? 用什么代替 GMT ??
这是ZoneId.of("UTC")
...
证据如下:
public static void main(String[] args) {
// define the ZoneId
ZoneId utc = ZoneId.of("UTC");
// get the current date and time using that zone
ZonedDateTime utcNow = ZonedDateTime.now(utc);
// define a formatter that uses O for GMT in the output
DateTimeFormatter gmtStyleFormatter = DateTimeFormatter
.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSS O");
// and print the datetime using the default DateTimeFormatter and the one defined above
System.out.println(utcNow + " == " + utcNow.format(gmtStyleFormatter));
}
输出(不久前):
2020-09-16T08:02:34.717Z[UTC] == 2020-09-16T08:02:34.717 GMT
通过
获得此区域的the total zone offset in secondsutc.getRules().getOffset(Instant.now()).getTotalSeconds();
将导致 0
,因为该区域没有偏移量。
使用Etc/UTC
import java.time.Instant;
import java.time.ZoneId;
public class Main {
public static void main(String args[]) {
System.out.println(ZoneId.of("Etc/UTC").getRules().getOffset(Instant.now()).getTotalSeconds());
}
}
输出:
0
官方答案是:
Etc/GMT
除名称外,与其他答案中建议的Etc/UTC相同。
为了完整起见,有许多相同的别名,其中许多已弃用,但不是全部。您可以在 link.
中找到它们而且我并不反对告诉您更喜欢 UTC 的评论。我只是想按要求回答问题。
对于你的情况,你根本不需要问。 ZoneId.of("GMT").getRules().getOffset(Instant.now()).getTotalSeconds()
应始终产生 0,因此无需减去任何内容。我要么插入一个注释为什么我不这样做,要么用值 0.