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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 milisecond value using toEpochMilli()
        long value = instant.toEpochMilli();
  
        // print result
        System.out.println("Milisecond value: "
                           + value);
    }
}

chevron_right


Output:

Milisecond value: 1546198490630

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 milisecond value using toEpochMilli()
        long value = instant.toEpochMilli();
  
        // print result
        System.out.println("Milisecond value: "
                           + value);
    }
}

chevron_right


Output:

Current Instant: 2018-11-27T04:46:36.368Z
Milisecond value: 1543293996368

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



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.