📜  Java 8 Clock millis() 方法与示例

📅  最后修改于: 2022-05-13 01:55:37.601000             🧑  作者: Mango

Java 8 Clock millis() 方法与示例

Java Clock 类是Java的日期时间 API Java .time.Clock 的一部分。 Java日期时间 API 是从Java版本 8 添加的。
Clock 类的 millis() 方法以毫秒为单位返回时钟的当前时刻。从 1970-01-01T00:00Z (UTC) 到当前时间,以毫秒为单位进行测量。此方法与 System.currentTimeMillis() 方法的工作相同。如果对象的创建是不可接受的,则使用此方法允许在高性能用例中使用Java.time.Clock。
句法:

public long millis()

返回值:此方法以毫秒为单位返回时钟的当前时刻。
异常:如果无法从 Clock 获取瞬间,此方法将引发DateTimeException
示例::

Code:
Clock clock = Clock.systemDefaultZone();
long milliSeconds=clock.millis();
System.out.println(milliSeconds);

Output:: 
1534749202051

Explanation:: 
when millis() is called, then it returns a current instant
of Class Object in milliseconds. 

下面的程序说明了Java.time.Clock 类的 millis() 方法:
程序 1:使用 millis() 和使用 systemDefaultZone 创建的 Clock 对象

Java
// Java program to demonstrate millis()
// method of Clock class
 
import java.time.*;
 
// create class
public class millisMethodDemo {
 
    // Main method
    public static void main(String[] args)
    {
 
        // create Clock Object
        Clock clock = Clock.systemDefaultZone();
 
        // get Instant Object of Clock object
        // in milliseconds using millis() method
        long milliseconds = clock.millis();
 
        // print details of milliseconds variable
        System.out.println("Instant for class name "
                           + clock + " in milliseconds is "
                           + milliseconds);
    }
}


Java
// Java program to demonstrate millis()
// method of Clock class
 
import java.time.*;
 
// create class
public class millisMethodDemo {
 
    // Main method
    public static void main(String[] args)
    {
 
        // create a Zone Id for Europe/Paris
        ZoneId zoneId = ZoneId.of("Europe/Paris");
 
        // create Clock Object by passing zoneID
        Clock clock = Clock.system(zoneId);
 
        // get Instant Object of Clock object
        // in milliseconds using millis() method
        long milliseconds = clock.millis();
 
        // print details of milliseconds variable
        System.out.println("Instant for clock class"
                           + " in milliseconds is " + milliseconds);
    }
}


输出
Instant for class name SystemClock[Etc/UTC] in milliseconds is 1623838188802

程序 2:使用带有“欧洲/巴黎”区域创建的时钟对象的毫秒

Java

// Java program to demonstrate millis()
// method of Clock class
 
import java.time.*;
 
// create class
public class millisMethodDemo {
 
    // Main method
    public static void main(String[] args)
    {
 
        // create a Zone Id for Europe/Paris
        ZoneId zoneId = ZoneId.of("Europe/Paris");
 
        // create Clock Object by passing zoneID
        Clock clock = Clock.system(zoneId);
 
        // get Instant Object of Clock object
        // in milliseconds using millis() method
        long milliseconds = clock.millis();
 
        // print details of milliseconds variable
        System.out.println("Instant for clock class"
                           + " in milliseconds is " + milliseconds);
    }
}
输出
Instant for clock class in milliseconds is 1623837795533

参考: https: Java/time/Clock.html#millis–