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:
TimeZone tz = TimeZone.getTimeZone("GMT+01:00"); ZonedDateTime date1 = ZonedDateTime.parse(summerTime).withZoneSameInstant(tz.toZoneId()); ZonedDateTime date2 = ZonedDateTime.parse(winterTime).withZoneSameInstant(tz.toZoneId());
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:
TimeZone shortName = TimeZone.getTimeZone("Europe/Paris");
This gives me the correct response:
but I can’t use this because I should use only the abbreviation.
- Why do the two formats give different results?
- How can get the full name from the abbreviation so I can get the correct output?
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.