Friday, September 5, 2025
HomeLanguagesJavaInstant toEpochMilli() method in Java with Examples

Instant toEpochMilli() method in Java with Examples

The toEpochMilli() method of an Instant class is used to convert this instant to the number of milliseconds from the epoch of 1970-01-01T00:00:00Z to a long value. This method returns that long value.
Syntax: 
 

public long toEpochMilli()

Returns: This method returns number of milliseconds since the epoch of 1970-01-01T00:00:00Z.
Exception: This method throws ArithmeticException if numeric overflow occurs.
Below programs illustrate the Instant.toEpochMilli() method:
Program 1: 
 

Java




// Java program to demonstrate
// Instant.toEpochMilli() method
 
import java.time.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // create a Instant object
        Instant instant
            = Instant.parse("2018-12-30T19:34:50.63Z");
 
        // get millisecond value using toEpochMilli()
        long value = instant.toEpochMilli();
 
        // print result
        System.out.println("Millisecond value: "
                           + value);
    }
}


Program 2: 
 

Java




// Java program to demonstrate
// Instant.toEpochMilli() method
 
import java.time.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // create a Instant object
        Instant instant = Instant.now();
 
        // current Instant
        System.out.println("Current Instant: "
                           + instant);
 
        // get millisecond value using toEpochMilli()
        long value = instant.toEpochMilli();
 
        // print result
        System.out.println("Millisecond value: "
                           + value);
    }
}


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

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

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6750 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS