About Question enthuware.ocpjp.v8.2.2018 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
rohitbe
Posts: 5
Joined: Fri Mar 10, 2017 12:25 pm
Contact:

About Question enthuware.ocpjp.v8.2.2018 :

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

aceoftrumps
Posts: 1
Joined: Mon Aug 24, 2015 4:40 am
Contact:

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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?
If you like our products and services, please help us by posting your review here.

sedletskiyv
Posts: 3
Joined: Sat Jun 03, 2017 11:11 am
Contact:

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

Post by sedletskiyv »

deleted
Last edited by sedletskiyv on Thu Jun 15, 2017 6:36 am, edited 1 time in total.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

sedletskiyv
Posts: 3
Joined: Sat Jun 03, 2017 11:11 am
Contact:

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

Post 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

alexandrerisi
Posts: 1
Joined: Sat Aug 12, 2017 4:44 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

prakh_k
Posts: 6
Joined: Sat Jan 20, 2018 12:06 pm

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Wesley
Posts: 11
Joined: Sun Mar 18, 2018 9:08 pm
Contact:

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

Post 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.

Touciuciu
Posts: 11
Joined: Fri Aug 17, 2018 8:52 am
Contact:

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

Post by Touciuciu »

Thank you, Wesley!!!

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

Cannelids
Posts: 25
Joined: Thu Jun 01, 2017 2:50 pm
Contact:

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

Post 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) 
        

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

flaviu.vanca
Posts: 1
Joined: Fri Apr 30, 2021 8:47 am
Contact:

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

Post 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
Attachments
2021-04-30 (1).png
2021-04-30 (1).png (71.11 KiB) Viewed 4419 times

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 69 guests