File size: 2,251 Bytes
0284885
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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、

    }
}