nerdexam
Oracle

1Z0-809 · Question #195

In 2015, daylight saving time in New York, USA, begins on March 8th at 2:00 AM. As a result, 2:00 AM becomes 3:00 AM. Given the code fragment: ZonedId zone = ZoneId.of("America/New_York")…

The correct answer is D. 4:00 - difference: 2. Option D is correct because plusHours(2) operates on the underlying UTC instant, not the wall clock. Starting at 1:00 AM EST (UTC-5), adding 2 real hours yields 8:00 AM UTC, which converts to 4:00 AM EDT (UTC-4) after the spring-forward - so the formatter prints 4:00…

Question

In 2015, daylight saving time in New York, USA, begins on March 8th at 2:00 AM. As a result, 2:00 AM becomes 3:00 AM. Given the code fragment: ZonedId zone = ZoneId.of("America/New_York"); ZonedDateTime dt = ZonedDateTime.of(LocalDate.of(2015, 3, 8), LocalTime.of(1, 0), zone); ZonedDateTime dt2 = dt.plusHours(2); System.out.print(DateTimeFormatter.ofPattern("H:mm - ").format(dt2)); System.out.print(ChronoUnit.HOURS.between(dt, dt2)); What is the result?

Options

  • A3:00 - difference: 2
  • B2:00 - difference: 1
  • C4:00 - difference: 3
  • D4:00 - difference: 2

How the community answered

(22 responses)
  • A
    14% (3)
  • B
    5% (1)
  • C
    5% (1)
  • D
    77% (17)

Explanation

Option D is correct because plusHours(2) operates on the underlying UTC instant, not the wall clock. Starting at 1:00 AM EST (UTC-5), adding 2 real hours yields 8:00 AM UTC, which converts to 4:00 AM EDT (UTC-4) after the spring-forward - so the formatter prints 4:00. ChronoUnit.HOURS.between() also compares raw instants (6:00 UTC vs. 8:00 UTC), giving a difference of 2, not the 3 hours the wall clock would appear to show.

Why the distractors fail:

  • A (3:00 - 2): Gets the difference right but the time wrong - naively adding 2 wall-clock hours from 1:00 AM might suggest 3:00 AM, but plusHours skips over the missing hour to land at 4:00 AM.
  • B (2:00 - 1): Both values are wrong; this confuses the DST gap with stopping at the clocks-forward boundary.
  • C (4:00 - 3): Gets the time right but misreads between() as counting wall-clock hours (1→2→[gap]→3→4 looks like 3), when it actually measures elapsed real time.

Memory tip: In Java's date-time API, both plusHours() and ChronoUnit.HOURS.between() deal in real elapsed seconds under the hood - DST relabels the clock face but never changes the math of actual time passing.

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice