Java LocalDate: Adding Years and Days - Exam 1Z0-809

Java LocalDate: Adding Years and Days

Question

Given the code fragment: LocalDate valentinesDay =LocalDate.of(2015, Month.FEBRUARY, 14); LocalDate nextYear = valentinesDay.plusYears(1); nextYear.plusDays(15); //line n1 System.out.println(nextYear); What is the result?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

A.

The correct answer is A. 2016-02-14.

Explanation: The given code fragment creates a LocalDate object named valentinesDay representing February 14th, 2015. Then, it creates another LocalDate object named nextYear by adding 1 year to the valentinesDay object. Therefore, nextYear represents February 14th, 2016.

On the next line, it calls the plusDays() method on the nextYear object with an argument of 15. However, the plusDays() method does not modify the original nextYear object, but instead returns a new LocalDate object that represents 15 days after the original nextYear object.

Since the code does not assign the new LocalDate object returned by plusDays() to any variable, the result of that operation is discarded.

Finally, the code prints the value of the original nextYear object using the println() method. Therefore, the output will be:

2016-02-14