找出系统时区在Java中是UTC的前面还是后面。

huangapple go评论72阅读模式
英文:

Find if system timezone is ahead or behind UTC in java

问题

我需要找出我的当前系统时区是UTC时区的前面还是后面,我已经尝试了不同的方法,但问题是我想从系统中获取当前时区并将其与UTC进行比较。

虽然有许多与此相关的问题,但是关于前后概念的问题我没有找到。

我并不是在问如何找到区域偏移量(zoneOffset)。我只想检查应用程序是在UTC时区的前面还是后面。

英文:

I need to find out if my current system timezone is ahead or behind UTC timezone, I have tried different ways but the problem is I want to fetch the current time zone from the system and compare it with UTC.

There are many questions related to this but for ahead and behind concept I didn't find any.

I am not asking how to find zoneOffset. I just wanted to check if the application is ahead or behind UTC timezone.

答案1

得分: 3

你可以通过偏移的秒数来比较时区,例如:

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("我的时区领先于UTC");
        } else if (offsetSecondsMyTZ < 0) {
            System.out.println("我的时区落后于UTC");
        } else {
            System.out.println("我的时区是UTC");
        }

        // 假设我的时区是UTC
        offsetSecondsMyTZ = ZoneOffset.UTC.getTotalSeconds();
        if (offsetSecondsMyTZ > 0) {
            System.out.println("我的时区领先于UTC");
        } else if (offsetSecondsMyTZ < 0) {
            System.out.println("我的时区落后于UTC");
        } else {
            System.out.println("我的时区是UTC");
        }

        // 假设我的时区是UTC - 2小时
        offsetSecondsMyTZ = ZoneOffset.ofHours(-2).getTotalSeconds();
        if (offsetSecondsMyTZ > 0) {
            System.out.println("我的时区领先于UTC");
        } else if (offsetSecondsMyTZ < 0) {
            System.out.println("我的时区落后于UTC");
        } else {
            System.out.println("我的时区是UTC");
        }
    }
}

输出:

我的时区领先于UTC
我的时区是UTC
我的时区落后于UTC

注意: 这个解决方案基于这个答案

英文:

You can compare the no. of seconds by which a zone is offset e.g.

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 &gt; 0) {
System.out.println(&quot;My timezone is ahead of UTC&quot;);
} else if (offsetSecondsMyTZ &lt; 0) {
System.out.println(&quot;My timezone is behind UTC&quot;);
} else {
System.out.println(&quot;My timezone is UTC&quot;);
}
// Assuming my time-zone is UTC
offsetSecondsMyTZ = ZoneOffset.UTC.getTotalSeconds();
if (offsetSecondsMyTZ &gt; 0) {
System.out.println(&quot;My timezone is ahead of UTC&quot;);
} else if (offsetSecondsMyTZ &lt; 0) {
System.out.println(&quot;My timezone is behind UTC&quot;);
} else {
System.out.println(&quot;My timezone is UTC&quot;);
}
// Assuming my time-zone is UTC - 2 hours
offsetSecondsMyTZ = ZoneOffset.ofHours(-2).getTotalSeconds();
if (offsetSecondsMyTZ &gt; 0) {
System.out.println(&quot;My timezone is ahead of UTC&quot;);
} else if (offsetSecondsMyTZ &lt; 0) {
System.out.println(&quot;My timezone is behind UTC&quot;);
} else {
System.out.println(&quot;My timezone is UTC&quot;);
}
}
}

Output:

My timezone is ahead of UTC
My timezone is UTC
My timezone is behind UTC

Note: This solution is based on this answer.

答案2

得分: 2

以下是翻译好的内容:

有一条来自 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");
}

当我在欧洲/哥本哈根时区运行它时,输出为:

+02:00 is ahead of UTC

起初你可能会觉得奇怪,compareTo() 认为 +02:00 的偏移量 小于 UTC。我猜想这么设计的逻辑是,他们希望在偏移量的自然顺序中,它位于 UTC 之前,至少对我来说是有道理的。

请注意,偏移量随时间而变化。特别是在许多地方,由于夏令时(DST),偏移量会随季节变化。因此,如果在某个地方您发现偏移量在一年的某个时间要么在 UTC 之前要么在 UTC 之后,那么在一年的另一个时间,它很可能与 UTC 相等,反之亦然。

最后,ZoneId.systemDefault() 给我们提供了 JVM 的默认时区,它通常与操作系统的时区相同,但并不总是相同。

英文:

There’s a good answer by Arvind Kumar Avinash already. My code only differs in the details.

ZoneOffset myZoneOffset
= OffsetDateTime.now(ZoneId.systemDefault()).getOffset();
int diff = myZoneOffset.compareTo(ZoneOffset.UTC);
if (diff &lt; 0) {
System.out.println(&quot;&quot; + myZoneOffset + &quot; is ahead of UTC&quot;);
} else if (diff &gt; 0) {
System.out.println(&quot;&quot; + myZoneOffset + &quot; is behind UTC&quot;);
} else {
System.out.println(&quot;&quot; + myZoneOffset + &quot; is UTC&quot;);
}

When I ran it just now in Europe/Copenhagen time zone, the output was:

> +02:00 is ahead of UTC

You may consider it strange at first that compareTo() considers an offset of +02:00 less than UTC. I suppose the logic is that they wanted it to come before UTC in the natural ordering of offsets, which at least to me makes sense.

Be aware that offset varies with time. In particular in many places it varies with the seasons because of summer time (DST). So if in one place you see that the offset is either before or after UTC at one time of year, it could easily be equal to UTC at another time of year, or vice versa.

Finally ZoneId.systemDefault() gives us the JVM’s default time zone, it often the same as the operating system time zone, but not always.

huangapple
  • 本文由 发表于 2020年9月9日 23:07:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63814519.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定