Instant toEpochMilli() method in Java with Examples
Last Updated :
17 Feb, 2021
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
import java.time.*;
public class GFG {
public static void main(String[] args)
{
Instant instant
= Instant.parse( "2018-12-30T19:34:50.63Z" );
long value = instant.toEpochMilli();
System.out.println( "Millisecond value: "
+ value);
}
}
|
Program 2:
Java
import java.time.*;
public class GFG {
public static void main(String[] args)
{
Instant instant = Instant.now();
System.out.println( "Current Instant: "
+ instant);
long value = instant.toEpochMilli();
System.out.println( "Millisecond value: "
+ value);
}
}
|
References: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#toEpochMilli()
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...