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?
A.
2016-02-14
B. A DateTimeException is thrown.
C.
2016-02-29
D. A compilation error occurs at line n1.
題解
這題是在考 Java 8 加入的日期與時間(Date-Time) API,可以參考以下文章:
程式第 6 行會建立出一個「2015/02/14」的 LocalDate 物件。
程式第 7 行會建立出一個「2016/02/14」的 LocalDate 物件。
程式第 8 行會建立出一個「2016/02/29」的 LocalDate 物件,但這個物件參考不會被任何變數儲存。
程式第 9 行會輸出第 7 行 nextYear 變數所參考到的 LocalDate 物件,也就是「2016/02/14」,所以會輸出「2016-02-14」。
