【Java】Instant 时间戳

它就表示是某一个固定的时间的对象。

在这个类中,它有常见的一系列的方法

static Instant now() // 获取当前时间的Instant对象(它获取的是标准时间,是不带时区的!如果想要获取到中国的时间,需要在这个基础上加8个小时)
static Instant ofXxxx(long epochMilli) 	// 根据(秒/毫秒/纳秒)获取指定时间的Instant对象
ZonedDateTime atZone(ZoneIdzone) // 指定时区,方法的返回值是ZonedDateTime,它是一个带时区的事件对象
boolean isxxx(Instant otherInstant) // 判断系列的方法
Instant minusXxx(long millisToSubtract) // 减少时间系列的方法
Instant plusXxx(long millisToSubtract) // 增加时间系列的方法

一、获取当前时间的Instant对象(标准时间)

它获取的是标准时间,是不带时区的!如果想要获取到中国的时间,需要在这个基础上加8个小时。

static Instant now() // 获取当前时间的Instant对象

代码示例

Instant now = Instant.now();
System.out.println(now);

程序运行完毕,看控制台,年月日、时分秒、纳秒。这个就是当前的世界标准时间。

image-20240424134756323

二、获取指定的时间

static Instant ofXxxx(long epochMilli) 	// 根据(秒/毫秒/纳秒)获取指定时间的Instant对象

注意,这里是 ofXXX,用 Instant类 去调用 of 开头的方法,方法有三个


ofEpochMilli(long epochMilli)

我们先来看第一个:ofEpochMilli(long epochMilli)

image-20240424135146142

这个方法的第三个单词:Milli 就是毫秒的意思,因此后面的参数就表示时间的毫秒,我可以通过时间的毫秒值来获取到一个Instant的对象。

Instant instant1 = Instant.ofEpochMilli(0L);
System.out.println(instant1); // 1970-01-01T00:00:00z

程序运行外部,看见获取的是计算机时间原点的Instant对象,而且是世界的标准时间,这个是不带时区的。

image-20240424135634866

ofEpochSecond(long epochSecond)

second:秒钟。

因此你调用这个方法,括号中传入的就是秒钟。

假设我在这里传入 1L:表示计算机时间原点开始后过了 1秒 的时间。

Instant instant2 = Instant.ofEpochSecond(1L);
System.out.println(instant2); // 1970-01-01T00:00:01Z

ofEpochSecond(long epochSecond, long nanoAdjustment)

这个方法有两个参数:

  • epochSecond:跟刚刚一样,表示的也是秒钟

  • nanoAdjustment:纳秒。

    nano 就是 纳秒的意思

下面代码表示:从计算机时间原点开始,多了这么多时间(1秒 + 1000000000纳秒)的那个时间点。

PS:1秒 = 1000000000纳秒,因此下面刚好就是过了两秒钟。

Instant instant3 = Instant.ofEpochSecond(1L, 1000000000L);
System.out.println(instant3); // 1970-01-01T00:00:027

三、指定时区

ZonedDateTime atZone(ZoneIdzone) // 指定时区,方法的返回值是ZonedDateTime,它是一个带时区的事件对象

由于 atZone()方法 不是静态的,因此我们需要通过 Instant.now() 获取到当前时间的对象,然后再用时间对象去调用 atZone() 方法。

Instant.now().atZone();

atZone() 需要传入时区,但是这个方法的参数类型并不是一个字符串,而是 ZoneId,因此就可以用到我们刚刚学习的 ZoneId.of() 方法来获取时区对象。

image-20240424141228956
ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
System.out.println(time);

程序运行完毕,可以发现已经在标准时间之上加上8小时了。

image-20240424141427645

四、判断系列的方法

boolean isxxx(Instant otherInstant) // 判断系列的方法
isBefore():判断调用者代表的时间是否在参数表示时间的前面
isAfter():判断调用者代表的时间是否在参数表示时间的后面

既然是判断,那我就先获取到两个 Instant的对象

Instant instant4 = Instant.ofEpochMilli(0L); // 表示计算机的时间原点
Instant instant5 = Instant.ofEpochMilli(1000L); // 表示计算机的时间原点上加上1000毫秒的时间点

然后我想要比较这两个时间,谁在前,谁在后。

此时我们不需要去获取到里面的毫秒值,而是调用 isAfter() / isBefore() 方法

//isBefore:判断调用者代表的时间是否在参数表示时间的前面
boolean result1=instant4.isBefore(instant5);
System.out.println(result1); // true,表示当前的调用者 instant4 在 instant5 之前

//isAfter:判断调用者代表的时间是否在参数表示时间的后面
boolean result2 = instant4.isAfter(instant5);
System.out.println(result2);// false,表示 instant4 不在 instant5 之后

五、减少时间系列的方法

方法表示以当前时间为基准,往前去减少时间

细节:JDK8新增的这些时间的对象,它是不可变的,如果你对它发生了减少 / 增加,那么原有的时间对象是不会变的,它会产生一个新的 Instant对象,所以说方法的返回值就是 Instant的对象

Instant minusXxx(long millisToSubtract) // 减少时间系列的方法

首先我们先来获取一个时间对象

Instant instant6 =Instant.ofEpochMilli(3000L);
System.out.println(instant6); // 1970-01-01T00:00:03Z

然后就可以调用 minusXXX 方法来减。

它在减的时候,可以减秒钟(minusSeconds()),也可以减毫秒(minusMillis),还可以减纳秒(minusNanos)

image-20240424142852944

我这里就来减1s了

Instant instant7 = instant6.minusSeconds(1);
System.out.println(instant7); // 1970-01-01T00:00:02Z

plus方法minus方法 同理

Instant plusXxx(long millisToSubtract) // 增加时间系列的方法

方法稍微有一点多,但是这些方法千万不要背,根据方法名取猜一猜它的意思就行了。

  • 21
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值