Java Utility Library

Java Calendar - getTime() Method



The java.util.Calendar.getTime() method returns a Date object representing this Calendar's time value (millisecond offset from the Epoch).

Syntax

public Date getTime()

Parameters

No parameter is required.

Return Value

Returns a Date representing the time value.

Exception

NA

Example:

In the example below, the java.util.Calendar.getTime() method is used to create a Date object from a given Calendar instance.

import java.util.*;

public class MyClass {
  public static void main(String[] args) throws InterruptedException{
    //creating a Calendar instance
    Calendar Cal1 = Calendar.getInstance();

    //Creating a date object from calendar and printing it
    System.out.println("The Calendar is: " + Cal1.getTime());

    //Adding a delay of 2 seconds
    Thread.sleep(2000);

    //creating another Calendar instance
    Calendar Cal2 = Calendar.getInstance();

    //Creating a date object from calendar and printing it
    System.out.println("Next Calendar is: " + Cal2.getTime());    
  }
}

The output of the above code will be:

The Calendar is: Sat May 08 10:29:26 UTC 2021
Next Calendar is: Sat May 08 10:29:28 UTC 2021

❮ Java.util - Calendar