Java program to print the current date-time using Calendar class

Learn how to print the current date-time using Calendar class in Java?
Submitted by Nidhi, on April 01, 2022

Problem Solution:

Create an instance of the Calendar class and get the current date-time using the getTime() method, and printed the result.

Program/Source Code:

The source code to print the current date-time using the Calendar class is given below. The given program is compiled and executed successfully.

// Java program to print current date-time 
// using Calendar class

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Calendar cal = Calendar.getInstance();

    System.out.println("The Current DateTime is: [" + cal.getTime() + "]");
  }
}

Output:

The Current DateTime is: [Fri Apr 01 06:58:17 GMT 2022]

Explanation:

In the above program, we created a class Main that contains a static method main(). The main() method is the entry point for the program, here we created the object of the Calendar class and get the current date-time using the getTime() method, and printed the result.

Java Date and Time Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.