Instant ofEpochSecond() Method in Java with Examples - GeeksforGeeks

15 Oct, 2019
In Instant class, there are two types of ofEpochSecond() method depending upon the parameters passed to it.
ofEpochSecond() method of an Instant class used to return an instance of Instant using seconds passed as parameter to method calculated from the epoch of 1970-01-01T00:00:00Z.The nanosecond field is set to zero.
Syntax:
public static Instant ofEpochSecond(long epochSecond)

Parameters: This method accepts only one parameter epochSecond which is the number of seconds from 1970-01-01T00:00:00Z.
Return value: This method returns Instant using seconds passed as parameter.
Exception: This method throws following Exceptions DateTimeException if the instant exceeds the maximum or minimum instant.
Below programs illustrate the ofEpochSecond() method:Program 1:
// Java program to demonstrate// Instant.ofEpochSecond() method  import java.time.*;  public class GFG {    public static void main(String[] args)    {          // long epochsecond value        long epochSecond = 1200000l;          // apply ofEpochSecond method of Instant class        Instant result            = Instant.ofEpochSecond(epochSecond);          // print results        System.out.println("Instant: "                           + result);    }}
Instant: 1970-01-14T21:20:00Z

ofEpochSecond() method of an Instant class used to return an instance of Instant using seconds passed as parameter to method calculated from the epoch of 1970-01-01T00:00:00Z and nanoseconds fraction of second is also passed as parameter which will alter the values of the second and nanosecond in order to ensure that the stored nanosecond is in the range 0 to 999, 999, 999.
Syntax:
public static Instant ofEpochSecond(long epochSecond,
                                    long nanoAdjustment)

Parameters: This method accepts two parameters epochSecond which is the number of seconds from 1970-01-01T00:00:00Z and nanoAdjustment which is the nanosecond adjustment to the number of seconds, positive or negative.
Return value: This method returns Instant using seconds passed as parameter.
Exception: This method throws following Exceptions:
DateTimeException – if the instant exceeds the maximum or minimum instant.
ArithmeticException – if numeric overflow occurs.
Below programs illustrate the ofEpochSecond() method:Program 1:
// Java program to demonstrate// Instant.ofEpochSecond() method  import java.time.*;  public class GFG {    public static void main(String[] args)    {          // long epochsecond value and nanoadjustment value        long epochSecond = 1200000000l;        long nanoadjustment = 999999l;          // apply ofEpochSecond method of Instant class        Instant result            = Instant.ofEpochSecond(epochSecond,                                    nanoadjustment);          // print results        System.out.println("Instant: "                           + result);    }}
Instant: 2008-01-10T21:20:00.000999999Z

References:https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#ofEpochSecond(long, long)https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#ofEpochSecond(long)
Akanksha_Rai
java-basics
Java-Functions
Java-Instant
Java-time package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Generics in Java
Different ways of Reading a text file in Java
Internal Working of HashMap in Java
Introduction to Java
Comparator Interface in Java with Examples
Strings in Java