📅  最后修改于: 2020-10-01 06:42:16             🧑  作者: Mango
Java Instant类用于表示时间轴上的特定时刻。它继承了Object类并实现Comparable接口。
我们来看一下java.time.Instant类的声明。
public final class Instant extends Object
implements Temporal, TemporalAdjuster, Comparable, Serializable
Method | Description |
---|---|
Temporal adjustInto(Temporal temporal). | It is used to adjust the specified temporal object to have this instant. |
int get(TemporalField field) | It is used to get the value of the specified field from this instant as an int. |
boolean isSupported(TemporalField field) | It is used to check if the specified field is supported. |
Instant minus(TemporalAmount amountToSubtract) | It is used to return a copy of this instant with the specified amount subtracted. |
static Instant now() | It is used to obtain the current instant from the system clock. |
static Instant parse(CharSequence text) | It is used to obtain an instance of Instant from a text string such as 2007-12-03T10:15:30.00Z. |
Instant plus(TemporalAmount amountToAdd) | It is used to return a copy of this instant with the specified amount added. |
Instant with(TemporalAdjuster adjuster) | It is used to return an adjusted copy of this instant. |
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