• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Last day for DST (Daylight Saving Time)

 
Greenhorn
Posts: 18
Eclipse IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.
I'm wondering which is the best way to determine if I'm on the last day for DST given a date and a TimeZone.

The solution to this problem I was thinking about is summarized in the following code:


I don't even know if this can work. What do you think?
BTW: I *MUST* use java.util.* classes /methods for date and time, I *cannot* use Java 8 java.time.* classes or Joda Time or anything else.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Renato Perini wrote:I don't even know if this can work. What do you think?


I think the general idea is right, but as you've written it, it will NEVER return true because GMT doesn't use DST.

If it was me, I think I'd also supply the timezone to the method; and return false immediately if it doesn't allow DST.

Also: you can replace lines 12-17 with:
  return tz.inDaylightTime(current) && !tz.inDaylightTime(dayAfter);

BTW: I *MUST* use java.util.* functions for date and time, I *cannot* use Java 8 java.time.* classes.


Well I hope that's because they're going to teach you about the v8 stuff later on, because Java's "util" methods for dates are crap.

Winston
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You also need to consider that DST change does not occur at midnight.



Take a look at this example. Time instances differ by one minute within the same date day but the result is:
true
false


I needed to alter your method a little as you can't write TimeZone tz = new TimeZone("GMT") because TimeZone is an abstract class.
Also added static modifier.
 
Saloon Keeper
Posts: 27852
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DST/Summer TIme does not begin or end on the same date everywhere, and in some locales (for example, Arizona) it never kicks in at all. Indiana used to vary by county, although they amended that not too long ago.

For your function to work PROPERLY, you need to specify precisely where it is that you want to know the transition for.

Unfortunately, you'd also need to wander up and down the calendar to probe for it, as the transition dates can cover something like 2 months or so, and may be based on day-of-week, not absolute date - in the USA, the transition occurs at 2AM on a Sunday. Which, incidentally, means that it's not safe to calculate using midnight without further narrowing things down. And don't forget that there are a few locales where the time zone is on a 15-minute boundary. I think India does that.

An alternative - for Linux at least - is to simply query the tzdata database using the zdump program. Although if you do, it will simply spit out 4 cryptic numbers, which I suspect indicate begin/end dates for a 2-year span, but I didn't research it futher.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you are going to do historical dates, remember that the rules have changed over the years. I don't recall exactly when, but I think maybe > 8 years ago, it significantly moved, at least in (most of) the U.S.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:You also need to consider that DST change does not occur at midnight.


I doubt that will be a problem, since Renato is supplying a Date, which includes time; although I'd have to plough through Calendar's horrific API to be sure.

Fred Rosenberger wrote:if you are going to do historical dates, remember that the rules have changed over the years. I don't recall exactly when, but I think maybe > 8 years ago, it significantly moved, at least in (most of) the U.S.


Again, not so sure that is a huge problem - if TimeZone actually holds all the historic rules (and if it's based on .tz files, it should).

Winston
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:I doubt that will be a problem, since Renato is supplying a Date, which includes time


This is a problem that a Date includes time. Renato wants to know if the surrent day has DST change so an answer should not depend on the exact time during a day. See my example.
 
Tim Holloway
Saloon Keeper
Posts: 27852
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Time is inseparably linked to Date when locale is considered. When it's 10pm Monday where I am, it's 2am Tuesday in London, give or take a Daylight savings variance.

Even more importantly, at 1am on Sunday March 8. 2015, it will be EST, but 3am Sunday March 8 will be EDT. And Sunday November 1 will have 2 2:30 a.m.'s, one of them EDT and one of them EST.

Also, java.util.Date itself has a granularity not of days, but of milliseconds. Fred's also right about the rules changing. They change a LOT. The Linux tzdata package gets updated more than almost anything else in the OS, including the kernel itself.

We won't even consider how leap seconds figure in (we can't - they cannot be predicted and Linus Torvalds doesn't take them into account).

 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:This is a problem that a Date includes time. Renato wants to know if the surrent day has DST change so an answer should not depend on the exact time during a day.


Ah, see what you mean; but the rules about what is which "day" of change haven't been made clear. The fact is that if TimeZone does it's job right, 1:00AM on a Sunday of a change to DST should return false; so, should 1:00AM on the Saturday return true or false?

You've assumed that the date is what's important (and I suspect you're right); I've assumed that the problem is: is there a change within the next 24 hours?

Both are perfectly valid interpretations - we now need clarification from Renato.

Winston

Edit: And if the date is what's critical, I think I'd just change the supplied hour to 12 (assuming a 24-hour clock) once it's in the Calendar. Then all the other code remains the same.
 
Renato Perini
Greenhorn
Posts: 18
Eclipse IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:
I think the general idea is right, but as you've written it, it will NEVER return true because GMT doesn't use DST.



Eeek! Why?


If it was me, I think I'd also supply the timezone to the method; and return false immediately if it doesn't allow DST.



Right, I think it's a better choice. I was supplying a fixed timezone only for simplicity.

Paweł Baczyński wrote:You also need to consider that DST change does not occur at midnight.

...

Take a look at this example. Time instances differ by one minute within the same date day but the result is:
true
false


I needed to alter your method a little as you can't write TimeZone tz = new TimeZone("GMT") because TimeZone is an abstract class.
Also added static modifier.



I'm not completely sure why. I have done some tests based on your code, but I obtain:
false
false


The code I used:



Dirty code in a rush. But I don't understand why I obtain results different than yours. You were using Locale.GERMANY while I'm using "Europe/Berlin" for obtaining the TimeZone for the Calendar instance.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Renato Perini wrote:Eeek! Why?


Dunno, probably because GMT pre-dates DST.

I'm not completely sure why.


Because, as has already been explained, DST usually does NOT kick in at midnight. In the UK it's at 2AM and, as we've heard, in America it may depend on where you are.

Dirty code in a rush. But I don't understand why I obtain results different than yours. You were using Locale.GERMANY while I'm using "Europe/Berlin" for obtaining the TimeZone for the Calendar instance.


Hmmm. I'd have thought they were the same, but TBH, I don't know.

Suffice to say, there's an awful lot to know about timezones, so writing universally correct code is tough.

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Even more importantly, at 1am on Sunday March 8. 2015, it will be EST, but 3am Sunday March 8 will be EDT. And Sunday November 1 will have 2 2:30 a.m.'s, one of them EDT and one of them EST.


Good point, but again, I suspect this isn't a major problem, since Timezone.inDaylightTime() takes a Date, not a Calendar, and I assume that Calendar.add(DATE) is smart enough to know when a target date/time might be ambiguous - though what it does I have no idea. If I was the designer, I'd opt for the one that equals yesterday + 24 hours (ie, EDT).

Winston
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Renato Perini wrote:Dirty code in a rush. But I don't understand why I obtain results different than yours. You were using Locale.GERMANY while I'm using "Europe/Berlin" for obtaining the TimeZone for the Calendar instance.


My code:Your code:Can you see the difference?
 
Renato Perini
Greenhorn
Posts: 18
Eclipse IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:

Renato Perini wrote:Dirty code in a rush. But I don't understand why I obtain results different than yours. You were using Locale.GERMANY while I'm using "Europe/Berlin" for obtaining the TimeZone for the Calendar instance.


My code:Your code:Can you see the difference?



Oopss!! :P

Paweł Baczyński wrote:

Winston Gutkowski wrote:I doubt that will be a problem, since Renato is supplying a Date, which includes time


This is a problem that a Date includes time. Renato wants to know if the surrent day has DST change so an answer should not depend on the exact time during a day. See my example.



Verified your example and works exactly as expected: first date is "true", second date is "false".
We're speaking about GERMAN (Europe/Berlin) timezone in this example.
So, what do you suggest to fix this problem? Actually I can't figure out what a possible solution would be.


 
Marshal
Posts: 28258
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem? What problem?

The last day for DST is the day when you set the clocks back, isn't it? That day would only be DST up to (usually) 2 AM.

Or is the last day one which is DST all day?

You're going to need to state your requirements clearly. Once you know which of the two you're looking for, then set the time part of your Calendar object appropriately (early in the day for the first version, late in the day for the second version).
 
reply
    Bookmark Topic Watch Topic
  • New Topic