Instant getEpochSecond() method in Java with Examples
The getEpochSecond() method of Instant class is used to return the number of seconds from the Java epoch of 1970-01-01T00:00:00Z. It means the difference between time in seconds of this instance and time represented by “1970-01-01T00:00:00Z” is returned by this method. The epoch second count is a simple incrementing count of seconds where the second 0 is 1970-01-01T00:00:00Z.
Syntax:
public long getEpochSecond()
Returns: This method returns the seconds from the epoch of 1970-01-01T00:00:00Z.
Below programs illustrate the Instant.getEpochSecond() method:
Program 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
Program 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
Reference: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#getEpochSecond()
Recommended Posts:
- Instant until() Method in Java with Examples
- Instant now() Method in Java with Examples
- Instant plus() method in Java with Examples
- Instant with() Method in Java with Examples
- Instant get() method in Java with Examples
- Instant from() method in Java with Examples
- Instant compareTo() method in Java with Examples
- Instant toString() method in Java with Examples
- Instant truncatedTo() method in Java with Examples
- Instant range() method in Java with Examples
- Instant atZone() method in Java with Examples
- Java 8 Clock instant() method with Examples
- Instant ofEpochMilli() method in Java with Examples
- Instant parse() method in Java with Examples
- Instant plusSeconds() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



