w3resource

Java Date.toinstant()Method

public Instant toInstant()

The toinstant() method is used to convert Date object to an Instant. The conversion creates an Instant that represents the same point on the time-line as this Date.

Package: java.util

Java Platform: Java SE 8

Syntax:

toInstant()

Return Value:
an instant representing the same point on the time-line as this Date object.

Return Value Type:

Example: Java Date.toInstant() Method

import java.util.Date;
import java.time.Instant;

public class Main {
   public static void main(String[] args) {

      // create a date
       Date date = new Date(2012, 2, 2);

      // Converts the said Date object to an Instant.
       Instant i = date.toInstant();
       System.out.println(i);
   }
}
 

Output:

3912-03-02T00:00:00Z

Java Code Editor:

Previous:setTime Method
Next:toString Method



Follow us on Facebook and Twitter for latest update.