Java .getTime()
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 instanceCalendar calendar = Calendar.getInstance();// Get the current date objectDate currentDate = calendar.getTime();// Print the current dateSystem.out.println("Current date: " + currentDate);}}
The output should be similar to:
Current date: Sat Sep 02 13:10:27 GMT 2023
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours