在 java 中查找系统时区是否早于或晚于 UTC
Find if system timezone is ahead or behind UTC in java
我需要确定我当前的系统时区是早于还是晚于 UTC 时区,我尝试了不同的方法,但问题是我想从系统中获取当前时区并将其与 UTC 进行比较。
有很多与此相关的问题,但是对于前后概念我没有找到。
我不是在问如何找到 zoneOffset。我只是想检查应用程序是领先还是落后于 UTC 时区。
你可以比较一下。区域偏移的秒数,例如
import java.time.Instant;
import java.time.ZoneOffset;
public class Main {
public static void main(String[] args) {
long offsetSecondsMyTZ = ZoneOffset.systemDefault().getRules().getOffset(Instant.now()).getTotalSeconds();
if (offsetSecondsMyTZ > 0) {
System.out.println("My timezone is ahead of UTC");
} else if (offsetSecondsMyTZ < 0) {
System.out.println("My timezone is behind UTC");
} else {
System.out.println("My timezone is UTC");
}
// Assuming my time-zone is UTC
offsetSecondsMyTZ = ZoneOffset.UTC.getTotalSeconds();
if (offsetSecondsMyTZ > 0) {
System.out.println("My timezone is ahead of UTC");
} else if (offsetSecondsMyTZ < 0) {
System.out.println("My timezone is behind UTC");
} else {
System.out.println("My timezone is UTC");
}
// Assuming my time-zone is UTC - 2 hours
offsetSecondsMyTZ = ZoneOffset.ofHours(-2).getTotalSeconds();
if (offsetSecondsMyTZ > 0) {
System.out.println("My timezone is ahead of UTC");
} else if (offsetSecondsMyTZ < 0) {
System.out.println("My timezone is behind UTC");
} else {
System.out.println("My timezone is UTC");
}
}
}
输出:
My timezone is ahead of UTC
My timezone is UTC
My timezone is behind UTC
注:本方案基于.
Arvind Kumar Avinash 已经给出了很好的回答。我的代码仅在细节上有所不同。
ZoneOffset myZoneOffset
= OffsetDateTime.now(ZoneId.systemDefault()).getOffset();
int diff = myZoneOffset.compareTo(ZoneOffset.UTC);
if (diff < 0) {
System.out.println("" + myZoneOffset + " is ahead of UTC");
} else if (diff > 0) {
System.out.println("" + myZoneOffset + " is behind UTC");
} else {
System.out.println("" + myZoneOffset + " is UTC");
}
当我运行它刚刚在Europe/Copenhagen时区时,输出是:
+02:00 is ahead of UTC
您可能首先认为 运行ge compareTo()
认为 +02:00 小于 UTC 的偏移量。我想逻辑是他们希望它在偏移量的自然排序中 在 UTC 之前出现,这至少对我来说是有道理的。
请注意偏移随时间变化。特别是在许多地方,它因夏令时 (DST) 而随季节变化。因此,如果您在某个地方看到一年中某个时间的偏移量在 UTC 之前或之后,那么它很可能在一年中的另一个时间等于 UTC,反之亦然。
最后ZoneId.systemDefault()
给我们JVM的默认时区,它通常与操作系统时区相同,但并非总是如此。
我需要确定我当前的系统时区是早于还是晚于 UTC 时区,我尝试了不同的方法,但问题是我想从系统中获取当前时区并将其与 UTC 进行比较。
有很多与此相关的问题,但是对于前后概念我没有找到。
我不是在问如何找到 zoneOffset。我只是想检查应用程序是领先还是落后于 UTC 时区。
你可以比较一下。区域偏移的秒数,例如
import java.time.Instant;
import java.time.ZoneOffset;
public class Main {
public static void main(String[] args) {
long offsetSecondsMyTZ = ZoneOffset.systemDefault().getRules().getOffset(Instant.now()).getTotalSeconds();
if (offsetSecondsMyTZ > 0) {
System.out.println("My timezone is ahead of UTC");
} else if (offsetSecondsMyTZ < 0) {
System.out.println("My timezone is behind UTC");
} else {
System.out.println("My timezone is UTC");
}
// Assuming my time-zone is UTC
offsetSecondsMyTZ = ZoneOffset.UTC.getTotalSeconds();
if (offsetSecondsMyTZ > 0) {
System.out.println("My timezone is ahead of UTC");
} else if (offsetSecondsMyTZ < 0) {
System.out.println("My timezone is behind UTC");
} else {
System.out.println("My timezone is UTC");
}
// Assuming my time-zone is UTC - 2 hours
offsetSecondsMyTZ = ZoneOffset.ofHours(-2).getTotalSeconds();
if (offsetSecondsMyTZ > 0) {
System.out.println("My timezone is ahead of UTC");
} else if (offsetSecondsMyTZ < 0) {
System.out.println("My timezone is behind UTC");
} else {
System.out.println("My timezone is UTC");
}
}
}
输出:
My timezone is ahead of UTC
My timezone is UTC
My timezone is behind UTC
注:本方案基于
Arvind Kumar Avinash 已经给出了很好的回答。我的代码仅在细节上有所不同。
ZoneOffset myZoneOffset
= OffsetDateTime.now(ZoneId.systemDefault()).getOffset();
int diff = myZoneOffset.compareTo(ZoneOffset.UTC);
if (diff < 0) {
System.out.println("" + myZoneOffset + " is ahead of UTC");
} else if (diff > 0) {
System.out.println("" + myZoneOffset + " is behind UTC");
} else {
System.out.println("" + myZoneOffset + " is UTC");
}
当我运行它刚刚在Europe/Copenhagen时区时,输出是:
+02:00 is ahead of UTC
您可能首先认为 运行ge compareTo()
认为 +02:00 小于 UTC 的偏移量。我想逻辑是他们希望它在偏移量的自然排序中 在 UTC 之前出现,这至少对我来说是有道理的。
请注意偏移随时间变化。特别是在许多地方,它因夏令时 (DST) 而随季节变化。因此,如果您在某个地方看到一年中某个时间的偏移量在 UTC 之前或之后,那么它很可能在一年中的另一个时间等于 UTC,反之亦然。
最后ZoneId.systemDefault()
给我们JVM的默认时区,它通常与操作系统时区相同,但并非总是如此。