Given the code fragment:

public static void main(String[] args) {
    String date = LocalDate.parse("2014-05-04").format(DateTimeFormatter.ISO_DATE_TIME);
    System.out.println(date);
}

What is the result?

A.

May 04, 2014T00:00:00.000

B.

2014-05-04T00:00:00.000

C.

5/4/14T00:00:00.000

D. An exception is thrown at runtime.

题解

程序第 7 行会使用 parse 方法产生 LocalDate 对象,再用 LocalDate 对象的 format 方法将 LocalDate 对象转成字符串,但此时会抛出 UnsupportedTemporalTypeException 例外。原因在于 ISO_DATE_TIME 这个 DateTimeFormatter 无法用于 LocalDate 对象,LocalDate 对象并没有时间的信息。

若改成使用 ISO_DATE 这个 DateTimeFormatter,则输出结果为:

2014-05-04