user4039871
user4039871

Reputation:

Wrong hour of day in Java calendar

TimeZone zone1 = TimeZone.getTimeZone("America/New_York");
TimeZone zone2 = TimeZone.getTimeZone("Europe/Istanbul");
int hour1 = Calendar.getInstance(zone1).get(Calendar.HOUR_OF_DAY);
int hour2 = Calendar.getInstance(zone2).get(Calendar.HOUR_OF_DAY);

// hour1 gives 1
// hour2 gives 7

Right now it is 1 at New York, but 8 at Istanbul. Why does Java give 1 less result for Europe/Istanbul? Can it be because of daylight savings? If so, doesn't it take care of it automatically?

I am not sure if the problem is because of daylight savings thing. The following shows, I guess, both timezones have DST offset:

zone1.getDSTSavings()/1000L   // gives 3600
zone2.getDSTSavings()/1000L   // gives 3600

zone1.useDaylightTime()   // true
zone2.useDaylightTime()   // true

zone1.inDaylightTime(new Date())   // false
zone2.inDaylightTime(new Date())   // false

SOLVED:

Time zone data was not up to date. Download the tzupdater utility from Java website, and issue:

java -jar tzupdater.jar -l 'http://www.iana.org/time-zones/repository/tzdata-latest.tar.gz'

This auto downloads the latest tz data and updates the JRE. This is a hell. Losing 2 days for a stupid timezone problem. This should be automated and the JRE should self update every day. Why not???

Insert new crontab entry: (Yet another one)

crontab -e
00 05 * * * java -jar tzupdater.jar -l https://www.iana.org/time-zones/repository/tzdata-latest.tar.gz

Upvotes: 0

Views: 1061

Answers (2)

Hasan YILDIZ
Hasan YILDIZ

Reputation: 19

This code is correct. My result New York 1 - Istanbul 9. This problem is your computer or OS. I live in Istanbul and hour of day 9. Summer time application is not over. Because the Turkish elections were postponed for 2 weeks.

    TimeZone zone1 = TimeZone.getTimeZone("America/New_York");
    TimeZone zone2 = TimeZone.getTimeZone("Europe/Istanbul");
    int hour1 = Calendar.getInstance(zone1).get(Calendar.HOUR_OF_DAY);
    int hour2 = Calendar.getInstance(zone2).get(Calendar.HOUR_OF_DAY);

    System.out.println("New York" + hour1 + " - Istanbul "+ hour2);

Upvotes: 2

Ankur Anand
Ankur Anand

Reputation: 3904

There seems to be problems with Turkey time as turkey has changed there DST from 1 Nov to 8 Nov

because for the un-updated JVM if i do

TimeZone timeZone = TimeZone.getTimeZone("Europe/Istanbul");
System.out.println(timeZone.inDaylightTime(new Date()))

It gives me False but as for Istanbul the DST is till NOV 8.

So you need to update the JVM tzdata version from here. Here is what Latest tzdata Data version also says.

tzdata2015g n/a Turkey's 2015 fall-back transition is scheduled for Nov. 8, not Oct. 25. Norfolk moves from +1130 to +1100 on 2015-10-04 at 02:00 local time. Fiji's 2016 fall-back transition is scheduled for January 17, not 24. Fort Nelson, British Columbia will not fall back on 2015-11-01. It has effectively been on MST (-0700) since it advanced its clocks on 2015-03-08. New zone America/Fort_Nelson.

So Refer to this tzupdater documents to get update for your JVM

Upvotes: 7

Related Questions