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.
About Question enthuware.ocpjp.v8.2.2018 :
Moderator: admin
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.2018 :
If you like our products and services, please help us by posting your review here.
-
- Posts: 11
- Joined: Sun Mar 18, 2018 9:08 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.2018 :
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.
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.
-
- Posts: 11
- Joined: Fri Aug 17, 2018 8:52 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.2018 :
Thank you, Wesley!!!
This was very confussing but now I got it thanks to you
This was very confussing but now I got it thanks to you
-
- Posts: 25
- Joined: Thu Jun 01, 2017 2:50 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.2018 :
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...
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)
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.2018 :
Thank you for sharing your perspective. Different people grasp concepts differently and your perspective will definitely help some people make sense of this.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Fri Apr 30, 2021 8:47 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.2018 :
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).
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).
- Attachments
-
- 2021-04-30 (1).png (71.11 KiB) Viewed 4427 times
Who is online
Users browsing this forum: No registered users and 55 guests