Java Instant类

时间点

此类的对象表示时间线上的一点。可以理解为人类的绝对时间。

因为是时间线的一点,所以时间点可以比较大小,那么这个时间点的标准都是统一的(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;

三、时间点的构造

(1)当前时间点

		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

(2)根据距离原点的秒构造时间点

这个构造方法不够精准,无法到纳秒级。

①距离原点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

(3)构造到纳秒级的时间点

源码:

 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

(4)构造到毫秒级的时间点

		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

(5)根据文本构造时间点

对字符串的格式有要求,字符串必须如同: 2007-12-03T10:15:30.00Z

		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

五、时间点的运算

(1)时间点加秒级、毫秒、纳秒

①原点加100000s

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

(2)时间点加任意时间单位

①原点加一百天

		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
  • 7
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值