Given the code fragment:



What is the result?

A.

Travel time is 4 hours

B.

Travel time is 6 hours

C.

Travel time is 8 hours

D. An exception is thrown at line n1.

題解

這題是在考Java 8加入的日期與時間(Date-Time)API,可以參考以下文章:

https://magiclen.org/java-8-date-time-api/

ChronoUnit類別可以利用Duration類別計算兩日期或是時間的間隔。

(2015/1/15 9 + 5) - (2015/1/15 3 + 7) = 4 hours

所以答案是選項A。