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 枚举的 between 方法可以计算两个日期或是时间的间隔。
(2015/1/15 9 + 5) - (2015/1/15 3 + 7) = 4 hours
所以答案是选项 A。
