.getTime()

ChiragAgg5k's avatar
Published Sep 6, 2023
Contribute to Docs

The .getTime() method of the Calendar class returns a Date object that represents the time value of a given Calendar object.

Syntax

calendar.getTime()

Example

In the example below, a Calendar object is created and the current date is retrieved using the .getTime() method.

import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] args) {
// Create a Calendar instance
Calendar calendar = Calendar.getInstance();
// Get the current date object
Date currentDate = calendar.getTime();
// Print the current date
System.out.println("Current date: " + currentDate);
}
}

The output should be similar to:

Current date: Sat Sep 02 13:10:27 GMT 2023

All contributors

Contribute to Docs

Learn Java on Codecademy