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.