Java SE 17 Developer — Question 53

Given the code fragment:

LocalDate now = LocalDate.now();
LocalDate this_labourDay = LocalDate.of(2021, Month.MAY, 1); LocalDate next_labourDay = this_labourDay.plusYears(1);
LocalDate add_week = next_labourDay.plusDays(7);
Period p = now.until(add_week);
System.out.println(p);

Assume LocalDate.now() returns 17th Aug 2021.

What is the result?

Answer options

Correct answer: B

Explanation

The code calculates the period from the current date (17th Aug 2021) to 8th May 2022, which is 8 months and 21 days. Hence, the result is represented as P8M21D, making option B the correct choice. The other options do not accurately represent the calculated period.