GregorianCalendar is a concrete subclass of Calendar In Java - java.util.Calendar Class and provides the standard calendar system used by most of the world. The getInstance() method of Calendar returns a GregorianCalendar initialized with the current date and time in the default locale and time zone. GregorianCalendar defines two fields: AD and BC . These represent the two eras defined by the Gregorian calendar.
GregorianCalendar Constructors:
GregorianCalendar Methods:
Gregorian Calendar import java.util.Calendar; import java.util.GregorianCalendar;
class GregorianCalendarTest { public static void main(String arg[]) { GregorianCalendar gc = new GregorianCalendar(); System.out.println("The current date is : " + gc.getTime()); System.out.println("Month : " + gc.get(Calendar.MONTH)); System.out.println("Date : " + gc.get(Calendar.DATE)); System.out.println("Year : " + gc.get(Calendar.YEAR)); System.out.println("Hour : " + gc.get(Calendar.HOUR)); System.out.println("Hour of day : " + gc.get(Calendar.HOUR_OF_DAY)); System.out.println("Minute : " + gc.get(Calendar.MINUTE)); System.out.println("Second : " + gc.get(Calendar.SECOND)); System.out.println("Milli second : " + gc.get(Calendar.MILLISECOND)); System.out.println("Day of month : " + gc.get(Calendar.DAY_OF_MONTH)); System.out.println("Day of week : " + gc.get(Calendar.DAY_OF_WEEK)); System.out.println("Week of year : " + gc.get(Calendar.WEEK_OF_YEAR)); System.out.println("Week of month : " + gc.get(Calendar.WEEK_OF_MONTH)); } } OUTPUTThe current date is : Sun Jan 19 12:24:02 IST 2014 Month : 0 Date : 19 Year : 2014 Hour : 0 Hour of day : 12 Minute : 24 Second : 2 Milli second : 295 Day of month : 19 Day of week : 1 Week of year : 4 Week of month : 4 DESCRIPTIONIn this program, we are creating a date using GregorianCalendar and extracting individual month, date, year, hour, hour of the day, minute, second, milli second, day of month, day of week, week of year and week of month. Note the output will be different when you run this program.
Gregorian Calendar Methods import java.util.*;
class GregorianCalendarMethodsTest1 { public static void main(String arg[]) { GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance(); System.out.println("The current date : " + gc.getTime()); gc.add(Calendar.MONTH, 3); // Adds 3 months // LINE A System.out.println("Adds 3 months to present year : " + gc.getTime()); GregorianCalendar gc1 = (GregorianCalendar) gc.clone(); // Cloning method System.out.println("Clone date : " + gc1.getTime()); if (gc.equals(gc1)) { // Equals method System.out.println("Both calendars are same"); } System.out.println("The 2016 is Leap Year : " + gc.isLeapYear(2016)); // Leap year gc.roll(Calendar.DAY_OF_MONTH, true); // Rolls a single day System.out.println("Adds a single day : " + gc.getTime()); gc.roll(Calendar.DAY_OF_MONTH, 5); // Rolls 5 days System.out.println("Adds five days : " + gc.getTime()); gc.set(Calendar.MONTH, 11); // Sets month as December } } OUTPUTThe current date : Sun Jan 19 12:58:07 IST 2014 Adds 3 months to present year : Sat Apr 19 12:58:07 IST 2014 Clone date : Sat Apr 19 12:58:07 IST 2014 Both calendars are same The 2016 is Leap Year : true Adds a single day : Sun Apr 20 12:58:07 IST 2014 Adds five days : Fri Apr 25 12:58:07 IST 2014 DESCRIPTIONIn this program, The getInstance method takes the current date in gc object. The add, clone, equals, isLeapYear, and roll methods are used. THINGS TO TRY - At LINE A,
add method, replace -2 in place of 3 and see the output difference.
- In
isLeapYear method replace 2015 in place of 2016 and see the output difference.
- In
roll method replace false in place of true and see the output difference.
- In
roll method replace Calendar.MONTH in place of Calendar.DAY_OF_MONTH and see the output difference.
Gregorian Calendar Methods127 import java.util.*;
class GregorianCalendarMethodsTest2 { public static void main(String arg[]) { GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance(); System.out.println("The current date : " + gc.getTime()); int max = gc.getActualMaximum(Calendar.DAY_OF_MONTH); System.out.println("Actual Maximum : " + max); int min = gc.getActualMinimum(Calendar.DAY_OF_MONTH); System.out.println("Actual Minimum : " + min); min = gc.getGreatestMinimum(Calendar.DAY_OF_MONTH); System.out.println("Greatest Minimum : " + min); max = gc.getLeastMaximum(Calendar.DAY_OF_MONTH); System.out.println("Least Maximum : " + max); max = gc.getMaximum(Calendar.DAY_OF_MONTH); System.out.println("Maximum : " + max); min = gc.getMinimum(Calendar.DAY_OF_MONTH); System.out.println("Minimum : " + min); System.out.println("Time Zone : " + gc.getTimeZone().getDisplayName()); System.out.println("The hash code : " + gc.hashCode()); System.out.println("Get Gregorian change date : " + gc.getGregorianChange()); gc.setGregorianChange(new Date(2016, 7, 10, 05, 48, 32)); System.out.println("Gregorian Changed Date : " + gc.getGregorianChange()); } } OUTPUTThe current date : Sun Jan 19 13:11:52 IST 2014 Actual Maximum : 31 Actual Minimum : 1 Greatest Minimum : 1 Least Maximum : 28 Maximum : 31 Minimum : 1 Time Zone : India Standard Time The hash code : -1527345468 Get Gregorian change : Fri Oct 15 05:30:00 IST 1582 Gregorian Change Date : Thu Aug 10 05:48:32 IST 3916 DESCRIPTIONIn this program, the getInstance method takes the current date in gc object and getting the actual maximum, actual minimum, greatest minimum, least maximum, maximum, minimum day of month. We are also displaying time zone , hash code , getting gregorian change date and set gregorian change date. THINGS TO TRY - In
getActualMaximum , replace DAY_OF_MONTH by MONTH and see the output difference.
- In
getMaximum , replace DAY_OF_MONTH by DAY_OF_YEAR and see the output difference.
- In
setGregorianChange , give your own date and time and see the output.
|