w3resource logo


Java Programming Exercies

Java DateTime, Calendar Exercises: Display information of a default calendar

Secondary Nav

Java DateTime, Calendar : Exercise-2 with Solution

Write a Java program to get and display information (year, month, day, hour, minute) of a default calendar.

Java Code:

import java.util.*;
public class Exercise2 {
 public static void main(String[] args)
    {
       // Create a default calendar
        Calendar cal = Calendar.getInstance();
       // Get and display information of current date from the calendar:
	    System.out.println();
        System.out.println("Year: " + cal.get(Calendar.YEAR));
        System.out.println("Month: " + cal.get(Calendar.MONTH));
        System.out.println("Day: " + cal.get(Calendar.DATE));
        System.out.println("Hour: " + cal.get(Calendar.HOUR));
        System.out.println("Minute: " + cal.get(Calendar.MINUTE));
	    System.out.println();
    }
}

Sample Output :

Year: 2016                                                              
Month: 8                                                              
Day: 15                                                              
Hour: 1                                                              
Minute: 16 

Java Practice online:

import java.util.*;
public class Exercise2 {
 public static void main(String[] args)
    {
       // Create a default calendar
        Calendar cal = Calendar.getInstance();
       // Get and display information of current date from the calendar:
	    System.out.println();
        System.out.println("Year: " + cal.get(Calendar.YEAR));
        System.out.println("Month: " + cal.get(Calendar.MONTH));
        System.out.println("Day: " + cal.get(Calendar.DATE));
        System.out.println("Hour: " + cal.get(Calendar.HOUR));
        System.out.println("Minute: " + cal.get(Calendar.MINUTE));
	    System.out.println();
    }
}



Join our Question Answer community to learn and share your programming knowledge.

Solve these problems:

Java: How to convert a string to an integer in Java?

C#: Loops in c#

SQL: JOIN using more than 5 tables