Search code examples
javadatetimejodatimedatetime-conversion

API for timezone conversion


Is there any API available for datetime conversion between different timezones similar to google map's timezone api's.

Explaination for datetime conversion between different timezones -

Given the base timezone, datetime in base timezone and target timezone, an api which returns datetime in target timezone. (Which also considers concepts like dst as well)

Edit 1 -

Based On the negative reviews received on this question, had to give some more details for the need of this question and the efforts already wasted with different approaches.

Also with this question I am Expecting to know about some online available api, which could be hit all the time rather than depending on a library like ZonedDateTime provided with java8.

Some issues with ZonedDateTime of java 8-

See below scala code -

import java.time.{LocalDateTime, ZoneId, ZoneOffset, ZonedDateTime}
import java.time.format.DateTimeFormatter

val istanbul: ZoneId = ZoneId.of("Europe/Istanbul");
val str: String = "2017-03-29 17:00:00";
val str1: String = "2017-03-24 17:00:00";
val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
val localtDateAndTime: LocalDateTime = LocalDateTime.parse(str, formatter);
val localtDateAndTime1: LocalDateTime = LocalDateTime.parse(str1, formatter);
val dateAndTimeInIstanbul: ZonedDateTime = ZonedDateTime.of(localtDateAndTime, istanbul );
val dateAndTimeInIstanbul1: ZonedDateTime = ZonedDateTime.of(localtDateAndTime1, istanbul );


val utcDate: ZonedDateTime = dateAndTimeInIstanbul.withZoneSameInstant(ZoneOffset.UTC);
val utcDate1: ZonedDateTime = dateAndTimeInIstanbul1.withZoneSameInstant(ZoneOffset.UTC);

System.out.println("Original date and time in a particular timezone : " + dateAndTimeInIstanbul);
System.out.println("Converted date and time in UTC : " + utcDate);

System.out.println("Origianl date and time in a particular timezone : " + dateAndTimeInIstanbul1);
System.out.println("Converted date and time in UTC : " + utcDate1);

output generated by the above code is -

```

Original date and time in a particular timezone : 2017-03-29T17:00+03:00[Europe/Istanbul]
Converted date and time in UTC : 2017-03-29T14:00Z


Origianl date and time in a particular timezone : 2017-03-24T17:00+02:00[Europe/Istanbul]
Converted date and time in UTC : 2017-03-24T15:00Z

```

Now the problem is that from 2017 to 2020 there won't be any dst considered in Istanbul, which seems like is not considered in this ZonedDateTime library.

So would like to know about some other alternate. Preferably a web based API.


Solution

  • Now the problem is that from 2017 to 2020 there won't be any dst considered in Istanbul, which seems like is not considered in this ZonedDateTime library.

    That is not a problem with the library. If timezone information is incorrect (and I say "if"), then it is due to an issue with the timezone rules that your JVM is using.

    Here is what the latest timezone rules from IANA say for for Istanbul:

    # Zone  NAME            GMTOFF  RULES   FORMAT  [UNTIL]
    Zone    Europe/Istanbul 1:55:52 -       LMT     1880
                            1:56:56 -       IMT     1910 Oct
                            2:00    Turkey  EE%sT   1978 Oct 15
                            3:00    Turkey  +03/+04 1985 Apr 20
                            2:00    Turkey  EE%sT   2007
                            2:00    EU      EE%sT   2011 Mar 27  1:00u
                            2:00    -       EET     2011 Mar 28  1:00u
                            2:00    EU      EE%sT   2014 Mar 30  1:00u
                            2:00    -       EET     2014 Mar 31  1:00u
                            2:00    EU      EE%sT   2015 Oct 25  1:00u
                            2:00    1:00    EEST    2015 Nov  8  1:00u
                            2:00    EU      EE%sT   2016 Sep  7
                            3:00    -       +03
    

    The last line is saying that from 3am on 2016 Sep 7 onwards, the time offset is UTC + 3 hours. The source is the 2017a release of the IANA timezone database.

    When I run zdump -V Europe/Istanbul on my Linux system, it agrees with this. (The timezone rule files are distributed via the package manager, assuming you keep your system patched.)

    Now Java is a bit different. The Java libraries don't use the system timezone rules. Instead, they rely on a file derived from the IANA data (aka the Olson data) that is part of the Java installation. If you are running an old version of Java, you may have an old version of the timezone data. But there are two solutions to that:

    1. Update to the latest release of your version of Java. (This won't for end-of-lifed versions of Java; i.e. Java 7 and earlier ... as of March 2017.)
    2. Download the latest timezone database from IANA, and use the Oracle Timezone Updater Tool to update your JVM / JRE installation.