当前位置:网站首页>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
边栏推荐
- Use of for statement in Verilog
- Is it OK to directly compare the size of two numbers in FPGA?
- The difference between DDoS attack and CC attack
- 多线程并发下的指令重排问题
- Introduction to OSPF theory
- 循环神经网络RNN
- Understand the great changes of network security in five years
- How to judge whether a business is attacked by DDoS? What harm will it cause?
- IPv6表示方法与配置案例
- 偏向锁、轻量级锁测试工具类级相关命令
猜你喜欢
Neuralcf neural collaborative filtering network
centos 部署postgresql 13
CDM—码分复用(简单易懂)
VMware虚拟机在物理机win10系统下如何连接外网
OpenResty的核心与cosocket
SQL developer graphical window to create database (tablespace and user)
Hongke share | bring you a comprehensive understanding of "can bus error" (I) -- can bus error and error frame
Huawei switch ce12808 import and export configuration file
软件包设置成——>YUM源
SDN拓扑发现原理
随机推荐
数据单位:位、字节、字、字长
解决分频模块modelsim下仿真输出为stx的错误
JMM 内存模型概念
day03_1_流程控制
Hongke shares | why EtherCAT is the best solution to improve the performance of the control system?
How to judge whether a business is attacked by DDoS? What harm will it cause?
网络安全学习(一)
网络工具中的“瑞士军刀”-nc
Multiple IO usage
什么是DNS放大攻击
7、 Next generation Internet IPv6
Understand the great changes of network security in five years
Hongke shares | how to test and verify complex FPGA designs (1) -- entity or block oriented simulation
greenplum企业部署
Hongke share | let you have a comprehensive understanding of "can bus errors" (IV) -- producing and recording can errors in practice
C语言数据类型
FIR filter design (1) -- using the FDATool toolbox of MATLAB to design FIR filter parameters
Solution for website being suspended
Use of PDO
What if the 80443 port of the website server has been maliciously attacked?