Before marking it duplicate read it thoroughly Plus Can't use Joda Time and Java 8 time.util*.
I'm Trying to get the current date of Country with DST Enabled. Since with time zone it get [local-millis] = [UTC-millis] + [offset-millis]
so i added the DST_OFFSET
to it to get the time in the DST enabled country, as done on the Linux machine GMT_TIME + LOCAL_TIME_ZONE_OFFSET + DST_OFFSET
to get the current local time.
The code to print the current Time of Istanbul Which has currently DST Enabled by 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 output
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 of generic so i tried to add following line, so that if only the daylight is enabled then only add
the dstSaving So i changed the following calendar.add(Calendar.MILLISECOND, timeZone.getDSTSavings());
with
if (timeZone.inDaylightTime(new Date())){
calendar.add(Calendar.MILLISECOND, timeZone.getDSTSavings());
}
But the Problem is if i do so i get the output without any DST. and printing System.out.println(timeZone.inDaylightTime(new Date()));
gives me false and hence the result but the daylight saving is there 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 the time zone Brazil gives me true for inDaylightTime but displays a result one hour ahead now
Ideone link for all the code ordered in a way discussed 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 Time Zone. Why it's showing false. Why for Brazil i'm not getting current DST time even when the inDaylightTime
is true.
What is the proper way to handle such situation ?
The right way to handle DST in Java is to instantiate a Timezone with a specific TZDB Timezone ID, eg. “Europe/Rome”. Then, we'll use this in conjunction with time-specific classes like java. util.
Localized times and date arithmetic This library also allows you to do date arithmetic using local times, although it is more complicated than working in UTC as you need to use the normalize() method to handle daylight saving time and other timezone transitions.
If you cannot change the OS or the JVM timezone, you can still convert a Java Date/Time or Timestamp to a specific time zone using the following two JDBC methods: PreparedStatement#setTimestamp(int parameterIndex, Timestamp x, Calendar cal) – to convert the timestamp that goes to the database.
You should not do this:
calendar.add(Calendar.MILLISECOND, timeZone.getDSTSavings());
Nor should you do this:
if (timeZone.inDaylightTime(new Date())){
calendar.add(Calendar.MILLISECOND, timeZone.getDSTSavings());
}
You do not need to adjust the time manually. It is done for you automatically based on the date you are formatting. Calling add
just moves the instantaneous point in time you are working with.
Remove those lines, and update your JVM to take in the latest Turkish DST change (as Monz described in his answer), and you should be good to go.
There seems to be a few questions about time zones and daylight savings in Turkey today.
This is likely because Turkey has changed the date for the daylight savings switch from November 1 to November 8.
The timezone data in your JVM may not be current with the change. Oracle has an update for their JVM.
The timezone data updater that you download from the link above is an executable jar file. To update your JVM on a unix host:
sudo java -jar tzupdater.jar --update --location http://www.iana.org/time-zones/repository/tzdata-latest.tar.gz
The tool doesn't seem to output anything on updates, so to verify run:
java -jar tzupdater.jar --version
The version of the timezone data with the Turkey update is tzdata2015g
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With