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?