I have a use case where I get timeZoneId in UTC +/- format as String. Based on that I need to set correct timeZone in Java. Does anyone know the correct way of doing it?
For example:
String timeZoneId = "UTC+01:00";
TimeZone.getTimeZone(timeZoneId); <- Ideally it should set timezone of CET.
But it does not. It set default timeZone GMT. Does anyone know how to achieve this?
tl;dr
You cannot reliably determine a time zone from an offset. Your question is illogical.
Details
No, that is not a time zone. That is merely an offset.
Continent/Regionsuch asAsia/TokyoorAfrica/Tunis.Not possible. Many time zones may share the same offset.
Take your example of +01:00. Many time zones may share that offset. These include Africa/Casablanca, Africa/Algiers, Africa/Ndjamena, Africa/Lagos, Europe/Brussels, Europe/Andorra, Europe/Malta, Europe/Oslo, Europe/Paris, and many more.
So an offset does not map to a time zone one-to-one.
Furthermore, the offset used by a time zone varies over time. This happens for various reasons. An invading army may dictate a certain time zone in its occupied territory. A politician may change the time zone of their jurisdiction to annoy or appease a neighboring country. Or some loopy politicians may decide to require their citizens go to bed an hour earlier or open their shops an hour later, in a practice known as Daylight Saving Time (DST).
The offset changing in a locality over time means that you cannot determine the offset in effect without specifying a date and time along with the name of a time zone.
No,
CETis not a time zone. Such 2-4 letter abbreviations are pseudo zones that hint at a time zone, and indicate whether Daylight Saving Time (DST) is in effect or not. But these pseudo-zones are not standardized, and are not even unique. Avoid them. Use real time zones with names ofContinent/Region.If you mean “How to achieve determining a time zone from a given offset?”, you cannot. That is not possible as discussed above.
UTC and GMT are the same thing when speaking practically, in common business contexts. The difference is less than a second.
All of this has been discussed many times on Stack Overflow. Search to learn more.