Java / API_project /Date_module /src /Instant_package /Instant_demo1.java
zxy0307's picture
Upload 2300 files
0284885 verified
Raw History Blame Contribute Delete
2.25 kB
package Instant_package;
import java.net.SocketTimeoutException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Instant_demo1 {
public static void main(String[] args) {
/*
static Instant now () 获取当前时间的Instant对象(标准时间)
static Instant ofXxxx (long epochMilli) 根据(秒/毫秒/纳秒)获取Instant对象
ZonedDateTime atZone (ZoneId zone) 指定时区
boolean isXxx (Instant otherInstant) 判断系列的方法
Instant minusXxx (long millisToSubtract) 减少时间系列的方法
Instant plusXxx (long millisToSubtract) 增加时间系列的方法
*/
//1、获取当前时间的Instant对象(标准时间)
Instant now = Instant.now();
System.out.println(now);
//2、根据(秒/毫秒/纳秒)获取Instant对象
Instant instant1 = Instant.ofEpochMilli(0L);
System.out.println(instant1);
Instant instant2 = Instant.ofEpochSecond(1L);
System.out.println(instant2);
Instant instant3 = Instant.ofEpochSecond(1L, 1000000000L);
System.out.println(instant3);
//3、指定时区
ZonedDateTime time = Instant.now().atZone(ZoneId.of("America/Cuiaba"));
System.out.println(time);
//4、isXxx 判断
Instant instant4 = Instant.ofEpochMilli(0L);
Instant instant5 = Instant.ofEpochMilli(1000L);
//isBefore:判断调用者代表的时间是否在参数表示时间的前面
boolean result1 = instant4.isBefore(instant5);
System.out.println(result1);//true
//isAfter:判断调用者代表的时间是否在参数表示时间的后面
boolean result2 = instant4.isAfter(instant5);
System.out.println(result2);//false
//5、Instant minusXxx (long millisToSubtract) 减少时间系列的方法
Instant instant6 = Instant.ofEpochMilli(324567234L);
System.out.println(instant6);//1970-01-04T18:09:27.234Z
Instant instant7 = instant6.minusSeconds(1);
System.out.println(instant7);//1970-01-04T18:09:26.234Z
//6、
}
}