Given the code fragment:



ZonedDateTime depart = ZonedDateTime.of(2015, 1, 15, 3, 0, 0, 0, ZoneId.of("UTC-7"));
ZonedDateTime arrive = ZonedDateTime.of(2015, 1, 15, 9, 0, 0, 0, ZoneId.of("UTC-5"));
long hrs = ChronoUnit.HOURS.between(depart, arrive); //line n1
System.out.println("Travel time is " + hrs + " hours");

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,可以參考以下文章:

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

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

所以答案是選項A。