Java Date - toInstant() Method
The java.util.Date.toInstant() method is used to convert this Date object to an Instant. The conversion creates an Instant that represents the same point on the time-line as this Date.
Syntax
public Instant toInstant()
Parameters
No parameter is required.
Return Value
Returns an instant representing the same point on the time-line as this Date object.
Exception
NA
Example:
In the example below, the java.util.Date.toInstant() method is used to convert the given Date object to an Instant.
import java.util.*; import java.time.Instant; public class MyClass { public static void main(String[] args) { //creating a Date Date Dt = new Date(); //printing the date object System.out.println("The Date is: " + Dt); //converting the date object into //an Instant object Instant Inst = Dt.toInstant(); //printing the instant object System.out.println("The Instant is: " + Inst); } }
The output of the above code will be:
The Date is: Sat May 08 11:03:24 UTC 2021 The Instant is: 2021-05-08T11:03:24.726Z
❮ Java.util - Date