0

I m using ZonedDateTime library from Java to retrieve UTC offset of any timezone out of the standard timezone list (Olson timezone database).

Here is my simple code.

import java.time.ZoneId;
import java.time.ZonedDateTime;

public class HelloWorld{

    public static void main(String []args){
        displayUtcOffset("Europe/Istanbul");
        displayUtcOffset("America/Caracas");
    }

    public static void displayUtcOffset(String olsonId){
        ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of(olsonId));
        float utcOffset = zonedDateTime.getOffset().getTotalSeconds()/3600f;
        System.out.println("For "+olsonId+", UTC"+utcOffset);
    }
}

And the output of these is,

For Europe/Istanbul, UTC2.0
For America/Caracas, UTC-4.5

As we see UTC offset for Caracas is correct but that for Istanbul is actually +3 but it gives output as +2 which is incorrect. Has there been some change to the way this java library works? or is there a more reliable library for converting olson Id to UTC offset?

Note: olson Ids List

Kedar Prabhu
  • 316
  • 3
  • 6

2 Answers2

1

Did you take the daylight saving time into account? For example, when you check the time in summer, like this:

    public static void displayUtcOffset(String olsonId){
        ZonedDateTime zonedDateTime = ZonedDateTime.of(2017, 7, 15, 1, 1, 11, 1, ZoneId.of(olsonId));
        float utcOffset = zonedDateTime.getOffset().getTotalSeconds()/3600f;
        System.out.println("For "+olsonId+", UTC"+utcOffset);
    }

the output is coherent with what you expected:

For Europe/Istanbul, UTC3.0
For America/Caracas, UTC-4.5
lukeg
  • 4,189
  • 3
  • 19
  • 40
  • That's the problem. Even when Istanbul does not have daylight savings, this library is still considering it. Is there a way to fix this? Or do I need to switch to a different library? – Kedar Prabhu Jan 11 '17 at 23:18
  • Oh, I didn't know Turkey scraped the DST (good for them!). Then the instructions have been given [here](http://stackoverflow.com/a/40401351/5899161) – lukeg Jan 11 '17 at 23:30
1

Turkey recently stopped observing DST and your timezone database is probably not up to date.

Looking at the revision history, the change was introduced in version tzdata2016g. At the time of writing, the latest version of the JDK is update 111/112 which use tzdata2016f and would therefore not have the correct time zone for Turkey.

You can either wait for the next update of the JDK which will probably contain the correction, or you can install the latest time zone database by using the Timezone Updater Tool.

assylias
  • 321,522
  • 82
  • 660
  • 783