I am preaparing for the Java OCP Test, and in the mock test there is a question about Java DateTime like this:
Given that New York is 3 hours ahead of Los Angeles, what will the following code print?
LocalDateTime ldt = LocalDateTime.of(2017, 12, 02, 6, 0, 0); ZonedDateTime nyZdt = ldt.atZone(nyZone); ZonedDateTime laZdt = ldt.atZone(laZone); Duration d = Duration.between(nyZdt, laZdt); System.out.println(d);
And the correct answer is PT3H but I am a little bit confused here is if the book gives the wrong answer or not?
Given is NY 3 hours ahead of LA, does it mean, for example, NY is 5:00 and then LA will be 2:00. So the Duration.between(5,2) should be PT-3H because according to the Javadoc: The result of this method can be a negative period if the end is before the start. To guarantee to obtain a positive duration call abs() on the result., and in this case "2" is before "5" so the result should be PT-3H, not PT3H.
What do you think, which one is correct?