Home  >  Core Java

Java GregorianCalendar Example

By Arvind Rai, September 28, 2021
On this page we will provide Java GregorianCalendar example.
1. The java.util.GregorianCalendar is the concrete subclass of java.util.Calendar class.
2. The GregorianCalendar provides the standard calendar system used by most of the countries.
3. The GregorianCalendar is a hybrid calendar that supports both the Julian and Gregorian calendar systems.

Let us discuss GregorianCalendar in detail.



Get Current Time

To get current time we can use following constructors.
GregorianCalendar()
GregorianCalendar(Locale aLocale)
GregorianCalendar(TimeZone zone)
GregorianCalendar(TimeZone zone, Locale locale) 
If TimeZone and Locale are not specified, their default values are considered.
Find the example.
GregorianCalendar gc1 = new GregorianCalendar();
System.out.println(gc1.getTime());
System.out.println("--Using Locale--");

GregorianCalendar gc2 = new GregorianCalendar(Locale.JAPAN);
System.out.println(gc2.getTime());

System.out.println("--Using TimeZone--");
TimeZone t = SimpleTimeZone.getTimeZone("Asia/Calcutta"); 
GregorianCalendar gc3 = new GregorianCalendar(t);
System.out.println(gc3.getTime());

System.out.println("--Using TimeZone and Locale--");    
GregorianCalendar gc4 = new GregorianCalendar(t, Locale.GERMAN);
System.out.println(gc4.getTime()); 

Add Time

void add(int field, int amount) 
Adds the specified signed amount of time to the given calendar field. If amount added is not in the range of specified field, then next larger field is incremented or decremented.
GregorianCalendar gc = new GregorianCalendar();
System.out.println(gc.getTime());    
gc.add(Calendar.MONTH, 2);
System.out.println(gc.getTime());
gc.add(Calendar.MONTH, -3);
System.out.println(gc.getTime());
gc.add(Calendar.HOUR, 5);
System.out.println(gc.getTime()); 
Output
Fri Sep 24 13:57:21 IST 2021
Wed Nov 24 13:57:21 IST 2021
Tue Aug 24 13:57:21 IST 2021
Tue Aug 24 18:57:21 IST 2021 

Convert to Date

We can convert GregorianCalendar to Date using Calendar.getTime() method. The GregorianCalendar is the subclass of Calendar and so getTime() is inherited to GregorianCalendar.
GregorianCalendar gc = new GregorianCalendar();
Date date = gc.getTime();
System.out.println(date); 
Output
Fri Sep 24 09:15:59 IST 2021 

roll Method

1.
public void roll(int field, boolean up) 
Adds or subtracts a single unit of time on the given time field without changing larger fields. If Boolean value is true, addition will take place. If Boolean value is false, subtraction will take place.
GregorianCalendar gc = new GregorianCalendar(2021, 8, 10);
System.out.println(gc.getTime());    
gc.roll(Calendar.MONTH, true);
System.out.println(gc.getTime());
gc.roll(Calendar.YEAR, false);
System.out.println(gc.getTime()); 
Output
Fri Sep 10 00:00:00 IST 2021
Sun Oct 10 00:00:00 IST 2021
Sat Oct 10 00:00:00 IST 2020 
2.
public void roll(int field, int amount) 
Adds a signed amount to the specified calendar field without changing larger fields.
GregorianCalendar gc = new GregorianCalendar(2021, 8, 10);
System.out.println(gc.getTime());    
gc.roll(Calendar.MONTH, 5);
System.out.println(gc.getTime()); 
Output
Fri Sep 10 00:00:00 IST 2021
Wed Feb 10 00:00:00 IST 2021 
We added 5 month to September. But it does not affect year.


Set Date

1.
void setWeekDate(int weekYear, int weekOfYear, int dayOfWeek) 
Sets this GregorianCalendar to the date given by the date specifiers.
weekYear: The week year.
weekOfYear: It follows the WEEK_OF_YEAR numbering i.e. 1 to 53.
dayOfWeek: It is DAY_OF_WEEK i.e. from SUNDAY to SATARDAY.
GregorianCalendar gc = new GregorianCalendar();
gc.setWeekDate(2020, 15, Calendar.MONDAY);
System.out.println(gc.getTime()); 
Output
Mon Apr 06 09:25:43 IST 2020 
2. Find the constructors that constructs the GregorianCalendar with given dates.
GregorianCalendar(int year, int month, int dayOfMonth)
GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute)
GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute, int second) 
Example:
GregorianCalendar c1 = new GregorianCalendar(2020, 8, 22); // 8 is for September
GregorianCalendar c2 = new GregorianCalendar(2020, 8, 22, 10, 7);
GregorianCalendar c3 = new GregorianCalendar(2020, 8, 22, 10, 7, 50); 

Create from ZonedDateTime

Find the static from method of GregorianCalendar.
static GregorianCalendar from(ZonedDateTime zdt) 
1. Obtains an instance of GregorianCalendar with the default locale from a ZonedDateTime object.
2. Since ZonedDateTime uses ISO calendar system and does not support a Julian-Gregorian cutover date, the instance of GregorianCalendar obtained from from method, is a pure Gregorian calendar and uses ISO 8601 standard for week definitions.
3. It has MONDAY as the FirstDayOfWeek and 4 as the value of the MinimalDaysInFirstWeek.
4. If the zoned date-time is too large to represent as a GregorianCalendar, it will throw IllegalArgumentException.
Example:
ZonedDateTime zdt = ZonedDateTime.now();
GregorianCalendar gc = GregorianCalendar.from(zdt);
System.out.println(gc.getTime()); 
Output
Thu Sep 23 18:17:20 IST 2021 

Convert to ZonedDateTime

Find the GregorianCalendar method.
ZonedDateTime toZonedDateTime() 
Converts this object to a ZonedDateTime that represents the same point on the time-line as this GregorianCalendar.
GregorianCalendar c = new GregorianCalendar(2021, 8, 26, 10, 7, 50);
ZonedDateTime zdt = c.toZonedDateTime();
System.out.println(zdt); 
Output
2021-09-26T10:07:50+05:30[Asia/Calcutta] 

Format Date

Convert GregorianCalendar into ZonedDateTime and then format it using DateTimeFormatter.
GregorianCalendar c = new GregorianCalendar(2021, 8, 26, 10, 7, 50);
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("dd MMMM, yyyy");	
String date1 = formatter1.format(c.toZonedDateTime());
System.out.println(date1);

DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss");	
String date2 = formatter2.format(c.toZonedDateTime());
System.out.println(date2); 
Output
26 September, 2021
2021-Sep-26 10:07:50 

Compare Times

Compare Gregorian calendars using Calendar.compareTo method.
The Calendar.compareTo returns 0, if both times are equal.
It returns less than 0, if the time of this Calendar is before the time represented by the argument.
It returns greater than 0, if the time of this Calendar is after the time represented by the argument.
GregorianCalendar c1 = new GregorianCalendar(2021, 8, 26, 10, 7, 50);
GregorianCalendar c2 = new GregorianCalendar(2020, 8, 26, 10, 7, 50);
System.out.println(c1.compareTo(c1)); // 0
System.out.println(c1.compareTo(c2)); // 1
System.out.println(c2.compareTo(c1)); // -1 

Convert to XMLGregorianCalendar

We can convert GregorianCalendar to XMLGregorianCalendar as following.
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
public class MyApp {
  public static void main(String args[]) throws DatatypeConfigurationException {
	GregorianCalendar gc = new GregorianCalendar(2021, 8, 26, 10, 7, 50);
	XMLGregorianCalendar xgc = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
	System.out.println(xgc);
 }
} 
Output
2021-09-26T10:07:50.000+05:30 

Gregorian Change Date

The default Gregorian change date is October 15, 1582. This is the point when the switch from Julian dates to Gregorian dates occurred. Previous to this, dates will be in the Julian calendar. We can set to different date to Gregorian change. Find the GregorianCalendar methods.
void setGregorianChange(Date date)
final Date getGregorianChange() 
Example:
String strDate="15/05/1560";  
Date date = new SimpleDateFormat("dd/MM/yyyy").parse(strDate);
GregorianCalendar gc = new GregorianCalendar();
gc.setGregorianChange(date);
System.out.println(gc.getGregorianChange()); 
Output
Wed May 15 00:00:00 IST 1560 

Get GregorianCalendar Informations

1.
int getActualMaximum(int field) 
Returns the maximum value that this calendar field could have.
GregorianCalendar gc = new GregorianCalendar(2021, 8, 26, 10, 7, 50);
System.out.println(gc.getActualMaximum(Calendar.MONTH)); // 11 
2.
int getActualMinimum(int field) 
Returns the minimum value that this calendar field could have.
GregorianCalendar gc = new GregorianCalendar(2021, 8, 26, 10, 7, 50);
System.out.println(gc.getActualMinimum(Calendar.MONTH)); // 0 
3.
int getGreatestMinimum(int field) 
Returns the highest minimum value for the given calendar field of this GregorianCalendar instance.
GregorianCalendar gc = new GregorianCalendar(2021, 8, 26, 10, 7, 50);
System.out.println(gc.getGreatestMinimum(Calendar.MONTH)); // 0 
4.
int getLeastMaximum(int field) 
Returns the lowest maximum value for the given calendar field of this GregorianCalendar instance.
GregorianCalendar gc = new GregorianCalendar(2021, 8, 26, 10, 7, 50);
System.out.println(gc.getLeastMaximum(Calendar.MONTH)); // 11 
5.
int getMaximum(int field) 
Returns the maximum value for the given calendar field of this GregorianCalendar instance.
GregorianCalendar gc = new GregorianCalendar(2021, 8, 26, 10, 7, 50);
System.out.println(gc.getMaximum(Calendar.MONTH)); // 11 
6.
int getMinimum(int field) 
Returns the minimum value for the given calendar field of this GregorianCalendar instance.
GregorianCalendar gc = new GregorianCalendar(2021, 8, 26, 10, 7, 50);
System.out.println(gc.getMinimum(Calendar.MONTH)); // 0 
7.
int getWeekYear() 
Returns the week year represented by this GregorianCalendar.
GregorianCalendar gc = new GregorianCalendar(2021, 8, 26, 10, 7, 50);
System.out.println(gc.getWeekYear()); // 2021 
8.
boolean isLeapYear(int year) 
Determines if the given year is a leap year.
GregorianCalendar gc = new GregorianCalendar(2021, 8, 26, 10, 7, 50);
System.out.println(gc.isLeapYear(2021)); // false 
9.
final boolean isWeekDateSupported() 
Returns true indicating this GregorianCalendar supports week dates.
GregorianCalendar gc = new GregorianCalendar(2021, 8, 26, 10, 7, 50);
System.out.println(gc.isWeekDateSupported()); // true 
10.
String getCalendarType() 
Returns "gregory" as the calendar type.
GregorianCalendar gc = new GregorianCalendar(2021, 8, 26, 10, 7, 50);
System.out.println(gc.getCalendarType()); // Gregory 
11.
static final int AD 
Value of the ERA field indicating the common era, also known as CE.
12.
static final int BC 
Value of the ERA field indicating the period before the common era, also known as BCE.

Reference

Class GregorianCalendar
POSTED BY
ARVIND RAI
ARVIND RAI











©2021 concretepage.com | Privacy Policy | Contact Us