aspose file tools*
The moose likes Java in General and the fly likes Does Java TimeZone takes care of daylight savings? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Does Java TimeZone takes care of daylight savings?" Watch "Does Java TimeZone takes care of daylight savings?" New topic
Author

Does Java TimeZone takes care of daylight savings?

Thanuja Vishwanath
Ranch Hand

Joined: Oct 07, 2010
Posts: 49


Hi,

In my application i need to show the date corresponding to the timezone of the user. I have written a function which will get Date from database and also users timezone. It then converts the given date to the user`s timezone.

Now iam stuck whether TimeZone takes care of daylight savings or do i need to do some additional stuff for day light savings?

Thanks,
Thanuja.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11477
    
  16

According to the API:
TimeZone represents a time zone offset, and also figures out daylight savings.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Thanuja Vishwanath
Ranch Hand

Joined: Oct 07, 2010
Posts: 49
Hi All,

Day light savings happened from 14-March. I tested my code and found that the day light savings change did not take place and it showed the wrong time.

Iam in India and using IST. i try to convert IST time to CST and it was working fine. I used TimeZone.set and getTimeZone. Now after the day light savings it should work and return the new date and time according to the day light savings. but iam not getting the expected output.

This is my code:



I have set the source time to 8:30 PM IST and the expected output is 10:00 AM CST (as there is a change due to day light savings).
Before day light savings the expected output was 9.00 AM CST(it worked perfect).


In API it is mentioned that TimeZone represents a time zone offset and also figures out daylight savings. So i thought it will take care of daylight savings automatically.

Now iam not getting that day light savings change in time. What should i do for this?

I googled and not able to find a proper solution. My app shows wrong time from today because of day light savings change. Please advise.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14345
    
  22

What version of Java are you using?

If you are using an older version, it might not have up-to-date information about when daylight savings changes occur. Whenever Oracle releases a new update version of the JDK, the daylight savings information is often updated. Make sure you're using the latest Java update.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Thanuja Vishwanath
Ranch Hand

Joined: Oct 07, 2010
Posts: 49
Jesper de Jong wrote:What version of Java are you using?

If you are using an older version, it might not have up-to-date information about when daylight savings changes occur. Whenever Oracle releases a new update version of the JDK, the daylight savings information is often updated. Make sure you're using the latest Java update.


I was using JDK 5.0 and now I have updated to JDK 1.6.0.24, I am using Netbeans IDE and I have changed the JDK version to 1.6.0.24 in netbeans.conf file. But still the problem remains.

Any clues?

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14345
    
  22

I tried compiling and running your code (using Java 1.6.0_24) and got an exception:

time:8:30 PM(14-Mar)
source:IST
destTz:CST
format:hh:mm a(dd-MMM)
java.text.ParseException: Unparseable date: "8:30 PM(14-Mar)"
at java.text.DateFormat.parse(DateFormat.java:337)
at TimeZoneConversion.convTimeZone(TimeZoneConversion.java:39)
at TimeZoneConversion.main(TimeZoneConversion.java:12)
Exception in thread "main" java.lang.NullPointerException
at java.util.Calendar.setTime(Calendar.java:1075)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:876)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:869)
at java.text.DateFormat.format(DateFormat.java:316)
at TimeZoneConversion.convTimeZone(TimeZoneConversion.java:51)
at TimeZoneConversion.main(TimeZoneConversion.java:12)

The error doesn't seem to have anything to do with time zones, but with a wrong date format. Do you get the same output? Make sure you fix this first.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14345
    
  22

Are you sure that CST is what you want to use? I searched for some info about what CST is exactly and found this:

Time zone offset: UTC - 6 hours
CST is 6 hours behind of Coordinated Universal Time (UTC)

Note that most places observe daylight saving time/summer time during summer, and are therefore using CDT instead in the summer.

It looks like the CST timzone itself doesn't have daylight savings time; the site mentions that specific states switch to CDT in the summer. Maybe you should use a location name like "Europe/Amsterdam" instead of the name of a specific timezone.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14345
    
  22

Rajesh Pattabi,
Your post was moved to a new topic.
Please don't put new, unrelated questions at the bottom of an old existing post.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Does Java TimeZone takes care of daylight savings?