-1

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.

Sumit Paliwal
  • 385
  • 1
  • 3
  • 14
  • If you're using java >= 8, use the new date time API: http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html If you're using java <= 7, consider these ones: - http://www.joda.org/joda-time/ - http://www.threeten.org/threetenbp/ (this one is a backport to java 8 date time api, so in new projects this one is recommended, as I believe joda will no longer be used) –  Mar 10 '17 at 13:06

1 Answers1

3

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.
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Hi, tried both the solutions suggested, but still no luck in this scenario... also zdump is showing me the similar response as you mentioned. But even after updating with IANA timezone database... the conversion is wrong.. – Sumit Paliwal Mar 10 '17 at 18:35
  • See also http://stackoverflow.com/questions/40400793/java-timezone-in-turkey-rejected-daylight-saving – Stephen C Mar 11 '17 at 00:29
  • If you update the Java timezone info **correctly** and it still doesn't work for you, then you are probably out of luck. Alternative Java APIs will use the same approach (or else they will have worse problems!) And relying on a network-accessible service for TZ lookups is a bad idea for other reasons. – Stephen C Mar 11 '17 at 00:33
  • Could not make ZonedDateTime library work... and as per your suggestions went for alternative Java APIs, opted for joda-time library... and worked well for me.. Thanks – Sumit Paliwal Mar 11 '17 at 18:32
  • @Sumit can you please accept the answer since it solved your problem – David Rawson Mar 16 '17 at 07:05