Date and Time

How to get Year Month Date from Calendar Object in Java

Table of Contents

Program

import java.util.Calendar;
import java.util.Date;

public class DateToCalendar
{
	public static void main(String[] args) 
	{
		Date date = new Date();	
		System.out.println("Java Date & Time:" + date);
		Calendar cal = Calendar.getInstance();
		System.out.println("Calendar Present Year : " + cal.get(Calendar.YEAR));
		System.out.println("Calendar Present Date : " + cal.get(Calendar.DATE));
		System.out.println("Calendar Month : " + cal.get(Calendar.MONTH));	
	}
}

Output

	Java Date & Time:Mon Apr 12 11:20:52 IST 2021
	Calendar Present Year : 2021
	Calendar Present Date : 12
	Calendar Month : 4

Description

public abstract class Calendar

Qextends Object
implements Serializable, Cloneable, Comparable
The Calendar class is an abstract class that provides methods for converting between a specific instant in
time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating
the calendar fields, such as getting the date of the next week. An instant in time can be represented by a
millisecond value that is an offset from the Epoch, January 1, 1970, 00:00:00.000 GMT (Gregorian).
The class also provides additional fields and methods for implementing a concrete calendar system outside the
package. Those fields and methods are defined as protected.
Like other locale-sensitive classes, Calendar provides a class method, getInstance, for getting a generally
useful object of this type. Calendar’s getInstance method returns a Calendar object whose calendar fields have
been initialized with the current date and time:
Calendar rightNow = Calendar.getInstance();
A Calendar object can produce all the calendar field values needed to implement the date-time formatting for a
particular language and calendar style (for example, Japanese-Gregorian, Japanese-Traditional). Calendar defines the
range of values returned by certain calendar fields, as well as their meaning. For example, the first month of the
calendar system has the value MONTH == JANUARY for all calendars. Other values are defined by the concrete subclass, such
as ERA. See individual field documentation and subclass documentation for details.