# Instant
# 介绍
Instant时间线上的某个时刻/时间戳
作用:可以用来记录代码的执行时间,或用于记录用户操作某个事件的时间点。
- 通过获取Instant的对象可以拿到此刻的时间,该时间由两部分组成:从1970-01-0100:00:00开始走到此刻的总秒数+不够1秒的纳秒数
TIP
1秒=1000毫秒
1毫秒=1000微秒
1微秒=1000纳秒
1秒=1000000000纳秒
# 常用方法
方法名 | 说明 |
---|---|
public static Instant now() | 获取当前时间的Instant对象(标准时间) |
public long getEpochSecond() | 获取从1970-61-01T88:88:8开始记录的秒数。 |
public int getNano() | 从时间线开始,获取从第二个开始的纳秒数 |
plusMillis plusSeconds plusNanos | 增加时间 |
minusMillis minusSeconds minusNanos | 减少时间系列的方法 |
equals、isBefore、isAfter | 判断时间是否之前、之后、相等 |
# 获取总秒数
import java.time.Instant;
public class InstantTest {
public static void main(String[] args) {
Instant now = Instant.now();
//获取总秒数
long epochSecond = now.getEpochSecond();
System.out.println("总秒速:" + epochSecond);
}
}
# 获取纳秒
import java.time.Instant;
public class InstantTest {
public static void main(String[] args) {
Instant now = Instant.now();
//不够1秒的纳秒数
int nano = now.getNano();
System.out.println("不够1秒的纳秒数;" + nano);
}
}
# 操作时间
import java.time.Instant;
public class InstantTest {
public static void main(String[] args) {
Instant now = Instant.now();
Instant instant = now.plusMillis(1);
System.out.println(instant);
}
}