Date, Time, and Localization Flashcards
7 cards from real OCP practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Date, Time, and Localization flashcards as text
Which Java class should you use to represent a single point in time as seconds since the Unix epoch?
Answer: Instant
Instant models a single instantaneous point on the timeline in epoch-seconds and nanosecond adjustment, independent of any time zone.
What is the result of `LocalDate.of(2024, 1, 31).plusMonths(1)`?
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.
Which expression correctly calculates the total number of days between two LocalDate instances?
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.
How do you convert a ZonedDateTime to the same instant in a different time zone?
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.
What is the output of `DateTimeFormatter.ofPattern("MM/dd/uuuu").format(LocalDate.of(2024, 6, 5))`?
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".
Which statement about the zone ID "US/Eastern" is 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.
What is the key difference between ZonedDateTime and OffsetDateTime?
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.