Instant類的getEpochSecond()方法用於從1970-01-01T00:00:00Z的Java紀元返回秒數。這意味著此實例返回的時間(以秒為單位)與“1970-01-01T00:00:00Z”表示的時間之間的差。紀元秒計數是秒的簡單遞增計數,其中秒0是1970-01-01T00:00:00Z。
用法:
public long getEpochSecond()
返回值:此方法返回從1970-01-01T00:00:00Z的紀元開始的秒數。
以下示例程序旨在說明Instant.getEpochSecond()方法:
示例1:
// Java program to demonstrate
// Instant.getEpochSecond() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create instance object
Instant instant
= Instant.parse("2018-10-20T16:55:30.00Z");
// print Instant Value
System.out.println("Instant: " + instant);
// get epochValue using getEpochSecond
long epochValue = instant.getEpochSecond();
// print results
System.out.println("Java epoch Value: "
+ epochValue);
}
}
輸出:
Instant: 2018-10-20T16:55:30Z Java epoch Value: 1540054530
示例2:
// Java program to demonstrate
// Instant.getEpochSecond() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create current instance object
Instant instant
= Instant.now();
// print Instant Value
System.out.println("Current Instant: "
+ instant);
// get epochValue using getEpochSecond
long epochValue = instant.getEpochSecond();
// print results
System.out.println("Java epoch Value: "
+ epochValue);
}
}
輸出:
Current Instant: 2018-11-22T08:26:19.502Z Java epoch Value: 1542875179
參考:https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#getEpochSecond()
相關用法
- Java Instant until()用法及代碼示例
- Java Instant now()用法及代碼示例
- Java Instant from()用法及代碼示例
- Java Instant get()用法及代碼示例
- Java Instant plus()用法及代碼示例
- Java Instant with()用法及代碼示例
- Java Instant isAfter()用法及代碼示例
- Java Instant isBefore()用法及代碼示例
- Java Instant query()用法及代碼示例
- Java Instant isSupported()用法及代碼示例
- Java Instant getLong()用法及代碼示例
- Java Instant toString()用法及代碼示例
- Java Instant getNano()用法及代碼示例
- Java Instant hashCode()用法及代碼示例
- Java Instant toEpochMilli()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Instant getEpochSecond() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。