当前位置:网站首页>API for using the new date class of instant
API for using the new date class of instant
2022-07-29 06:47:00 【The evil way does not miss the firewood cutting skill】
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. Get the... Of the current time Instant object
Instant now01 = Instant.now();
System.out.println(now01);
System.out.println(" Ji yuansec : "+now01.getEpochSecond());
System.out.println(" Time stamp : "+System.currentTimeMillis());
System.out.println(" Milli second : "+now01.toEpochMilli());
System.out.println(" Na second : "+now01.getNano());
System.out.println("===========================");
// 2. Get the specified time of Instant object
Instant instant01 = Instant.ofEpochSecond(1);
System.out.println(instant01);
System.out.println(" Ji yuansec : "+instant01.getEpochSecond());
System.out.println(" Milli second : "+instant01.toEpochMilli());
System.out.println(" Na second : "+instant01.getNano());
System.out.println("===========================");
//3. Specify timestamp creation Date time object with time zone 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. Specify timestamp creation The date time object of the default time zone 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 amount to date
Instant now = Instant.now();
// The timestamp obtained in this way is different from Beijing time 8 Time zone , It needs to be revised to Beijing time ,
// Discover by viewing source code Instant.now() Use, etc. yes UTC Time Clock.systemUTC().instant().LocalDate、LocalDateTime
// Of now() Method uses the system default time zone non-existent Instant.now() The question of time .
System.out.println(" result 01:"+now);
// Add 8h After that Instant Values are as follows
Instant instant = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8));
System.out.println(" result 02:"+instant);
Date date = new Date();
System.out.println(" result 03:"+date);
// instant turn date Class method (java.util.date)
Date from = Date.from(instant);
System.out.println(" result 04:"+from);
// date turn instant Object methods (java.util.date)
Instant instant1 = date.toInstant();
System.out.println(" result 05:"+instant1);
// instant According to the millisecond value or date Convert to instant Class method (java.time)
Instant instant2 = Instant.ofEpochMilli(date.getTime());
System.out.println(" result 06:"+instant2);
// instant According to the second value or date Convert to instant Class method (java.time)
Instant instant3 = Instant.ofEpochSecond(60 * 60L);
Instant instant4 = Instant.ofEpochSecond(60 * 60 * 24L);
System.out.println(" result 07:"+instant3);
System.out.println(" result 08:"+instant4);
// instant The first parameter specifies the number of seconds , The second unit specifies the number of nanoseconds , That's what we got instant It will contain nanosecond data 1000000000 nanosecond (9 position )=1 second
Instant instant5 = Instant.ofEpochSecond(60 * 60 * 24L, 1000000000*60L);
System.out.println(" result 09:"+instant5);
// instant The string type of instant Convert to instantd object , Such as :1970-01-02T00:01:00Z, however date The result string of cannot be converted , Will report a mistake DateTimeParseException
// Be careful : What must be passed in is the conformity UTC Format string
Instant parse = Instant.parse("1970-01-01T01:00:00Z");
System.out.println(" result 10:"+parse+"parse");
// instant In the existing instant Add some time to your time , The following example is added 5 Hours 10 minute , here plus There will be new instant object
Instant plus = instant.plus(Duration.ofHours(5).plusMinutes(10));
System.out.println(" result 11:"+instant+" instant "+plus+"plus");
System.out.println(" result 12:"+(instant == plus));//plus There will be new instant object So the result is false
// instant Get its 5 An hour ago instant ( At the moment )
Instant minus = instant.minus(5, ChronoUnit.HOURS);
System.out.println(" result 13:"+instant+" instant "+minus+" minus");
// You can also call the relevant subtraction method directly , The effect is the same as the above method
Instant instant6 = instant.minusSeconds(60 * 60 * 5);
System.out.println(" result 14:"+instant+" instant "+instant6+" instant6");
// Subtraction method , The effect is the same as the above method
Instant minus1 = instant.minus(Duration.ofHours(5));
System.out.println(" result 15:"+instant+" instant "+minus1+" minus1");
// Calculate two Instant The number of seconds between , ChronoUnit What do you use , The result is what unit
long between = ChronoUnit.SECONDS.between(instant6, instant);
System.out.println(" result 17:"+between);
// Or use Duration Calculate the time between two times
long millis = Duration.between(instant6, instant).toMillis();
System.out.println(" result 18:"+millis);
// Compare the two instant equal 0, The former time nanosecond value is greater than the latter 1, Less than the latter -1 Or less than 0
int i = instant.compareTo(instant6);
System.out.println(" result 19:"+i);
// Judge instant Around the time , The former returns after the latter true, conversely false
boolean after = instant.isAfter(instant6);
System.out.println(" result 20:"+after);
// Judge instant Around the time , The former returns before the latter true, conversely false, Just the opposite of the above
boolean before = instant.isBefore(instant6);
System.out.println(" result 21:"+before);
}
}
Running results :
2022-07-21T06:48:41.670Z
Ji yuansec : 1658386121
Time stamp : 1658386121777
Milli second : 1658386121670
Na second : 670000000
===========================
1970-01-01T00:00:01Z
Ji yuansec : 1
Milli second : 1000
Na second : 0
===========================
zonedDateTime = 2022-03-20T21:47:51+08:00[Asia/Shanghai]
===========================
localDateTime = 1970-01-01T08:00:01
===========================
result 01:2022-07-21T06:48:41.804Z
result 02:2022-07-21T14:48:41.804Z
result 03:Thu Jul 21 14:48:41 CST 2022
result 04:Thu Jul 21 22:48:41 CST 2022
result 05:2022-07-21T06:48:41.804Z
result 06:2022-07-21T06:48:41.804Z
result 07:1970-01-01T01:00:00Z
result 08:1970-01-02T00:00:00Z
result 09:1970-01-02T00:01:00Z
result 10:1970-01-01T01:00:00Zparse
result 11:2022-07-21T14:48:41.804Z instant 2022-07-21T19:58:41.804Zplus
result 12:false
result 13:2022-07-21T14:48:41.804Z instant 2022-07-21T09:48:41.804Z minus
result 14:2022-07-21T14:48:41.804Z instant 2022-07-21T09:48:41.804Z instant6
result 15:2022-07-21T14:48:41.804Z instant 2022-07-21T09:48:41.804Z minus1
result 17:18000
result 18:18000000
result 19:1
result 20:true
result 21:false
......
Process finished with exit code 0
边栏推荐
猜你喜欢

day12_ Multithreading

三、广域通信网

为什么5G N2接口控制面使用SCTP协议?

How to use SFTP command to access SFTP server on the development board

Hongke share | FPGA implementation of pass through and store and forward switching delay

Design of IIR filter based on FPGA

OpenResty的核心与cosocket

Network Security Learning (II)

Hongke shares | why EtherCAT is the best solution to improve the performance of the control system?

Hongke share | bring you a comprehensive understanding of "can bus error" (I) -- can bus error and error frame
随机推荐
Right value reference and mobile construction
软件定义边界SDP
Floating point multiplication and division of vivado IP core floating point
day02_基本语法
Common server faults and their solutions
Hongke share | FPGA implementation of pass through and store and forward switching delay
Hongke will share the EtherCAT demo for you and teach you how to quickly transition from other protocols to EtherCAT industrial bus
Tcp/ip 五层参考模型以及对应的典型设备以及ipv6
7、 Next generation Internet IPv6
Hongke automation SoftPLC | Hongke kPa modk operation environment and construction steps (3) -- modk routine test
Computer right mouse click always turn around what's going on
etcd原理
Hongke share | let you have a comprehensive understanding of "can bus error" (III) -- can node status and error counter
apisix健康检查测试
Floating point addition and subtraction method of vivado IP core floating point
PhantomReference 虚引用代码演示
一文看懂网络安全五年之巨变
Let the computer run only one program setting
数据单位:位、字节、字、字长
【面试题】2022年最新软件测试面试题(400道)【附带答案】持续更新...