Java Instant Class | ofEpochSecond() Method with Example

Instant Class ofEpochSecond() method: Here, we are going to learn about the ofEpochSecond() method of Instant Class with its syntax and example.
Submitted by Preeti Jain, on May 28, 2020

Instant Class ofEpochSecond() method

Syntax:

    public static Instant ofEpochSecond(long sec_val);
    public static Instant ofEpochSecond(long sec_val, long nanos_adjust);
  • ofEpochSecond() method is available in java.time package.
  • ofEpochSecond(long sec_val) method is used to represent an instance of this Instant by using the given seconds from the java epoch standard format since 1970-01-01T00:00:00Z.
  • ofEpochSecond(long sec_val, long nanos_adjust) method is used to represent an instance of this Instant by using the given seconds and nano fraction of seconds from the java epoch of 1970-01-01T00:00:00Z.
  • These methods may throw an exception at the time of representing seconds in epoch format.
    DateTimeException: This exception may throw when this Instant value reaches out of the min or max instant.
  • These are static methods and it is accessible with class name and if we try to access these methods with class object then we will not get an error.

Parameter(s):

  • In the first case, "ofEpochSecond(long sec_val)",
    • long sec_val – represents the number of seconds in value since 1970-01-01T00:00:00Z.
  • In the second case, "ofEpochSecond(long sec_val, long nanos_adjust)",
    • long sec_val – Similar as defined in the first case.
    • long nanos_adjust – represents the adjustment when the second reaches out of range.

Return value:

In both the cases, the return type of the method is Instant,

  • In the first cases, it returns the Instant that represent the given seconds.
  • In the second cases, it returns the Instant that represent the given seconds and nano fraction of seconds.

Example:

// Java program to demonstrate the example 
// of ofEpochSecond() method of Instant

import java.time.*;
import java.time.temporal.*;

public class OfEpochSecondOfInstant {
    public static void main(String args[]) {
        long epoch_sec = 25;
        long nanos_adjust = 25000;

        // Instantiates two Instant 
        Instant ins1 = Instant.parse("2006-04-03T05:10:15.00Z");
        Instant ins2 = Instant.now();

        // Display ins1,ins2
        System.out.println("Instant ins1 and ins2: ");
        System.out.println("ins1: " + ins1);
        System.out.println("ins2: " + ins2);

        System.out.println();

        // Here, this method represents the given second
        // from the java epoch of 1970-01-01T00:00:00Z
        Instant of_epoch_sec = ins1.ofEpochSecond(epoch_sec);

        // Display of_epoch_sec
        System.out.println("ins1.ofEpochSecond(epoch_sec): " + of_epoch_sec);

        // Here, this method represents the instant by using
        // seconds and nanoseconds from the java epoch
        // of 1970-01-01T00:00:00Z
        of_epoch_sec = ins2.ofEpochSecond(epoch_sec, nanos_adjust);

        // Display of_epoch_sec
        System.out.println("ins2.ofEpochSecond(epoch_sec,nanos_adjust): " + of_epoch_sec);
    }
}

Output

Instant ins1 and ins2: 
ins1: 2006-04-03T05:10:15Z
ins2: 2020-05-28T07:12:35.393208Z

ins1.ofEpochSecond(epoch_sec): 1970-01-01T00:00:25Z
ins2.ofEpochSecond(epoch_sec,nanos_adjust): 1970-01-01T00:00:25.000025Z



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.