Page 1 of 1

About Question enthuware.ocpjp.v8.2.1749 :

Posted: Sat Jan 30, 2016 11:29 am
by lucasheitich
I didn't get it...

"Given that daylight Savings Time ends on Nov 1 at 2 AM in US/Eastern time zone. (As a result, 2 AM becomes 1 AM.), what will the following code print?"

If you have Nov/01 01:00AM it means you have 01:00.
If you have Nov/01 02:00AM it means you have 01:00.

Since the daylight savings ends exactly in nov 1 02:00AM, then the Nov/01 01:00AM should be still 01:00AM and Nov/01 02:00AM as well.

I compiled and ran the code and it printed -2, but I still didn't get it.

How 1 - 1 == -2?

Re: About Question enthuware.ocpjp.v8.2.1749 :

Posted: Sat Jan 30, 2016 9:31 pm
by admin
1-1 is not -2. On Nov 1, at 2AM, the display changes back to 1AM. So that means, even after adding the first one hour to 1 AM, you still have the time showing as 1 AM. So to go to 2AM, you need to add one more hour. Therefore, the total number of hours that you added is 2. Therefore, 1AM - 2AM is -2.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v8.2.1749 :

Posted: Mon May 23, 2016 8:35 am
by RAZER-KIEV
In fact we have 2 AM twice. What about the case when we want to get 2 AM witch comes after clock changes back?
When we call LocalDateTime.of(2015, Month.NOVEMBER, 1, 2, 0); how the System knows witch of 2 time points we means ?

Re: About Question enthuware.ocpjp.v8.2.1749 :

Posted: Mon May 23, 2016 9:05 am
by admin
System doesn't keep time in timezones or even in hours minutes seconds. It keeps the time in the form of total seconds elapsed after the epoch (e.g. start of the Unix epoch is at 1 January 1970 00:00:00 UT). So it doesn't matter to the system whether you call a particular time as 1st Nov 1AM or 1st Nov 2AM. As far as the system is concerned "the total seconds elapsed since the epoch" for "2AM before the shift" and "the total seconds elapsed since the epoch" for "2AM after the shift" are two different numbers.

Re: About Question enthuware.ocpjp.v8.2.1749 :

Posted: Sat May 28, 2016 2:32 am
by ramlax
I understand lucasheitich's concerns about this question and I do get the explanation to this question as well.
Until... I stopped at a very similar question just with the DST Start Date.

So the question is:
"Given that daylight Savings Time starts on March 8th at 2 AM in US/Eastern time zone. (As a result, 2 AM becomes 3 AM.)"

Here we have March/08 02:00AM it means you have 03:00.
Here we have March/08 03:00AM it means you have 03:00.

Can someone explain to me why is the result of ChronoUnit.HOURS.between function here 0, considering the analogy in the
lucasheitich's initial post?

Apparently the time in DST start (March) is moved ahead to 3 AM in contrast with DST end time (November) which is not moved back to 1 AM, or I misunderstood something?

Code: Select all

LocalDateTime ld1 = LocalDateTime.of(2015, Month.NOVEMBER, 1, 2, 0);    // 2015-11-01T02:00
ZonedDateTime zd1 = ZonedDateTime.of(ld1, ZoneId.of("US/Eastern"));     // 2015-11-01T02:00-05:00[US/Eastern
LocalDateTime lod1 = LocalDateTime.of(2015, Month.MARCH, 8, 2, 0);       // 2015-03-08T02:00
ZonedDateTime zod1 = ZonedDateTime.of(lod1, ZoneId.of("US/Eastern")); // 2015-03-08T03:00-04:00[US/Eastern] 
Thank you for your help.

Re: About Question enthuware.ocpjp.v8.2.1749 :

Posted: Sun May 29, 2016 12:15 am
by admin

Re: About Question enthuware.ocpjp.v8.2.1749 :

Posted: Mon Jun 20, 2016 7:58 pm
by schchen2000
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.
What do you mean when you said "it returns negative value if the end is before the start"?

Is that because end aka zd2 (01:00 AM) is less than start aka zd1 (02:00 AM)? Those of us in N. America also know that end is still in summer time zone and start is in winter time zone.

How do you compare 2 things in different time zones and say that one comes before the other?

Re: About Question enthuware.ocpjp.v8.2.1749 :

Posted: Mon Jun 20, 2016 8:28 pm
by admin
1. Yes, 1AM occurs before 2AM.
2. It is talking about the same time zone. Where do you see different time zones?

Re: About Question enthuware.ocpjp.v8.2.1749 :

Posted: Mon Jun 20, 2016 10:17 pm
by schchen2000
admin wrote:1. Yes, 1AM occurs before 2AM.
2. It is talking about the same time zone. Where do you see different time zones?
I ran your code with some print statements and I've got the following:

zd1 = 1 2015-11-01T02:00-05:00[US/Eastern] <==== Entering winter time in N. America. Offset of -5 here.
zd2 = 2 2015-11-01T01:00-04:00[US/Eastern] <==== Still in summer time in N. America. Offset of -4 here.

In that case, are you not comparing 2 different times in 2 different time zones?

Therefore, how do you draw the conclusion that one comes before another?

Would you reason that summer (i.e. end aka zd2) comes before winter (i.e. start aka zd1), given the simulation results?

Re: About Question enthuware.ocpjp.v8.2.1749 :

Posted: Mon Jun 20, 2016 11:14 pm
by admin
Time zone is same i.e. US/Eastern. Daylight start or end doesn't change the time zone. Only the zone offset wrt to GMT depends on whether dst is on or off.

Re: About Question enthuware.ocpjp.v8.2.1749 :

Posted: Tue Jun 21, 2016 11:24 am
by schchen2000
admin wrote:Time zone is same i.e. US/Eastern. Daylight start or end doesn't change the time zone. Only the zone offset wrt to GMT depends on whether dst is on or off.
I see. Thank you.

Re: About Question enthuware.ocpjp.v8.2.1749 :

Posted: Mon Oct 03, 2016 12:01 am
by vlive07@gmail.com
Program does not compiles . As per i know for comparision zoneddatetime must be converted into instant to remove timezone...

Re: About Question enthuware.ocpjp.v8.2.1749 :

Posted: Mon Oct 03, 2016 12:21 am
by admin
Please try the exact code that is given it the question. It compiles and runs fine.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v8.2.1749 :

Posted: Mon Feb 26, 2018 10:29 pm
by rnatesan
I think it is always good to pick the time that is before the DST when one is before DST and another is after DST. In this case it is LocalDateTime.of(2015, Month.NOVEMBER, 1, 1, 0) and LocalDateTime.of(2015, Month.NOVEMBER, 1, 2, 0) respectively.

Then, add up 1 hour at a time to the time (which is before DST ) in order to reach the other time after DST.

Code: Select all

LocalDateTime.of(2015, Month.NOVEMBER, 1, 1, 0) + 1 = LocalDateTime.of(2015, Month.NOVEMBER, 1, 2, 0) which becomes LocalDateTime.of(2015, Month.NOVEMBER, 1, 1, 0)
so add 1 more to the above result:

Code: Select all

(LocalDateTime.of(2015, Month.NOVEMBER, 1, 1, 0) + 1) + 1 = LocalDateTime.of(2015, Month.NOVEMBER, 1, 2, 0)
we add 2 hours in total to get to the other time.

See here: http://tpcg.io/LUCsMp

Re: About Question enthuware.ocpjp.v8.2.1749 :

Posted: Tue Mar 20, 2018 11:01 pm
by Wesley
To be honest, I don't care much for this question. It assumes knowledge how Java will behave when you set it 1AM on the day the clocks go back. On that day we have two 1AMs. How does Java know which one we are referring to when we set the time?

If it were the 2nd 1AM then the time difference would only be 1 hour. It would have been less ambiguous if it was 12:59am to 2PM. Then we could say without a doubt that it'll take 2 hours and 1 minute. It seems like it's testing us on a very specific and rare corner case. Although.. if the test-makers at Oracle write questions like this it'll be good practice for us. But even in that case I would argue that they designed the question poorly.

Again, just my two cents. It's not like it's important or anything.

Re: About Question enthuware.ocpjp.v8.2.1749 :

Posted: Wed Aug 01, 2018 8:58 am
by __JJ__
I have been playing around with this and what seems to be the case is that when the clocks go back, java sees any time before 2am as "pre-DST change" and any time from 2am onwards as "post-DST change":

Code: Select all

        LocalDateTime ld1 = LocalDateTime.of(2015, Month.NOVEMBER, 1, 1, 59); 
        ZonedDateTime zd1 = ZonedDateTime.of(ld1, ZoneId.of("US/Eastern"));     
        LocalDateTime ld2 = LocalDateTime.of(2015, Month.NOVEMBER, 1, 2, 0); 
        ZonedDateTime zd2 = ZonedDateTime.of(ld2, ZoneId.of("US/Eastern")); 
        long m = ChronoUnit.MINUTES.between(zd1, zd2); System.out.println(m); //61
Please correct me if I'm wrong.

Re: About Question enthuware.ocpjp.v8.2.1749 :

Posted: Wed Aug 01, 2018 9:01 am
by admin
That sounds correct.

Re: About Question enthuware.ocpjp.v8.2.1749 :

Posted: Wed Aug 01, 2018 9:03 am
by __JJ__
Thank you.

Re: About Question enthuware.ocpjp.v8.2.1749 :

Posted: Sun Feb 23, 2020 4:02 pm
by bvrulez
To grasp this concept fully you have to understand, that if it already is 2 am in LocalDateTime, then the time change already happened. The clock is NOT turned back to 1 am!

2 am stays 2 am and now the question is how long does it take to go from 1 am to 2 am considering the time change. It takes, of course, 2 hours.

Re: About Question enthuware.ocpjp.v8.2.1749 :

Posted: Tue Dec 08, 2020 6:25 am
by micro-jr
in Poland we change time Zone from CET - Central European Time (GMT+1) to CEST - Central European Sumer Time (GMT+2). this is very confusing that they using the same name for difrent Zone. If they change time zone, they should use difrent time zone name, but stil have the same offset from GMT.

how did they know on witch 1:01 AM they have? before or after change to summer time?