Date-Time API
的核心类之一是Instant类,它代表时间线上纳秒的开始。这个类用于生成一个时间戳来代表机器时间。
Java Instant
类用于表示时间线上的特定时刻。
本指南的例子可在Github上找到。Instant c
类提供了很多API/方法,但在本指南中,我们将讨论几个重要的和经常使用的Instant
API/方法,并举例说明。
下面的类图显示了 Instant类提供的API列表。
import java.time.Instant;
public class InstantExample1 {
public static void main(String[] args) {
Instant inst = Instant.parse("2017-02-03T10:37:30.00Z");
System.out.println(inst);
}
}
输出
2017-02-03T10:37:30Z
import java.time.Instant;
public class InstantExample2 {
public static void main(String[] args) {
Instant instant = Instant.now();
System.out.println(instant);
}
}
输出
2017-02-03T06:11:01.194Z
import java.time./*;
public class InstantExample3 {
public static void main(String[] args) {
Instant instant = Instant.parse("2017-02-03T11:25:30.00Z");
instant = instant.minus(Duration.ofDays(125));
System.out.println(instant);
}
}
输出
2016-10-01T11:25:30Z
import java.time./*;
public class InstantExample4 {
public static void main(String[] args) {
Instant inst1 = Instant.parse("2017-02-03T11:25:30.00Z");
Instant inst2 = inst1.plus(Duration.ofDays(125));
System.out.println(inst2);
}
}
输出
2017-06-08T11:25:30Z
import java.time.Instant;
import java.time.temporal.ChronoUnit;
public class InstantExample5 {
public static void main(String[] args) {
Instant inst = Instant.parse("2017-02-03T11:35:30.00Z");
System.out.println(inst.isSupported(ChronoUnit.DAYS));
System.out.println(inst.isSupported(ChronoUnit.YEARS));
}
}
输出
true
false
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.javaguides.net/2018/07/java-8-instant-class-api-guide.html
内容来源于网络,如有侵权,请联系作者删除!