-1

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.

Nikhil Agrawal
  • 26,128
  • 21
  • 90
  • 126

2 Answers2

3

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 > 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");
        }
    }
}

Output:

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

Note: This solution is based on this answer.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
2

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 < 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");
}

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.

Nikhil Agrawal
  • 26,128
  • 21
  • 90
  • 126
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161