• Post Reply Bookmark Topic Watch Topic
  • New Topic

Day Light Saving Offset when to add/substract  RSS feed

 
Rohit Shaarma
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a problem with day light saving time in java, i am able to get time zone offset (i.e, the amount of time by which the clock is forwarded or reversed) but i am not able to get the condition when the clock is forwarded/reversed.

Any help is greatly appreciated.


Thanks in advance
Rohit Sharma
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

So, you want to know for a specific timezone what the rules are for switching from summertime to wintertime and vice versa?

The standard java.util.TimeZone class does not provide that information. In fact, the rules can be quite complicated and differ from year to year - governments around the world are changing timezone rules all the time.

The Joda Time library might help. It has a DateTimeZone class, which as far as I can see also doesn't tell you the exact rules, but it does have methods to get when the next or previous transition was to or from summertime or wintertime.
 
Rohit Shaarma
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jesper, basically my problem is i have a scheduler which runs every day at 12:15PM and due to DST it is running an hour before i.e, at 11:15PM and since as you said TimeZone doesnt provide any rule it is just providing an usigned offset of 1 hour(3600000 msec) what would be the easiest way to handle this issue according to you.




Regards,
Rohit Sharma
 
Rohit Shaarma
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found a simple solution. I just checked whether either current time or scheduled time were in DST, based on that i added/substracted the DST offset. below is a sample code snippet






Regards,
Rohit Sharma
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of new Date(System.currentTimeMillis()) you can also use new Date(); they are the same thing.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rohit Shaarma wrote:Thank you Jesper, basically my problem is i have a scheduler which runs every day at 12:15PM and due to DST it is running an hour before i.e, at 11:15PM and since as you said TimeZone doesnt provide any rule it is just providing an usigned offset of 1 hour(3600000 msec) what would be the easiest way to handle this issue according to you.

Have your scheduler determine the start time based on java.util.Calendar, rather than java.util.Date. The problem isn't with TimeZone, but rather, I suspect, the fact that your scheduler isn't taking "clock time" into account. And I hate to say it, but your nifty piece of code looks suspiciously like a solution to the symptom, not the problem.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!