Home > database >  What is the difference between the full name and the abbreviation of a time zone
What is the difference between the full name and the abbreviation of a time zone

Time:11-10

I have multiple times in my database that look like this:

String summerTime = "2022-07-21T10:00:00Z";
String winterTime = "2022-11-21T10:00:00Z";

In another hand, I want to get the time part of these dates based on a custom timezone, the timezones that I have, look like this:

GMT-03:00
GMT-02:00
GMT-01:00
GMT 02:00
GMT 01:00
....

When I try to extract the time part using this code:

ZoneId tz = ZoneId.of("GMT 01:00");

ZonedDateTime date1 = ZonedDateTime.parse(summerTime).withZoneSameInstant(tz.toZoneId());
ZonedDateTime date2 = ZonedDateTime.parse(winterTime).withZoneSameInstant(tz.toZoneId());

I get:

11:00
11:00

But if you notice well, the time is the same even on summer or winter dates.

I expect to get:

12:00  // <----- correct
11:00  // <----- correct

I found a solution for my issue, which is using the full name of the time zone, instead of the abbreviation:

ZoneId tz = ZoneId.of("Europe/Paris");

This gives me the correct response:

12:00
11:00

but I can't use this because I should use only the abbreviation.

My question:

  • Why do the two formats give different results?
  • How can get the full name from the abbreviation so I can get the correct output?

CodePudding user response:

Why do the two formats gives different result?

Because your first form is a "fixed offset" time zone, effectively. "GMT 01:00" is always on UTC 1, for the whole of time. It doesn't actually map to any specific area of the world, it's just "always UTC 1".

How can get the full name from the abbreviation so I can get the correct output?

You can't. Even if you know the UTC offset at a particular instant in time, there may be multiple time zones that have that UTC offset at that instant, but have different UTC offsets from each other at different times.

For example, currently both Europe/London and Africa/Abidjan are on UTC 0... but in June 2022 (for example) Africa/Abidjan was still be UTC 0, and Europe/London was UTC 1.

I can't use this because I should use only the abbreviation.

That's a fundamental problem, and you should push back on the requirement. It's like saying "I really need the user's full name, but I only have their initials." It's an infeasible requirement.

  • Related