Friday, December 27, 2024
Google search engine
HomeLanguagesJavaInstant getEpochSecond() method in Java with Examples

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);
Ā Ā Ā Ā }
}


Output:

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);
Ā Ā Ā Ā }
}


Output:

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()

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?