How to properly manage daylight saving time in Java 7 without Joda Time?

Before marking it, repeat it carefully. Plus cannot use Joda time and Java 8 time.util * .

I am trying to get the current date of a country with DST enabled. Since with the time zone it gets [local-millis] = [UTC-millis] + [offset-millis] , so I added DST_OFFSET to it to get the time in the country with DST enabled, as is done on Linux machine GMT_TIME + LOCAL_TIME_ZONE_OFFSET + DST_OFFSET to get the current local time.

Code for printing the current time in Istanbul. DST is currently on for 1 hour.

 public class DstInstanbul { public static void main(String...args){ Calendar calendar = Calendar.getInstance(); TimeZone timeZone = TimeZone.getTimeZone("Europe/Istanbul"); calendar.setTimeZone(timeZone); calendar.add(Calendar.MILLISECOND, timeZone.getDSTSavings()); SimpleDateFormat simpleDateFormatLocal = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); simpleDateFormatLocal.setTimeZone(TimeZone.getTimeZone("Europe/Istanbul")); System.out.println("Current date and time : " + simpleDateFormatLocal.format(calendar.getTime())); System.out.println("Current date and time : " + simpleDateFormatLocal.format(calendar.getTime())); System.out.println("The TimeZone is : " + calendar.getTimeZone().getID()); } } 

Which gave me the correct result

 Current date and time : 2015-11-01 20:49:54 The Hour of the Day is : 20 The TimeZone is : Europe/Istanbul 

But since the above code is not so much in common, so I tried to add the following line, so if only daylight is on, add only dstSaving So I changed the following calendar.add(Calendar.MILLISECOND, timeZone.getDSTSavings()); on the

 if (timeZone.inDaylightTime(new Date())){ calendar.add(Calendar.MILLISECOND, timeZone.getDSTSavings()); } 

But the problem is that I am doing it so that I get the result without any DST. and print System.out.println(timeZone.inDaylightTime(new Date())); gives me a false and therefore result, but there is daylight, as you can see in this link Istanbul Clock

 Current date and time : 2015-11-01 19:54:49 The TimeZone is : Europe/Istanbul. 

The same logic for Brazil timezone gives me true for inDaylightTime, but now the result is displayed one hour ahead

An ideal link for all code ordered by method 1. https://ideone.com/4NR5Ym 2. https://ideone.com/xH7vhp 3. https://ideone.com/tQenb5

My question is what is the problem with timeZone.inDaylightTime(new Date()) with Istanbul timezone. Why is he showing a lie. Why for Brazil I do not get the current DST time, even when the inDaylightTime value inDaylightTime true. What is the right way to deal with this situation?

+5
source share
2 answers

It seems that in Turkey today there are a few questions about time zones and daily savings.

Probably because Turkey changed the daylight saving time from November 1 to November 8.

The time zone data in your JVM may not match the change. Oracle has an update for their JVM.

The time zone data update that you download from the above link is a jar executable. To upgrade the JVM on the unix host:

 sudo java -jar tzupdater.jar --update --location http://www.iana.org/time-zones/repository/tzdata-latest.tar.gz 

The tool does not seem to display anything for updates, therefore, to check the launch:

 java -jar tzupdater.jar --version 

Timezone data version with update in Turkey - tzdata2015g

+3
source

You should not do this:

 calendar.add(Calendar.MILLISECOND, timeZone.getDSTSavings()); 

And you should not do this:

 if (timeZone.inDaylightTime(new Date())){ calendar.add(Calendar.MILLISECOND, timeZone.getDSTSavings()); } 

You do not need to manually adjust the time. This is done for you automatically based on the format date. The add call simply moves the instant point during operation.

Delete these lines and update your JVM to accept the latest Turkish DST change (as Monz said in his answer) and you should be good to go.

+3
source

Source: https://habr.com/ru/post/1234967/


All Articles