Instant 新日期类的使用 API

package com.xxl.job.admin.mytest;

import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;

public class ForkJoinDemo {
    public static void main(String[] args) {

        //1.获取当前时间的Instant对象
        Instant now01 = Instant.now();
        System.out.println(now01);
        System.out.println("纪元秒 : "+now01.getEpochSecond());
        System.out.println("时间戳 : "+System.currentTimeMillis());
        System.out.println("毫  秒 : "+now01.toEpochMilli());
        System.out.println("纳  秒 : "+now01.getNano());

        System.out.println("===========================");

        // 2.获取指定时间的Instant对象
        Instant instant01 = Instant.ofEpochSecond(1);
        System.out.println(instant01);
        System.out.println("纪元秒 : "+instant01.getEpochSecond());
        System.out.println("毫  秒 : "+instant01.toEpochMilli());
        System.out.println("纳  秒 : "+instant01.getNano());
        System.out.println("===========================");

        //3.指定时间戳创建 带时区的日期时间对象 ZoneDateTime
        Instant instant02 = Instant.ofEpochSecond(1647784071); // 2022-03-20 21:47:51
        ZonedDateTime zonedDateTime = instant02.atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println("zonedDateTime = " + zonedDateTime);
        System.out.println("===========================");

        // 4.指定时间戳创建  默认时区的日期时间对象 LocalDateTime
        Instant instant03 = Instant.ofEpochSecond(1); // 2022-03-20 21:47:51
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant03, ZoneId.systemDefault());
        System.out.println("localDateTime = " + localDateTime);
        System.out.println("===========================");


        //instant 相当于 date
        Instant now = Instant.now();
        // 通过这种方式获取的时间戳与北京时间相差8个时区,需要修正为北京时间,
        // 通过查看源代码发现Instant.now()使用等是UTC时间Clock.systemUTC().instant()。LocalDate、LocalDateTime
        // 的now()方法使用的是系统默认时区 不存在Instant.now()的时间问题。
        System.out.println("结果01:"+now);
        // 加 8h 之后的 Instant 值如下
        Instant instant = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8));
        System.out.println("结果02:"+instant);

        Date date = new Date();
        System.out.println("结果03:"+date);

        // instant 转 date 类方法(java.util.date)
        Date from = Date.from(instant);
        System.out.println("结果04:"+from);

        // date 转 instant 对象方法(java.util.date)
        Instant instant1 = date.toInstant();
        System.out.println("结果05:"+instant1);

        // instant 根据毫秒值或者date转换为instant 类方法 (java.time)
        Instant instant2 = Instant.ofEpochMilli(date.getTime());
        System.out.println("结果06:"+instant2);

        // instant 根据秒值或者date转换为instant 类方法 (java.time)
        Instant instant3 = Instant.ofEpochSecond(60 * 60L);
        Instant instant4 = Instant.ofEpochSecond(60 * 60 * 24L);
        System.out.println("结果07:"+instant3);
        System.out.println("结果08:"+instant4);

        // instant 第一个参数指定秒数,第二个单位指定纳秒数,这样得到的instant会包含纳秒的数据 1000000000纳秒(9位)=1秒
        Instant instant5 = Instant.ofEpochSecond(60 * 60 * 24L,  1000000000*60L);
        System.out.println("结果09:"+instant5);

        // instant 将字符串类型的instant转换为instantd对象,如:1970-01-02T00:01:00Z,但是date的结果字符串不可以转换,会报错DateTimeParseException
        // 注意:必须传入的是符合 UTC格式的字符串
        Instant parse = Instant.parse("1970-01-01T01:00:00Z");
        System.out.println("结果10:"+parse+"parse");

        // instant 在现有的instant的时间上追加些时间,下面例子追加了5小时10分钟,这里plus会产生新的instant对象
        Instant plus = instant.plus(Duration.ofHours(5).plusMinutes(10));
        System.out.println("结果11:"+instant+" instant "+plus+"plus");
        System.out.println("结果12:"+(instant == plus));//plus会产生新的instant对象 所以结果位false

        // instant 获取其 5个小时前的 instant (此刻)
        Instant minus = instant.minus(5, ChronoUnit.HOURS);
        System.out.println("结果13:"+instant+" instant "+minus+" minus");

        // 也可以直接调用相关减法方法,效果跟上面的方法一样
        Instant instant6 = instant.minusSeconds(60 * 60 * 5);
        System.out.println("结果14:"+instant+" instant "+instant6+" instant6");

        // 减法方法,效果跟上面的方法一样
        Instant minus1 = instant.minus(Duration.ofHours(5));
        System.out.println("结果15:"+instant+" instant "+minus1+" minus1");

        // 计算两个 Instant 之间的秒数, ChronoUnit 用的什么,得到的结果就是什么单位
        long between = ChronoUnit.SECONDS.between(instant6, instant);
        System.out.println("结果17:"+between);

        // 或者使用 Duration 计算两个时间之间的耗时
        long millis = Duration.between(instant6, instant).toMillis();
        System.out.println("结果18:"+millis);

        // 比较两个 instant 相等 0, 前者时间纳秒值大于后者 1,小于后者 -1或小于0
        int i = instant.compareTo(instant6);
        System.out.println("结果19:"+i);

        // 判断 instant 时间前后,前者在后者之后返回 true,反之false
        boolean after = instant.isAfter(instant6);
        System.out.println("结果20:"+after);

        // 判断 instant 时间前后,前者在后者之前返回 true,反之false,正好与上面相反
        boolean before = instant.isBefore(instant6);
        System.out.println("结果21:"+before);
    }
}

运行结果:

2022-07-21T06:48:41.670Z
纪元秒 : 1658386121
时间戳 : 1658386121777
毫  秒 : 1658386121670
纳  秒 : 670000000
===========================
1970-01-01T00:00:01Z
纪元秒 : 1
毫  秒 : 1000
纳  秒 : 0
===========================
zonedDateTime = 2022-03-20T21:47:51+08:00[Asia/Shanghai]
===========================
localDateTime = 1970-01-01T08:00:01
===========================
结果012022-07-21T06:48:41.804Z
结果022022-07-21T14:48:41.804Z
结果03Thu Jul 21 14:48:41 CST 2022
结果04Thu Jul 21 22:48:41 CST 2022
结果052022-07-21T06:48:41.804Z
结果062022-07-21T06:48:41.804Z
结果071970-01-01T01:00:00Z
结果081970-01-02T00:00:00Z
结果091970-01-02T00:01:00Z
结果101970-01-01T01:00:00Zparse
结果112022-07-21T14:48:41.804Z instant 2022-07-21T19:58:41.804Zplus
结果12false
结果132022-07-21T14:48:41.804Z instant 2022-07-21T09:48:41.804Z minus
结果142022-07-21T14:48:41.804Z instant 2022-07-21T09:48:41.804Z instant6
结果152022-07-21T14:48:41.804Z instant 2022-07-21T09:48:41.804Z minus1
结果1718000
结果1818000000
结果191
结果20true
结果21false
......

Process finished with exit code 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

魔道不误砍柴功

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值