Page 1 of 1

About Question enthuware.ocpjp.v8.2.2018 :

Posted: Fri Mar 10, 2017 12:37 pm
by rohitbe
I think the answer should be PT-3H since new york time is always ahead by 3 hours and we are doing Duration.between(newYorkTime, losAngelesTime)

example -
LocalDateTime losAngelesTime = LocalDateTime.of(2017, 1, 10,6,30); //LA
LocalDateTime newYorkTime = LocalDateTime.of(2017, 1, 10, 9, 30); //NY

System.out.println(Duration.between(losAngelesTime, newYorkTime )); //PT3H
System.out.println(Duration.between(newYorkTime, losAngelesTime )); //PT-3H

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

Posted: Fri Mar 10, 2017 1:04 pm
by admin
No, NY is ahead of LA. So it will be +3 and not -3.

Please check https://docs.oracle.com/javase/8/docs/a ... .Temporal- for details.
The result of this method can be a negative period if the end is before the start.

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

Posted: Fri May 19, 2017 11:12 am
by aceoftrumps
Still don`t understand your answer.

Doesn`t "NY is ahead of LA" mean that time in NY is greater than in LA by 3 hours?

i.e.
NY 9:30 AM
LA 6:30 AM

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

Posted: Fri May 19, 2017 10:17 pm
by admin
aceoftrumps wrote:Still don`t understand your answer.

Doesn`t "NY is ahead of LA" mean that time in NY is greater than in LA by 3 hours?

i.e.
NY 9:30 AM
LA 6:30 AM
Right. So NY minus LA will be +3, right?

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

Posted: Thu Jun 15, 2017 6:26 am
by sedletskiyv
deleted

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

Posted: Thu Jun 15, 2017 6:34 am
by admin
The JavaDoc for this method clearly says that the result of this method is a negative period if the end is before the start.
Here, end i.e. LA is NOT before start i.e. NY.
LA is after NY.

So I am not sure why you are expecting the result to be negative.
Paul.

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

Posted: Thu Jun 15, 2017 6:45 am
by sedletskiyv
admin wrote:The JavaDoc for this method clearly says that the result of this method is a negative period if the end is before the start.
Here, end i.e. LA is NOT before start i.e. NY.
LA is after NY.

So I am not sure why you are expecting the result to be negative.
Paul.
Thanks, I think the correct logic here is "6 a.m. in LA will happen 3 hours after the 6 a.m. happens in NY" - that's why PT3H.

I was misled by an incorrect example at the top post:
LocalDateTime losAngelesTime = LocalDateTime.of(2017, 1, 10,6,30); //LA
LocalDateTime newYorkTime = LocalDateTime.of(2017, 1, 10, 9, 30); //NY

System.out.println(Duration.between(newYorkTime, losAngelesTime )); //PT-3H

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

Posted: Tue Aug 29, 2017 9:26 am
by alexandrerisi
This question is wrong. When you say "3 hours ahead", it means if it is 10:00 in LA then it will be 13:00 in NY. For people who live in the USA it might be easy to figure the question didn't actually meant 3 hour ahead but +3 on on LA's time zone. If what you mean is +3 on time zone than you need to inform that LA is GMT-8 otherwise everyone will assume you are talking about time and not time zone.

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

Posted: Tue Aug 29, 2017 10:37 am
by admin
This is the terminology used by the questions in the real exam. It means exactly what it says. NY is indeed ahead of LA by 3 hours.

HTH,
Paul.

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

Posted: Sun Feb 04, 2018 9:11 am
by prakh_k
admin wrote:
aceoftrumps wrote:Still don`t understand your answer.

Doesn`t "NY is ahead of LA" mean that time in NY is greater than in LA by 3 hours?

i.e.
NY 9:30 AM
LA 6:30 AM
Right. So NY minus LA will be +3, right?
why not LA - NY? why you subtract the other way? can you explain cause it is very confusing!

take a look at similar example

Code: Select all

        Instant now = Instant.now();
        Thread.sleep(100);
        Instant later = Instant.now();

        System.out.println(now);
        System.out.println(later);
        System.out.println(Duration.between(now, later).toMillis());
it prints something like

Code: Select all

2018-02-04T14:14:16.652Z
2018-02-04T14:14:16.764Z
112
so it looks like Duration.between(now, later) is later-now, isn't it?

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

Posted: Sun Feb 04, 2018 10:57 am
by admin
Well, did you read the complete thread above?
If NY is ahead of LA then at any given instant, the local time at NY will be later than the local time at LA. For example, if it is 1PM in LA then it will be 4PM in NY at that very instant.
In your example also, end is not before start, so why would you expect a negative number?

Please go through the thread above carefully and also read the JavaDoc API description of Duration.between method.

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

Posted: Mon Mar 19, 2018 12:04 am
by Wesley
I got messed up by this one too, but I figured it out. I think the confusion comes from the explanation.

The question says NY is 3 hours ahead of LA so that's why we start thinking "It's 3 hours later, so.. we have to go back in time 3 hours to get to LA"

But.. The time is 6am in both places, only the time zone changes. 6am in NY on any given day happens 3 hours before 6am in LA. So this: Duration d = Duration.between(nyZdt, laZdt); means starting at 6am in NY how long does it take until it's 6am in LA. 3 hours.

That's why we have practice tests with explanations.. lol.

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

Posted: Mon Mar 04, 2019 10:57 am
by Touciuciu
Thank you, Wesley!!!

This was very confussing but now I got it thanks to you :)

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

Posted: Thu May 30, 2019 7:55 am
by Cannelids
I also find descriptions of time differences potentially confusing, and got this wrong during the test. In case it helps anyone still studying for this exam and still unsure after reading the above discussions, here's the way I thought about it reviewing afterwards (just different words for same explanations):
Both will say 6am, but the LA 6am will be 3 hours farther along in universal time than NY; confirmed this by printing Instant equivalents below...

Code: Select all

        
        LocalDateTime ldt = LocalDateTime.of(2017, 12, 02, 6, 0, 0);
        
        // --I added the following declarations to define the nyZone and laZone variables below
        ZoneId nyZone = ZoneId.of("US/Eastern");
        ZoneId laZone = ZoneId.of("US/Pacific");

        ZonedDateTime nyZdt = ldt.atZone(nyZone);
        ZonedDateTime laZdt = ldt.atZone(laZone);
        Duration d = Duration.between(nyZdt, laZdt);
        System.out.println(d); // PT3H
        
        // I added to view time representations...
        System.out.println(nyZdt); // 2017-12-02T06:00-05:00[US/Eastern]
        System.out.println(laZdt); // 2017-12-02T06:00-08:00[US/Pacific]
        // ...and converting to Instant, will see LA time is 3 hours later...
        System.out.println(nyZdt.toInstant()); // 2017-12-02T11:00:00Z  -- 11:00 hours (5 hours later than GMT/UTC)
        System.out.println(laZdt.toInstant()); // 2017-12-02T14:00:00Z  -- 14:00 hours (8 hours later than GMT/UTC) 
        

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

Posted: Thu May 30, 2019 8:17 am
by admin
Thank you for sharing your perspective. Different people grasp concepts differently and your perspective will definitely help some people make sense of this.

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

Posted: Fri Apr 30, 2021 9:56 am
by flaviu.vanca
Hi Guys,
This question is brilliant, it got me good I answered PT-3H. This question it does not asses you on the time between the two city's it asses you on the zone id of the two city's, to understand this question you must understand how time zone works. Time Zone Map . I added an extra zone id from Europe which is Bucharest just to have a positive number.
Here is how this code works:
The question states: "Given that New York is 3 hours ahead of Los Angeles, what will the following code print?"
A LocalDateTime is created and given a custom date and time (2017, 12, 02, 6, 0, 0).
The zone id's are:
New York -> date 02/12/2017, time 6:00. Remember custom LocalDateTime.of(2017, 12, 02, 6, 0, 0) and zone id -5.
Los Angeles -> date 02/12/2017, time 3:00 because New York's time is ahead of Los Angeles time by 3 hours, zone id -8.
Bucharest -> date of 02/12/2017, time 11:00 because Bucharest its ahead of New York by 7 hours, zone id +2.
When the arguments are passed to between() method behind the scene a subtraction of the zone id's is made(nyZdt - laZdt). Image