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」。
