Constructor | Description | GregorianCalendar() | Creates a GregorianCalendar object with current date, time etc. |
GregorianCalendar(int year, int month, int dayOfMonth) | Creates a GregorianCalendar object with values of year, month and dayOfMonth. |
---|---|
GregorianCalendar(int year, int month, int dayOfMonth, int hours, int minutes) | Creates a GregorianCalendar object with values of year, month and dayOfMonth, hours and minutes |
Methods | Description | static Calendar getInstance() | Returns an object of Calendar class with default locale and time zone. |
---|---|
int get(int calendarField) | Gets the specific information regarding the mentioned calendarField. |
Date getTime() | Returns a Date object converted from invoked Calendar object. |
void set(int calenderField, int value) | Sets a calendarField of Calendar object with a specified value |
void set(int year, int month, int dayOfMonth) | Sets a Calendar object with specified year, month, dayOfMonth |
boolean isLeapYear(int calenderYear) | Returns a true if calendarYear is a leap year. |
void setTime(Date d) | Sets the invoked Calendar object with the value of a Date object. |
void add(int calendarField, int value) | Adds a value to the specfic calendarField of Calendar object. |
import java.util.*;
class A
{
public static void main(String... ar)
{
GregorianCalendar g = new GregorianCalendar();
System.out.println(g);
//checking if the current year is a leap year or not
System.out.println("Is this a Leap Year? "+ g1.isLeapYear(Calendar.YEAR));
}
}
Output
java.util.GregorianCalendar[time=1500973726436,areFieldsSet=true,areAllFieldsSet
=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=198
00000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek
=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2017,MONTH=6,WEEK_OF_YEAR=30,WEEK_OF_MONT
H=5,DAY_OF_MONTH=25,DAY_OF_YEAR=206,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1
,HOUR=2,HOUR_OF_DAY=14,MINUTE=38,SECOND=46,MILLISECOND=436,ZONE_OFFSET=19800000,
DST_OFFSET=0]
Is this a Leap Year? false
In the preceding code, we have created an object of GregorianCalendar class by calling its constructor that takes nothing.
By printing this object we have been presented
with a lot of information about our timezone, date, month, year, hour, minute, second, day of the month, day of the year, day of the week etc, which is a little hard to read.
Let's extract each of these specific information individually in a more presentable format.
import java.util.*;
class A
{
public static void main(String... ar)
{
GregorianCalendar g = new GregorianCalendar();
System.out.println(g);
System.out.println("Day of the year : "+g.get(Calendar.DAY_OF_YEAR));
System.out.println("Day of the month : "+ g.get(Calendar.DAY_OF_MONTH));
System.out.println("Day of the week : " +g.get(Calendar.DAY_OF_WEEK));
//Getting Hour and minutes of the day
System.out.println("Getting Hour, minutes, AM/PM of the day");
System.out.println("Hour of the day : "+g.get(Calendar.HOUR));
System.out.println("Minute : "+g.get(Calendar.MINUTE));
System.out.println("AM or PM? : "+g.get(Calendar.AM_PM));
//Getting current date, month and year
System.out.println("Getting the current date, month and year");
System.out.println("Date : "+g.get(Calendar.DATE));
System.out.println("Month : "+g.get(Calendar.MONTH));
System.out.println("Year: "+g.get(Calendar.YEAR));
//Getting the extra information about the week
System.out.println("Getting the extra information about the week");
System.out.println("Week of the month : "+g.get(Calendar.WEEK_OF_MONTH));
System.out.println("Week of the year : "+g.get(Calendar.WEEK_OF_YEAR));
}
}
Output -
Day of the year : 206
Day of the month : 25
Day of the week : 3
Getting Hour, minutes, AM/PM of the day
Hour of the day : 6
Minute : 0
AM or PM? : 1
Getting the current date, month and year
Date : 25
Month : 6
Year: 2017
Getting the extra information about the week
Week of the month : 5
Week of the year : 30
In the last code, we have extracted a more presentable and less cluttered specific piece information(one by one) about our day and time etc using the GregorianCalendar object.
import java.util.*;
class A
{
public static void main(String... ar)
{
GregorianCalendar g = new GregorianCalendar();
//Getting modified current date, month and year
System.out.println("Getting the current date, month and year");
System.out.println("Date : "+g.get(Calendar.DATE)); //calling get(int calendarField) method
System.out.println("Month : "+g.get(Calendar.MONTH));
System.out.println("Year: "+g.get(Calendar.YEAR));
//Setting the date in a Calendar object
g.set(Calendar.DATE,10); //calling set(int calendarField, int value) method
g.set(Calendar.MONTH,2);
g.set(Calendar.YEAR,5);
//Getting modified current date, month and year
System.out.println("Getting the modified current date, month and year");
System.out.println("Modified Date : "+g.get(Calendar.DATE));
System.out.println("Modified Month : "+g.get(Calendar.MONTH));
System.out.println("Modified Year: "+g.get(Calendar.YEAR));
//Getting the hour, minutes and seconds of the day
System.out.println("Getting the hour, minutes, second of the day");
System.out.println("Hour of the day : "+g.get(Calendar.HOUR));
System.out.println("Minute : "+g.get(Calendar.MINUTE));
System.out.println("Second : "+g.get(Calendar.SECOND));
//Setting the hour, minutes and seconds of the day
g.set(Calendar.HOUR,7);
g.set(Calendar.MINUTE,50);
g.set(Calendar.SECOND,23);
//Getting the modified hour, minutes and seconds of the day
System.out.println("Getting Modified hour, minutes and second of the day");
System.out.println("Modified Hour of the day : "+g.get(Calendar.HOUR));
System.out.println("Modified Minute : "+g.get(Calendar.MINUTE));
System.out.println("Modified Second : "+g.get(Calendar.SECOND));
}
}
Output -
Getting the current date, month and year
Date : 25
Month : 6
Year: 2017
Getting the modified current date, month and year
Modified Date : 10
Modified Month : 2
Modified Year: 5
Getting the hour, minutes and second of the day
Hour of the day : 2
Minute : 43
Second : 15
Getting modified hour, minutes and second of the day
Modified Hour of the day : 7
Modified Minute : 50
Modified Second : 23
We have twice modified the current date, month and year contained in a GregorianCalender object, by-
import java.util.*;
class A
{
public static void main(String... ar)
{
GregorianCalendar g = new GregorianCalendar();
//Getting current date, month and year
System.out.println("Getting the current date, month and year");
System.out.println("Date : "+g.get(Calendar.DATE));
System.out.println("Month : "+g.get(Calendar.MONTH));
System.out.println("Year: "+g.get(Calendar.YEAR));
//Another way to set the date in a Calendar object
g.add(Calendar.DATE,4); //adding 4 days to the current date.
g.add(Calendar.MONTH,-2); //subtracting 2 days from the current month.
g.add(Calendar.YEAR,-5); //subtracting 5 years from the current year.
//Getting current date, month and year
System.out.println("Getting the modified current date, month and year");
System.out.println("Modified Date : "+g.get(Calendar.DATE));
System.out.println("Modified Month : "+g.get(Calendar.MONTH));
System.out.println("Modified Year: "+g.get(Calendar.YEAR));
}
}
Output-
Getting the current date, month and year
Date : 25
Month : 6
Year: 2017
Getting the modified current date, month and year
Modified Date : 29
Modified Month : 4
Modified Year: 2012
import java.util.*;
class A
{
public static void main(String... ar)
{
Date d= new Date(1000000000000l);
System.out.println("Date object : "+ d);
GregorianCalendar g = new GregorianCalendar();
g.setTime(d);
System.out.println("GregorianCalendar object :");
System.out.println("DATE : "+ g.get(Calendar.DATE));
System.out.println("MONTH : "+g.get(Calendar.MONTH));
System.out.println("YEAR : "+ g.get(Calendar.YEAR));
System.out.println("HOUR : "+ g.get(Calendar.HOUR));
System.out.println("MINUTE : "+ g.get(Calendar.MINUTE));
System.out.println("SECOND : "+ g.get(Calendar.SECOND));
}
}
Output -
Date object : Sun Sep 09 07:16:40 IST 2001
GregorianCalendar Object:
DATE : 9
MONTH : 8
YEAR : 2001
HOUR : 7
MINUTE : 16
SECOND : 40
Coming Next
-
JSP & Servlets