I know:
- An offset is merely a number of hours-minutes-seconds ahead/behind the temporal meridian of UTC.
- A time zone is a named history of the past, present, and future changes to the offset used by the people of a specific region, as determined by their politicians.
So for any moment, a particular time zone is using one offset while in another moment that same time zone may be using a different offset.
And, of course, any number of time zones may share the same offset at any one point in time.
Is there a way to obtain a list of time zones using a particular offset at a particular moment?
tl;dr
Details
Yes, you can get such a list in Java 8+ using the java.time classes.
The
ZoneIdclass represents each time zone, with a name in the format ofContinent/Region. The rules in use by each time zone are contained in aZoneRulesobject.The
ZoneOffsetclass represents a mere offset.The
Instantclass represents a moment, a specific point on the time line, as seen with an offset of zero hours-minutes-seconds from UTC. To get the current moment, callInstant.now().We can interrogate the zone rules for the offset in use by a zone at any particular moment. This means getting a
ZoneIdby its name, asking for its rules, and the passing anInstantto thegetOffsetmethod of the rules object.See this code run at Ideone.com.
Beware: Politicians frequently change the rules of the time zone(s) within their jurisdiction. They sometimes make those changes with an alarming lack of forewarning. Be sure the tzdata within your JVM is current for the time zones of interest to you. You may need to update the tzdata yourself, as new versions arrive faster than updates to Java.