當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Instant toEpochMilli()用法及代碼示例


Instant類的toEpochMilli()方法用於將此瞬間轉換為從1970-01-01T00:00:00Z的紀元到long值的毫秒數。此方法返回該long值。

用法:

public long toEpochMilli()

返回值:此方法返回自1970-01-01T00:00:00Z的紀元以來的毫秒數。


異常:如果發生數字溢出,則此方法將引發ArithmeticException。

以下示例程序旨在說明Instant.toEpochMilli()方法:

示例1:

// Java program to demonstrate 
// Instant.toEpochMilli() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a Instant object 
        Instant instant 
            = Instant.parse("2018-12-30T19:34:50.63Z"); 
  
        // get milisecond value using toEpochMilli() 
        long value = instant.toEpochMilli(); 
  
        // print result 
        System.out.println("Milisecond value: "
                           + value); 
    } 
}
輸出:
Milisecond value: 1546198490630

示例2:

// Java program to demonstrate 
// Instant.toEpochMilli() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a Instant object 
        Instant instant = Instant.now(); 
  
        // current Instant 
        System.out.println("Current Instant: "
                           + instant); 
  
        // get milisecond value using toEpochMilli() 
        long value = instant.toEpochMilli(); 
  
        // print result 
        System.out.println("Milisecond value: "
                           + value); 
    } 
}
輸出:
Current Instant: 2018-11-27T04:46:36.368Z
Milisecond value: 1543293996368

參考:https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#toEpochMilli()



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Instant toEpochMilli() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。