Home »
        Java »
        Java Reference »
        Java Instant Class
    
    Java Instant Class | ofEpochMilli() Method with Example
    
    
       
    
	    Instant Class ofEpochMilli() method: Here, we are going to learn about the ofEpochMilli() method of Instant Class with its syntax and example.
	    
		    Submitted by Preeti Jain, on May 26, 2020
	    
    
    Instant Class ofEpochMilli() method
    
        - ofEpochMilli() method is available in java.time package.
 
        - ofEpochMilli() method is used to represent an instance of this Instant using the given milliseconds from the Java epoch standard format of 1970-01-01T00:00:00Z.
 
        - ofEpochMilli() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
 
        - ofEpochMilli() method may throw an exception at the time of representing milliseconds.
DateTimeException: This exception may throw when this Instant value reaches out of the min or max instant. 
    
Syntax:
    public static Instant ofEpochMilli(long milli_val);
    Parameter(s):
    
        - long milli_val – represents the number of milliseconds in value since 1970-01-01T00:00:00Z.
 
    
    
    Return value:
    The return type of this method is Instant, it returns the Instant that represents the given milliseconds.
    
    Example:
// Java program to demonstrate the example 
// of ofEpochMilli(long milli_val) method 
// of Instant
import java.time.*;
public class OfEpochMilliOfInstant {
    public static void main(String args[]) {
        long epoc_millis = 909990875;
        // Here, this method creates an Instant ins
        // by using the given epoch milliseconds
        // from the java epoch of 1970-01-01T00:00:00Z
        Instant ins = Instant.ofEpochMilli(epoc_millis);
        // Display ins
        System.out.println("Instant.ofEpochMilli(epoc_millis): " + ins);
        // Here, this method creates an Instant ins
        // by using the given epoch milliseconds
        // from the java epoch of 1970-01-01T00:00:00Z
        ins = Instant.ofEpochMilli(2000000);
        // Display ins
        System.out.println("Instant.ofEpochMilli(2000000): " + ins);
    }
}
Output
Instant.ofEpochMilli(epoc_millis): 1970-01-11T12:46:30.875Z
Instant.ofEpochMilli(2000000): 1970-01-01T00:33:20Z
    
    
  
    Advertisement
    
    
    
  
  
    Advertisement