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
===========================
结果01:2022-07-21T06:48:41.804Z
结果02:2022-07-21T14:48:41.804Z
结果03:Thu Jul 21 14:48:41 CST 2022
结果04:Thu Jul 21 22:48:41 CST 2022
结果05:2022-07-21T06:48:41.804Z
结果06:2022-07-21T06:48:41.804Z
结果07:1970-01-01T01:00:00Z
结果08:1970-01-02T00:00:00Z
结果09:1970-01-02T00:01:00Z
结果10:1970-01-01T01:00:00Zparse
结果11:2022-07-21T14:48:41.804Z instant 2022-07-21T19:58:41.804Zplus
结果12:false
结果13:2022-07-21T14:48:41.804Z instant 2022-07-21T09:48:41.804Z minus
结果14:2022-07-21T14:48:41.804Z instant 2022-07-21T09:48:41.804Z instant6
结果15:2022-07-21T14:48:41.804Z instant 2022-07-21T09:48:41.804Z minus1
结果17:18000
结果18:18000000
结果19:1
结果20:true
结果21:false
......
Process finished with exit code 0