Instant类 : 用于表示时间的对象

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

/*
    Instant类 : 用于表示时间的对象, 类似于之前学习的Date
*/
public class InstantDemo01 {
    public static void main(String[] args) {

        Instant now = Instant.now();
        System.out.println("当前的时间戳是:" + now);

        ZonedDateTime zonedDateTime = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(zonedDateTime);
    }
}
输出如下:
当前的时间戳是:2024-07-23T03:33:43.557678300Z
2024-07-23T11:33:43.557678300+08:00[Asia/Shanghai]

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

/*
    Instant常用方法 :

        static Instant now() : 获取当前时间的Instant对象(标准时间而非北京时间)
        static Instant ofXxx(long epochMilli) : 根据秒、毫秒、纳秒获取Instant对象
        ZoneDateTime at Zone(ZoneId zone) : 指定时区
        boolean isXxx(Instant otherInstant) : 判断系列的方法
        Instant minusXxx(long millisToSubtract) : 减少时间系列的方法
        Instant plusXxx(long millisToSubtract) : 增加时间系列的方法
*/
public class InstantDemo02 {
    public static void main(String[] args) {

        //获取当前时间的Instant对象(标准时间而非北京时间)
        Instant now = Instant.now();
        System.out.println("当前世界标准时间为:" + now);
        System.out.println("-------------------------");

        //根据秒、毫秒、纳秒获取Instant对象
        Instant instant1 = Instant.ofEpochMilli(1000);
        Instant instant2 = Instant.ofEpochSecond(5);
        System.out.println(instant1);
        System.out.println(instant2);
        System.out.println("-------------------------");

        //指定时区
        ZonedDateTime zonedDateTime = Instant.now().atZone(ZoneId.systemDefault());
        System.out.println("带时区的时间:" + zonedDateTime);
        System.out.println("-------------------------");

        //判断系列的方法
        System.out.println(now.isBefore(instant1));
        System.out.println(now.isAfter(instant1));
        System.out.println("-------------------------");

        //减少时间系列的方法
        System.out.println("减1000毫秒:" + now.minusMillis(1000));
        System.out.println("减五秒钟:" + now.minusSeconds(5));
        System.out.println("-------------------------");

        //增加时间系列的方法
        System.out.println("加1000毫秒:" + now.plusMillis(1000));
        System.out.println("加五秒钟:" + now.plusSeconds(5));
        System.out.println("-------------------------");
    }
}
输出如下:
当前世界标准时间为:2024-07-23T03:34:42.449735200Z
-------------------------
1970-01-01T00:00:01Z
1970-01-01T00:00:05Z
-------------------------
带时区的时间:2024-07-23T11:34:42.465356700+08:00[Asia/Shanghai]
-------------------------
false
true
-------------------------
减1000毫秒:2024-07-23T03:34:41.449735200Z
减五秒钟:2024-07-23T03:34:37.449735200Z
-------------------------
加1000毫秒:2024-07-23T03:34:43.449735200Z
加五秒钟:2024-07-23T03:34:47.449735200Z
-------------------------


  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值