当前位置:网站首页>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
边栏推荐
- 一文看懂网络安全五年之巨变
- AbstractQueuedSynchronizer(AQS) 之共享锁源码浅读
- Design and simulation code of 4-bit subtracter based on FPGA
- 网站受DDoS攻击的表现以及查看方法
- Floating point square root of vivado IP core floating point
- 10种常见的软件架构模式
- CNN-卷积神经网络
- 案例补充、ATM
- Is it OK to directly compare the size of two numbers in FPGA?
- FPGA - odd even frequency division and decimal frequency division code routine
猜你喜欢

6、 Network interconnection and Internet

Network Security Learning (II)

day03_ 1_ Process control

Using STP spanning tree protocol to solve the problem of two-layer loop in network

Understanding of access, hybrid and trunk modes

Joint modeling of price preference and interest preference in conversation recommendation - extensive reading of papers

After the EtherCAT master station is disconnected, how to ensure that the target system is not affected by the fault?

Hongke shares | how to test and verify complex FPGA designs (1) -- entity or block oriented simulation

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

CNN-卷积神经网络
随机推荐
比较单片机3种时钟电路方案
失效的访问控制
5G控制面协议之N2接口
Neuralcf neural collaborative filtering network
ping 原理
如何画出优秀的架构图
最新PyCharm2018破解教程
day12_ Multithreading
Enterprise manager cannot connect to the database instance in Oracle10g solution
Hongke share | let you have a comprehensive understanding of "can bus errors" (IV) -- producing and recording can errors in practice
Hongke shares | how to test and verify complex FPGA designs (1) -- entity or block oriented simulation
Instant 新日期类的使用 API
Hongke share | let you have a comprehensive understanding of "can bus error" (III) -- can node status and error counter
TCP based online dictionary
Network Security Learning (I)
【面试题】2022年最新软件测试面试题(400道)【附带答案】持续更新...
成长为架构师途中的一些思考
VMware虚拟机在物理机win10系统下如何连接外网
Hongke share | bring you a comprehensive understanding of "can bus error" (II) -- can error types
Computer right mouse click always turn around what's going on