Java JDK8 的 java.time API 提供全面的 date 和 time 的模型,用起来也方便。
下面是使用案例:求两个时间点的距离。
package com.tony.test; import static java.lang.System.out; import java.time.Duration; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; public class DateTimeExample { /** * example for Date and Time in Java 8 * @throws Exception */ public void run() throws Exception { LocalTime startT = LocalTime.parse("01:03:03"); LocalTime endT = LocalTime.parse("01:04:06"); Duration duration = Duration.between(startT, endT); out.println("Duration : " + duration.getSeconds()); LocalDate ll = LocalDate.now(); out.println(ll); LocalTime lt = LocalTime.now(); out.println(lt); LocalDateTime ldt = LocalDateTime.now(); out.println(ldt); } }
调用 run 方法后的运行结果如下:
Duration : 63 2016-05-01 15:35:31.792 2016-05-01T15:35:31.792
参考资料:
时间: 2024-10-08 10:04:20