Saturday, June 13, 2026
HomeLanguagesJavaInstant ofEpochMilli() method in Java with Examples

Instant ofEpochMilli() method in Java with Examples

The ofEpochMilli() method of Instant class helps to get an Instant using milliseconds passed as parameter from the epoch of 1970-01-01T00:00:00Z. This returned milliseconds is used to get the different time units like seconds, etc by conversion.

Syntax:

public static Instant 
    ofEpochMilli(long epochMilli)

Parameters: This method accepts one parameter epochMilli is value of milliseconds from 1970-01-01T00:00:00Z.

Returns: This method returns Instant as the time from the Epoch as milliseconds.

Exception: This method throws DateTimeException if the result exceeds the maximum or minimum instant.

Below programs illustrate the ofEpochMilli() method:

Program 1:




// Java program to demonstrate
// Instant.ofEpochMilli() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a long variable for milliseconds
        long milliseconds
            = 999999000;
  
        // get Instant using ofEpochMilli() method
        Instant instant
            = Instant.ofEpochMilli(milliseconds);
  
        // print result
        System.out.println("Instant: "
                           + instant);
    }
}


Output:

Instant: 1970-01-12T13:46:39Z

Program 2:




// Java program to demonstrate
// Instant.ofEpochMilli() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // get Instant using ofEpochMilli() method
        // passed epoch millisecond is 73264271044L
        Instant instant
            = Instant.ofEpochMilli(73264271044L);
  
        // print result
        System.out.println("Instant: "
                           + instant);
    }
}


Output:

Instant: 1972-04-27T23:11:11.044Z

References: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#ofEpochMilli(long)

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS