java.time.Duration
1 java.time.Duration介绍
Java Duration类用于以秒和纳秒为单位测量时间。它继承了Object类并实现Comparable接口。
2 java.time.Duration声明
让我们来看一下java.time.Duration类的声明。
public final class Duration extends Object
implements TemporalAmount, Comparable<Duration>, Serializable
3 java.time.Duration方法
4 java.time.Duration案例1
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.time.Duration的例子
*/
import java.time.*;
import java.time.temporal.ChronoUnit;
public class DurationExample1 {
public static void main(String[] args) {
Duration d = Duration.between(LocalTime.NOON,LocalTime.MAX);
System.out.println(d.get(ChronoUnit.SECONDS));
}
}
输出结果为:
43199
5 java.time.Duration案例2
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.time.Duration的例子
*/
import java.time.*;
public class DurationExample2 {
public static void main(String[] args) {
Duration d1 = Duration.between(LocalTime.MAX,LocalTime.NOON);
System.out.println(d1.isNegative());
Duration d2 = Duration.between(LocalTime.NOON,LocalTime.MAX);
System.out.println(d2.isNegative());
}
}
输出结果为:
true
false
热门文章
优秀文章