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 paramters.
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 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); } } |
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 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); } } |
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-
Recommended Posts:
- Date getTime() method in Java with Examples
- Calendar add() Method in Java with Examples
- Calendar get() method in Java with Examples
- Calendar set() Method in Java with Examples
- Calendar getMinimum() Method in Java with Examples
- Calendar getMinimalDaysInFirstWeek() Method in Java with Examples
- Calendar computeFields() method in Java with Examples
- Calendar computeTime() method in Java with Examples
- Calendar isLenient() Method in Java with Examples
- Calendar getTimeInMillis() Method in Java with Examples
- Calendar toString() Method in Java with Examples
- Calendar complete() Method in Java with Examples
- Calendar compareTo() Method in Java with Examples
- Calendar getActualMinimum() Method in Java with Examples
- Calendar getFirstDayOfWeek() Method in Java with Examples
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.



