Sunday, November 23, 2025
HomeLanguagesJavaCalendar getTime() Method in Java with Examples

Calendar getTime() Method in Java with Examples

The getTime() method in Calendar class is used to return an object resembling the Date that is represented by this Calendar’s time value.
Syntax: 
 

public final Date getTime()

Parameters: The method does not take any parameters.
Return Value: The method returns the Date, represented by this Calendar.
Below programs illustrate the working of getTime() Method of Calendar class: 
Example 1: 
 

Java




// Java code to illustrate
// getTime() method
 
import java.util.*;
 
public class Calendar_Demo {
 
    public static void main(String args[])
        throws InterruptedException
    {
 
        // Creating a calendar
        Calendar calndr1
            = Calendar.getInstance();
 
        // Displaying the current time
        System.out.println("The Current"
                           + " Time is: "
                           + calndr1.getTime());
 
        // Adding few delay
        Thread.sleep(10000);
 
        // Creating another calendar
        Calendar calndr2 = Calendar.getInstance();
 
        // Displaying the upcoming time
        Date dt = calndr2.getTime();
        System.out.println("The upcoming"
                           + " time is: " + dt);
    }
}


Output: 

The Current Time is: Wed Feb 20 14:40:37 UTC 2019
The upcoming time is: Wed Feb 20 14:40:47 UTC 2019

 

Example 2: 
 

Java




// Java code to illustrate
// getTime() method
 
import java.util.*;
 
public class Calendar_Demo {
 
    public static void main(String args[])
        throws InterruptedException
    {
 
        // Creating a calendar
        Calendar calndr1
            = Calendar.getInstance();
 
        // Displaying the current time
        System.out.println("The Current"
                           + " Time is: "
                           + calndr1.getTime());
 
        // Adding few delay
        Thread.sleep(5000);
 
        // Creating another calendar
        Calendar calndr2 = Calendar.getInstance();
 
        // Displaying the upcoming time
        Date dt = calndr2.getTime();
        System.out.println("The upcoming"
                           + " time is: " + dt);
    }
}


Output: 

The Current Time is: Wed Feb 20 14:40:50 UTC 2019
The upcoming time is: Wed Feb 20 14:40:55 UTC 2019

 

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#getMaximum-int-
 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32409 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6785 POSTS0 COMMENTS
Nicole Veronica
11932 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12000 POSTS0 COMMENTS
Shaida Kate Naidoo
6908 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6865 POSTS0 COMMENTS
Umr Jansen
6852 POSTS0 COMMENTS