tl;dr
Do not do what you are attempting.
Does not make sense, as zones sharing an offset at one moment in time may not share that offset at other moments in the past or future.
Zone is a history of offsets
The Answer by Ole V.V. is correct. Here is a bit more discussion.
I need a single timezone, and I have timezone offset.
Not really possible.
An offset-from-UTC is nothing more than a number of hours, minutes, and seconds ahead of, or behind, UTC.
A time zone is much more. A zone is a history of past changes in offset used by the people of a specific region. A zone also has rules for present and future changes to the offset for that region. So a time zone is always preferable to an mere offset, but only when known with certainty. In your case, you do not know the zone, and should not guess; just stick with using the offset.
A given offset cannot lead you to a particular time zone without ambiguity and without error.
- Ambiguity, because many zones may share the same offset by coincidence. For example,
Europe/Gibralter
& Europe/Oslo
& Africa/Lagos
all share the same offset today, one hour ahead of UTC. So with only an offset-from-UTC of +01:00
how would you know which is appropriate?
- Error, because without a date you cannot know what offset applies to a time zone. For example, the three zones listed above share the same offset today in the winter of 2017-2018, but in the summer Gibralter and Oslo observe Daylight Saving Time (DST) meaning they shift an hour to be two hours ahead of UTC while Lagos remains one hour ahead of UTC. And historically, the offsets may have changed for other reasons than DST as politicians world-wide have a curious propensity for redefining zones. For example, twice in a decade Venezuela has shifted its offset by a half-hour. As another example in recent years, Russia and Turkey have been changing their minds about being on DST, reverting to their previous static offset, or changing to to stay permanently ahead of their previous static offset.
Note how the ZoneRules
class can give you an offset for a particular zone only if you provide a moment (a date-time value) passed as an argument.
ZoneOffset offset = ZoneId.of( "Africa/Tunis" ) // Instantiate a `ZoneId`.
.getRules() // Obtain a `ZoneRules` object from that `ZoneId` object.
.getOffset( Instant.now() ) ; // Interrogate that `ZoneRules` for an offset-from-UTC in use at a specific moment in history.
I just need ANY timezone id or a single timezone of given offset.
This in not advisable. As discussed above, the time zones sharing a particular offset on one date may not share that offset on another date. Each zone varies by its history of changes in offset. So picking a zone arbitrarily is not sensible.
java.time
As explained in the other Answer, the TimeZone
& ZoneInfo
classes are now legacy. They are part of the troublesome old date-time classes that are supplanted by the java.time classes.
Instead use ZoneOffset
(offset-from-UTC) and ZoneId
(time zone).
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" );
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.