Wednesday, September 25, 2024
Google search engine
HomeLanguagesJavaInstant ofEpochSecond() Method in Java with Examples

Instant ofEpochSecond() Method in Java with Examples

In Instant class, there are two types of ofEpochSecond() method depending upon the parameters passed to it.

ofEpochSecond(long epochSecond)

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


Output:

Instant: 1970-01-14T21:20:00Z

ofEpochSecond(long epochSecond, long nanoAdjustment)

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


Output:

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)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments