0

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?

kandarp
  • 991
  • 1
  • 14
  • 35
  • 4
    You don't have a time zone ID, you have the UTC offset. A time zone ID is e.g. "Europe/Berlin". There is no way to set the correct time zone only with the offset. – akuzminykh Aug 22 '22 at 11:30
  • 1
    You will get about 30 different `ZoneId`s having an offset of `+01:00` (at the moment). How do you want to choose the correct one? Considering daylight saving time would alter the amount depending on the day of year you run a choosing code. – deHaar Aug 22 '22 at 11:47
  • 1
    That is not completely right. A time zone ID is not only "Europe/Berlin" but also e.g. "GMT+1" or "GMT+01:00". UTC however is not supported by TimeZone as an ID. So if you can get your timeZoneId in GMT format instead of UTC then you would be able to create a TimeZone instance from it. – StrongPoint Aug 22 '22 at 12:12
  • Yes, I know GMT is taken by TimeZone instance. Unfortunately I am getting it only in UTC. Can we some how convert UTC to GMT? @StrongPoint – kandarp Aug 22 '22 at 12:19
  • 2
    Don’t use `TimeZone` nor `Date`. Those classes are poorly designed and long outdated, not worth struggling with. Use java.time, the modern Java date and time API, and its `ZoneId` class for time zones. – Ole V.V. Aug 22 '22 at 12:31
  • 1
    CET is the common name for a great many European and African time zones using offset +01:00 in winter. Most of them, not all, use offset +02:00 in summer. Which of those zones do you want? Specify as for example Europe/Stockholm or Africa/Ceuta. – Ole V.V. Aug 22 '22 at 12:32
  • Related: [Java: getTimeZone without returning a default value](https://stackoverflow.com/questions/33373442/java-gettimezone-without-returning-a-default-value) – Ole V.V. Aug 22 '22 at 14:43
  • I believe that all that you need is the `ZoneId` you get from `ZoneId.of(timeZoneId)`. It’s exactly `UTC+01:00`, so not CET, but it sounds like that’s OK for your purpose? If you thought that you needed an old-fashioned `TimeZone` object for a `Calendar`, a `DateFormat` or another old class, — **don’t**. Stay away from those old and troublesome classes and instead use `ZonedDateTime` with your `ZoneId`. In case you can’t avoid calling some legacy method that requires a `TimeZone`, you may use `TimeZone.getTimeZone(ZoneId.of(timeZoneId).normalized())` to translate to a `TimeZone` of `GMT+01:00` – Ole V.V. Aug 22 '22 at 15:06

1 Answers1

1

tl;dr

You cannot reliably determine a time zone from an offset. Your question is illogical.

Details

timeZoneId = "UTC+01:00

No, that is not a time zone. That is merely an offset.

  • An offset is a number of hours-minutes-seconds ahead or behind the temporal prime meridian of UTC/GMT. Simply a number, nothing more.
  • A time zone is much more. A time zone is a named history of the past, present, and future changes to the offset used by the people of a particular region, as decided by their politicians. A time zone has a name in format of Continent/Region such as Asia/Tokyo or Africa/Tunis.

Based on that I need to set correct timeZone in Java.

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.

Ideally it should set timezone of CET

No, CET is 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 of Continent/Region.

Does anyone know how to achieve this?

If you mean “How to achieve determining a time zone from a given offset?”, you cannot. That is not possible as discussed above.

Can we some how convert UTC to GMT

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154