Advanced JAVA - Commonly used APIs of ZoneId class, ZoneDateTime class and Instant class (7)

Table of contents

API        

        ​ ​ 1.0 Description of ZoneId class

        1.1 How to create an object of ZoneId class?

        ​ ​ 1.2 getAvailableZoneIds() static method in ZoneId class

        ​ ​ 2.0 ZoneDateTime class description

        ​ ​ 2.1 How to create an object of ZoneDateTime class?

        3.0 Description of Instant class

        3.1 How to create an object of Instant class?

         3.2 How to get timestamps in other time zones?

        3.3 getEpochSecond() instance method in Instant class

        ​ ​ 3.4 getNano() instance method in Instant class

        ​ ​ 3.5 equals(), isBefore(), isAfter() instance methods in the Instant class


API        

        ​ ​ 1.0 Description of ZoneId class

        The ZoneId class is a class introduced in Java 8 that represents an identifier for a time zone. It is an immutable class that can be used to create time zone objects, obtain time zone information, convert time zones and other operations.

        1.1 How to create an object of ZoneId class?

        You can obtain the current system default time zone through the systemDefault() static method, or through  of(String zoneId) Static method to get the specified time zone identifier to create a ZoneId object.

code show as below:

import java.time.ZoneId;

public class ZoneID {
    public static void main(String[] args) {

        //获取默认时区
        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println(zoneId);

        //获取指定时区
        ZoneId zoneId1 = ZoneId.of("Asia/Shanghai");
        System.out.println(zoneId1);

    }
}

The running results are as follows:

        1.2 ZoneId 类中的 getAvailableZoneIds() Quiet Method

        Get all available time zone identifiers.

code show as below:

import java.time.ZoneId;

public class ZoneID {
    public static void main(String[] args) {

        System.out.println(ZoneId.getAvailableZoneIds());
    }

}    

The running results are as follows:

        Of course there are many more, the list is not finished. According to the above time zone, the corresponding object can be created through the of() static method.

        2.0 ZoneDateTime Category Description

        The ZoneDateTime class is a class introduced in Java 8 and is a subclass of the LocalDateTime class , used to represent a date and time with time zone information. It contains date, time and time zone information, and provides a series of operation methods to handle date and time conversion, comparison and calculation.

        2.1 How to create an object of ZoneDateTime class?

        You can use the now() static method to create the current system default date and time object with time zone, or you can use < a i=3>now(ZoneId zone )  Static method creates a date and time object in the specified time zone.

code show as below:

import java.time.ZoneId;
import java.time.ZonedDateTime;

public class ZoneID {
    public static void main(String[] args) {

        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        System.out.println(zonedDateTime);


        ZoneId zoneId = ZoneId.of("America/Marigot");
        ZonedDateTime zonedDateTime1 = ZonedDateTime.now(zoneId);
        System.out.println(zonedDateTime1);
    }
}

The running results are as follows:

       

          The instance methods in the ZoneDateTime class are the same as those in the LocalDateTime class (introduced in the link below) The instance methods are roughly the same, so I won’t go into details here.

https://blog.csdn.net/Tingfeng__/article/details/133839673?spm=1001.2014.3001.5501

        3.0 Instant Category explanation

        The Instant class is a class introduced in Java 8 that represents a timestamp, i.e. from January 1, 1970 00:00:00 UTC (Coordinated Universal Time) The number of seconds to start. It is an immutable class that can be used to obtain the current timestamp, perform timestamp comparisons and calculations, etc.

        3.1 How to create an object of Instant class?

        The current timestamp can be created through the now() static method.

code show as below:

import java.time.Instant;

public class Text_Instant {
    public static void main(String[] args) {

        Instant istand = Instant.now();
        System.out.println(istand);

    }
}

The running results are as follows:

         3.2 How to get timestamps in other time zones?

code show as below:

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Text_Instant {
    public static void main(String[] args) {

        //先找到适合的需要的时区,创建ZoneId对象
        ZoneId zoneId = ZoneId.of("America/Port_of_Spain");
        //然后根据 now() 方法传入ZoneId对象来创建ZonedDateTime
        ZonedDateTime zonedDateTime = ZonedDateTime.now(zoneId);
        //再有ZonedDateTime类中实例方法 toInstant() 得到 Instant类型的对象。
        Instant instant = zonedDateTime.toInstant();

        System.out.println(instant);


    }
}

The running results are as follows:

        3.3 Instant 类中的 getEpochSecond() Example Method

        Get the seconds of the timestamp.

code show as below:

import java.time.Instant;

public class Text_Instant {
    public static void main(String[] args) {
        Instant instant = Instant.now();
        long a = instant.getEpochSecond();
        System.out.println(a);

    }
}

The running results are as follows:

        3.4 Instant 类中的 getNano() Example Method

        Gets the number of nanoseconds since the second from the start of the timeline.

code show as below:

import java.time.Instant;

public class Text_Instant {

    public static void main(String[] args) {
        Instant instant = Instant.now();
        int b = instant.getNano();
        System.out.println(b);
    }
}

The running results are as follows:

        3.5 Instant in terms equals(), isBefore(), isAfter() Example method

        The equals() method is used to determine whether two objects of the same type, such as seconds, milliseconds, and nanoseconds, are the same.

        The isBefore() method is used to determine whether two objects of the same type, such as seconds, milliseconds, and nanoseconds, are before the object that calls this method.

       The isAfter() method is used to determine whether two objects of the same type, such as seconds, milliseconds, and nanoseconds, are after the object that calls this method.

The code is as follows (taking the equals() method as an example):

import java.time.Instant;

public class Text_Instant {

    public static void main(String[] args) {
        //创建当前时间戳对象
        Instant instant1 = Instant.now();
        //对当前时间戳减1秒
        Instant instant2 = instant1.minusSeconds(1);
        //对当前时间戳加300毫秒
        Instant instant3 = instant1.plusMillis(300);
        //判断两个对象的时间戳是否相同
        System.out.println(instant2.equals(instant3));
    }
}

The running results are as follows:

        The answer is definitely different, they have all changed. Note that the original timestamp object has not changed, only new objects have been created. These objects are all immutable objects, and their contents cannot be changed.

         The API in this article requires a lot of practice, and you can master it after a wave of practical operations.



Guess you like

Origin blog.csdn.net/Tingfeng__/article/details/133842602