Java Instant.ofEpochSecond() Method with Examples

Tags: Java Instant Java 8

In this Java core tutorial we learn how to use the java.time.Instant.ofEpochSecond() static method to create a new Instant object from an epoch second value in Java programming language.

Table of contents

  1. Create new Instant object from epoch seconds
  2. Create new Instant object from epoch seconds and nanos of second

Create new Instant object from epoch seconds

In Java we can use the Instant.ofEpochSecond(long epochSecond) static method to instantiate a new Instant object from an epoch second value as the example Java code below.

InstantOfEpochSecondExample1.java

import java.time.Instant;

public class InstantOfEpochSecondExample1 {
    public static void main(String... args) {
        long epochSeconds = 999999999;
        Instant instant = Instant.ofEpochSecond(epochSeconds);

        System.out.println("Epoch Seconds: " + epochSeconds);
        System.out.println("Instant: " + instant);
    }
}
The output as below.
Epoch Seconds: 999999999
Instant: 2001-09-09T01:46:39Z

Create new Instant object from epoch seconds and nanos of second

In Java we can use the Instant.ofEpochSecond(long epochSecond, long nanoAdjustment) static method to instantiate a new Instant object from an epoch second value and nanos of second value as the example Java code below.

InstantOfEpochSecondExample2.java

import java.time.Instant;

public class InstantOfEpochSecondExample2 {
    public static void main(String... args) {
        long epochSeconds = 999999999;
        long nanoAdjustment = 123456;
        Instant instant = Instant.ofEpochSecond(epochSeconds, nanoAdjustment);

        System.out.println("Epoch Seconds: " + epochSeconds);
        System.out.println("Nanosecond Adjustment: " + nanoAdjustment);
        System.out.println("Instant: " + instant);
    }
}
The output as below.
Epoch Seconds: 999999999
Nanosecond Adjustment: 123456
Instant: 2001-09-09T01:46:39.000123456Z

Happy Coding 😊

Java Instant.now() Method with Examples

Java Instant.ofEpochMilli() Method with Examples

Java Instant.parse() Method with Examples

Java Instant.getEpochSecond() Method with Examples

Java Instant.getNano() Method with Examples

Java Instant.plusSeconds() Method with Examples

Java Instant.plusMillis() Method with Examples

Java Instant.plusNanos() Method with Examples

Java Instant.minusSeconds() Method with Examples

Java Instant.minusMillis() Method with Examples

Java Instant.minusNanos() Method with Examples

Java Instant.atOffset() Method with Examples

Java Instant.atZone() Method with Examples

Java Instant.toEpochMilli() Method with Examples

Java Instant.compareTo() Method with Examples

Java Instant.isAfter() Method with Examples

Java Instant.isBefore() Method with Examples

Java Instant.equals() Method with Examples

Java Instant.toString() Method with Examples

Java Compare two Instant Values