OCP Date, Time, and Localization 2 — Questions and Answers
Question 1: Which Java class should you use to represent a single point in time as seconds since the Unix epoch?
- LocalDateTime
- ZonedDateTime
- Instant (Correct answer)
- Timestamp
Correct answer: Instant
Instant models a single instantaneous point on the timeline in epoch-seconds and nanosecond adjustment, independent of any time zone.
Question 2: What is the result of `LocalDate.of(2024, 1, 31).plusMonths(1)`?
- 2024-02-29 (Correct answer)
- 2024-02-28
- A DateTimeException is thrown
- 2024-03-02
Correct answer: 2024-02-29
Java adjusts the day to the last valid day of the target month; February 2024 has 29 days (leap year), so the result is 2024-02-29.
Question 3: Which expression correctly calculates the total number of days between two LocalDate instances?
- Period.between(date1, date2).getDays()
- ChronoUnit.DAYS.between(date1, date2) (Correct answer)
- Duration.between(date1, date2).toDays()
- date1.until(date2)
Correct answer: ChronoUnit.DAYS.between(date1, date2)
ChronoUnit.DAYS.between(date1, date2) returns the total elapsed days; Period.between().getDays() only returns the day component, not the total, and Duration.between() does not accept LocalDate.
Question 4: How do you convert a ZonedDateTime to the same instant in a different time zone?
- zonedDateTime.withZoneSameInstant(ZoneId.of("Europe/London")) (Correct answer)
- zonedDateTime.toZone(ZoneId.of("Europe/London"))
- ZonedDateTime.of(zonedDateTime, ZoneId.of("Europe/London"))
- zonedDateTime.atZone(ZoneId.of("Europe/London"))
Correct answer: zonedDateTime.withZoneSameInstant(ZoneId.of("Europe/London"))
withZoneSameInstant() converts the ZonedDateTime to a new zone while keeping the underlying instant the same, correctly shifting the wall-clock time.
Question 5: What is the output of `DateTimeFormatter.ofPattern("MM/dd/uuuu").format(LocalDate.of(2024, 6, 5))`?
- "6/5/2024"
- "06/05/2024" (Correct answer)
- "06/5/2024"
- "6/05/2024"
Correct answer: "06/05/2024"
The pattern MM pads the month to 2 digits and dd pads the day to 2 digits, so month 6 becomes "06" and day 5 becomes "05", yielding "06/05/2024".
Question 6: Which statement about the zone ID "US/Eastern" is correct?
- It is a valid alias for America/New_York and handles DST automatically (Correct answer)
- It throws a ZoneRulesException because the format is invalid
- It creates a fixed offset without DST adjustment
- It is not recognized by Java's built-in zone database
Correct answer: It is a valid alias for America/New_York and handles DST automatically
"US/Eastern" is a legacy IANA alias for "America/New_York" and is fully recognized by Java's ZoneId, including DST rules.
Question 7: What is the key difference between ZonedDateTime and OffsetDateTime?
- ZonedDateTime includes DST rules tied to a geographic zone; OffsetDateTime stores a fixed UTC offset only (Correct answer)
- OffsetDateTime is more precise because it stores nanoseconds
- ZonedDateTime can only represent UTC times
- OffsetDateTime automatically adjusts for daylight saving transitions
Correct answer: ZonedDateTime includes DST rules tied to a geographic zone; OffsetDateTime stores a fixed UTC offset only
ZonedDateTime carries full time-zone rules (including DST transitions) via a ZoneId, whereas OffsetDateTime holds only a fixed numeric offset with no DST awareness.
Which Java class should you use to represent a single point in time as seconds since the Unix epoch?