Java Utility Library

Java Calendar - toInstant() Method



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

Syntax

public final Instant toInstant()

Parameters

No parameter is required.

Return Value

Returns an instant representing the same point on the time-line as this Calendar object.

Exception

NA

Example:

In the example below, the java.util.Calendar.toInstant() method is used to convert the given Calendar object to an Instant.

import java.util.*;
import java.time.Instant; 

public class MyClass {
  public static void main(String[] args) {
    //creating a Calendar
    Calendar Cal = new GregorianCalendar(98, 10, 25);

    //printing the calendar
    System.out.println("The Calendar is: " + Cal.getTime());
    
    //converting the calendar object into 
    //an Instant object
    Instant Inst = Cal.toInstant();

    //printing the instant object
    System.out.println("The Instant is: " + Inst);
  }
}

The output of the above code will be:

The Calendar is: Sun Nov 25 00:00:00 UTC 98
The Instant is: 0098-11-23T00:00:00Z

❮ Java.util - Calendar