The toInstant() method of Date class in Java is used to convert a Date object to an Instant object. An Instant is created during the conversion which is used to represent the same point on the timeline as this Date.
Syntax:
public Instant toInstant()
Parameters: The method does not take any parameters.
Return Value: The method returns an Instant representing the same point on the timeline as this Date.
Below programs illustrate the use of toInstant() method in Java:
Example 1:
// Java code to demonstrate // toInstant() method of Date class import java.util.Date; import java.util.Calendar; import java.time.Instant; public class GfG { public static void main(String args[]) { // Creating a Calendar object Calendar c1 = Calendar.getInstance(); // Set Month // MONTH starts with 0 i.e. ( 0 - Jan) c1.set(Calendar.MONTH, 00); // Set Date c1.set(Calendar.DATE, 30); // Set Year c1.set(Calendar.YEAR, 2019); // Creating a date object // with specified time. Date dateOne = c1.getTime(); Instant inst = dateOne.toInstant(); System.out.println( "Original Date: " + dateOne.toString()); System.out.println( "Instant: " + inst); } } |
Original Date: Wed Jan 30 06:01:46 UTC 2019 Instant: 2019-01-30T06:01:46.730Z
Example 2:
// Java code to demonstrate // clone() method of Date class import java.util.Date; import java.util.Calendar; import java.time.Instant; public class GfG { public static void main(String args[]) { // Creating a Calendar object Calendar c1 = Calendar.getInstance(); // Set Month // MONTH starts with 0 i.e. ( 0 - Jan) c1.set(Calendar.MONTH, 06); // Set Date c1.set(Calendar.DATE, 12); // Set Year c1.set(Calendar.YEAR, 1996); // Creating a date object // with specified time. Date dateOne = c1.getTime(); Instant inst = dateOne.toInstant(); System.out.println( "Original Date: " + dateOne.toString()); System.out.println( "Instant: " + inst); } } |
Original Date: Fri Jul 12 06:01:49 UTC 1996 Instant: 1996-07-12T06:01:49.766Z
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#toInstant–
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- ChronoZonedDateTime toInstant() method in Java with Examples
- ChronoLocalDateTime toInstant() method in Java with Examples
- Date before() method in Java with Examples
- Date compareTo() method in Java with examples
- Date equals() method in Java with Examples
- Date toString() method in Java with Examples
- Date hashCode() method in Java with Examples
- Date setTime() method in Java with Examples
- Date getTime() method in Java with Examples
- Date clone() method in Java with Examples
- Date from() method in Java with Examples
- LocalDateTime of(date, time) method in Java with Examples
- Date class in Java (With Examples)
- util.date class methods in Java with Examples
- Date after() method in Java
- DateFormat format(Date obj) method in Java
- HijrahChronology date() method in Java with Example : Set - 1
- HijrahChronology date() method in Java with Example : Set - 2
- IsoChronology date(int, int, int) method in Java with Example
- IsoChronology date(TemporalAccessor) method in Java with Example
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.

