此类的对象表示时间线上的一点。可以理解为人类的绝对时间
。
因为是时间线的一点,所以时间点可以比较大小,那么这个时间点的标准都是统一的(UTC时间);即这个时间点对于整个地球人们来说是唯一的,不是北京时间、也不是东京时间、而是世界时间。
与时间点相关的两个概念就是时间线和时间的单位
。
时间点在时间线上(点在线上)。时间线上有三个重要的点:最大值点、最小值点、原点(时间不知道有没有尽头)。
在Java世界,时间线的末端分别是Instant.MAX和Instant.MIN;时间线的原点是Instant.EPOCH
(1)时间线上的原点
System.out.println(Instant.EPOCH);
结果:1970-01-01T00:00:00Z
(2)时间线的最大值
System.out.println(Instant.MAX);
结果:+1000000000-12-31T23:59:59.999999999Z
(3)时间线的最小值
System.out.println(Instant.MIN);
结果:-1000000000-01-01T00:00:00Z
年、月、日、时、分、秒都是时间的单位。
现在人们把控自己生活节奏已经到了分钟这个单位;而科技的进步要求把控我们的机器的时间单位是秒、毫秒、纳秒。
在时间线上,我们使用秒(s)作为时间点的单位
。
1s(1秒)= 1000ms(1000毫秒) = 1000_000us(微妙)=1000_000_000ns(纳秒)
1day(天)=86400s(秒)=8640000ms(秒)
1year(年)= 365.2425day(天) = 31556952s(秒)= 31556952000ms(毫秒)
在Instant对象中,有一个long长度的值表示距离原点的长度(单位秒),一个int长度的值表示纳秒(永远是正数,不会大于999_999_999;因为1000_000_000ns = 1s
)。
源码:
/**
* The number of seconds from the epoch of 1970-01-01T00:00:00Z.
*/
private final long seconds;
/**
* The number of nanoseconds, later along the time-line, from the seconds field.
* This is always positive, and never exceeds 999,999,999.
*/
private final int nanos;
Instant now = Instant.now();
System.out.println(now);
System.out.println(now.getEpochSecond());
System.out.println(now.getNano());
System.out.println(now.toEpochMilli());
结果:
2019-04-18T08:04:29.633Z
1555574669
633000000
1555574669633
这个构造方法不够精准,无法到纳秒级。
①距离原点1秒
Instant now = Instant.ofEpochSecond(1L);
System.out.println(now);
System.out.println(now.getEpochSecond());
System.out.println(now.getNano());
System.out.println(now.toEpochMilli());
结果:
1970-01-01T00:00:01Z
1
0
1000
②距离原点-1秒
Instant now = Instant.ofEpochSecond(-1L);
System.out.println(now);
System.out.println(now.getEpochSecond());
System.out.println(now.getNano());
System.out.println(now.toEpochMilli());
结果:
1969-12-31T23:59:59Z
-1
0
-1000
源码:
public static Instant ofEpochSecond(long epochSecond, long nanoAdjustment) {
long secs = Math.addExact(epochSecond, Math.floorDiv(nanoAdjustment, NANOS_PER_SECOND));
int nos = (int)Math.floorMod(nanoAdjustment, NANOS_PER_SECOND);
return create(secs, nos);
}
这个方法有两个参数,第一个是秒,第二个是可以为正负的纳秒值。
Instant now = Instant.ofEpochSecond(5L,500L);
System.out.println(now);
System.out.println(now.getEpochSecond());
System.out.println(now.getNano());
System.out.println(now.toEpochMilli());
1970-01-01T00:00:05.000000500Z
5
500
5000
Instant now = Instant.ofEpochSecond(5L,-500L);
System.out.println(now);
System.out.println(now.getEpochSecond());
System.out.println(now.getNano());
System.out.println(now.toEpochMilli());
1970-01-01T00:00:04.999999500Z
4
999999500
4999
Instant now = Instant.ofEpochMilli(5500L);
System.out.println(now);
System.out.println(now.getEpochSecond());
System.out.println(now.getNano());
System.out.println(now.toEpochMilli());
1970-01-01T00:00:05.500Z
5
500000000
5500
Instant now = Instant.parse("1007-12-03T10:15:30.00Z");
System.out.println(now);
System.out.println(now.getEpochSecond());
System.out.println(now.getNano());
System.out.println(now.toEpochMilli());
1007-12-03T10:15:30Z
-30360318270
0
-30360318270000
A.compareTo(B):A>B,return 1;A= B,return 0;A<B,return -1;
Instant datetime1 = Instant.parse("2007-12-03T10:15:30.00Z");
Instant datetime2 = Instant.parse("2017-12-03T10:15:30.00Z");
Instant datetime3 = Instant.parse("2019-12-03T10:15:30.00Z");
System.out.println(datetime2.compareTo(datetime3));
System.out.println(datetime2.compareTo(datetime2));
System.out.println(datetime2.compareTo(datetime1));
System.out.println(datetime2.isAfter(datetime2));
System.out.println(datetime2.isBefore(datetime3));
-1
0
1
false
true
Instant plusSeconds = Instant.EPOCH.plusSeconds(100000L);
System.out.println(plusSeconds);
System.out.println(plusSeconds.getEpochSecond());
System.out.println(plusSeconds.toEpochMilli());
System.out.println(plusSeconds.getNano());
1970-01-02T03:46:40Z
100000
100000000
0
Instant plus100days = Instant.EPOCH.plus(100, ChronoUnit.DAYS);
System.out.println(plus100days);
System.out.println(plus100days.getEpochSecond());
System.out.println(plus100days.toEpochMilli());
System.out.println(plus100days.getNano());
1970-04-11T00:00:00Z
8640000
8640000000
0