• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Difference between two Dates during DST

 
Ranch Hand
Posts: 166
1
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am practising enthuware questions and i got confused in one of the question and is as follows:



The options are :
1
-1
0
2
-2
This will not compile.

When i have chosen it as 0 , but i see that that answer from enthuware is -2.

Explanation by enthuware software guys is as follows:


Think of it as follows -
The time difference between two dates is simply the amount of time you need to go from date 1 to date 2.
So if you want to go from 1AM to 2AM, how many hours do you need? On a regular day, you need 1 hour. That is, if you add 1 hour to 1AM, you will get 2AM. However, as given in the problem statement, at the time of DST change, 2 AM becomes 1AM. That means, even after adding 1 hour to 1AM, you are not at 2AM. You have to add another hour to get to 2 AM. In total, therefore, you have to add 2 hours to 1AM to get to 2AM.

The answer can therefore be short listed to 2 or -2. Now, as per the JavaDoc description of the between method, it returns negative value if the end is before the start. In the given code, our end date is 1AM, while the start date is 2AM. This means, the answer is -2.



I am not clear about the explanation of Enthuware guys

Thanks

Swapna
 
Bartender
Posts: 15737
368
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What part don't you understand?
 
Sheriff
Posts: 28372
99
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
About that explanation, there are two things I don't understand. One is what they mean by "1AM" and the other is what they mean by "2AM".

On that day when daylight saving causes the clock to go back by one hour, the meaning of those two terms is ambiguous until you are told how the software handles them. No doubt the documentation for ZonedDateTime explains that, but the Enthuware explanation doesn't. Saying that "2AM becomes 1AM" is only true once on that day; after the time changes it's 2AM again an hour later, but that 2AM doesn't become 1AM.

I don't know what the answer to that question is. I assume the answer given by Enthuware is correct, and the easiest way to find out would be to run the code and see what happens. Another easy way would be to read the documentation. I haven't done either of those things because I don't think that it's necessary for a Java programmer to memorize how ZonedDateTime.of() works when given a time which coincides with a daylight saving switch. If I were to rank the things a Java programmer needs to know, that piece of trivia would be so far down the list you'd need a telescope to see it.
 
Paul Clapham
Sheriff
Posts: 28372
99
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
Swapna: sorry, my post maybe doesn't help you much. But as I mentioned, the documentation for ZonedDateTime.of() ought to be specific about how it handles times which are during daylight saving changes. A similar example to the one you asked about would involve 1:30 AM, which occurs twice on that day as well and the ambiguity can't be defined away. So the documentation will say how that's dealt with. I recommend reading it.
 
Swapna latha
Ranch Hand
Posts: 166
1
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:What part don't you understand?


Stephan, I am not sure how the answer is -2. I still think answer is 0. The explanation given by enthuware is not clear to me.


Thanks & Regards,

Swapna.
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to tell us what part of the explanation is not clear to you, and explain to us why you think it is 0.
 
Marshal
Posts: 80295
434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And tell us what you read in the ZonedDateTime documentation.
 
Swapna latha
Ranch Hand
Posts: 166
1
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You need to tell us what part of the explanation is not clear to you, and explain to us why you think it is 0.



Stephan, the lines of codes are as follows with my explanation . Correct me if i am wrong

LocalDateTime ld1 = LocalDateTime.of(2015, Month.NOVEMBER, 1, 2, 0); // This line of code is having a time of 2 AM so as per DST savings, it should become 1 AM
ZonedDateTime zd1 = ZonedDateTime.of(ld1, ZoneId.of("US/Eastern"));
LocalDateTime ld2 = LocalDateTime.of(2015, Month.NOVEMBER, 1, 1, 0);// There wont be any change of time as the time is still 1 AM and time changes after 2 AM
ZonedDateTime zd2 = ZonedDateTime.of(ld2, ZoneId.of("US/Eastern"));// As both the times are 1 AM , the result is zero.

Thanks & Regards,

Swapna.
 
Campbell Ritchie
Marshal
Posts: 80295
434
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swapna latha wrote:. . .  This line of code is having a time of 2 AM so as per DST savings, it should become 1 AM

No, the times go from 1:59:59 to 1:00:00. 2:00:00am is 1hour and 1sec after 1:59:59.

. . . As both the times are 1 AM , the result is zero. . . .

No, 1:00:00 is 2hours before 2:00:00 so the difference is −2hours.
 
Saloon Keeper
Posts: 28494
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this with 1:59 instead of 2:00. You might find it interesting. Indeed, try 1:59:59
 
Swapna latha
Ranch Hand
Posts: 166
1
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Tim. I got the answer now.

Tim Holloway wrote:Try this with 1:59 instead of 2:00. You might find it interesting. Indeed, try 1:59:59

 
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic