java.util.Calendar getTime()
Description :
This java tutorial shows how to use the getTime() method of Calendar class of java.util package. This method returns a Date object representing this Calendar’s time value (millisecond offset from the Epoch”).
Method Syntax :
public final Date getTime()
Parameter Input :
DataType | Parameter | Description |
---|---|---|
N/A | N/A | N/A |
Method Returns :
This method returns a Date representing the time value.
Compatibility Version :
Requires Java 1.1 and up
Exception :
N/A
Discussion :
The Calender getTime() method is used to get the current time on our system.
Java Code Example :
This java example source code demonstrates the use of getTime() method of Calendar class. Basically this code just prints the current time. For demonstration purposes we have added a sleep time and then we instantiated again the calendar object to show that at the time of calling the getInstance of the Calendar class is the one being returned by the getTime method.
package com.javatutorialhq.java.examples;import java.util.Calendar;
/* * This example source code demonstrates the use of * getTime() method of Calendar class */
public class CalendarGetTimeExample {
public static void main(String
[] args) throws InterruptedException {
Calendar cal = Calendar.getInstance(); // Printing the current time System.out.println("Time 1:"+cal.getTime());
// sleeping for one sec Thread.sleep(2000);
// reinstantiate the calendar object cal = Calendar.getInstance();
// Printing the new time System.out.println("Time 2:"+cal.getTime());
}
}
Sample Output :
Running the getTime() method example source code of Calendar class will give you the following output:
Exception Scenario :
N/A
Similar Method :
- N/A
Suggested Reading List :
References :