Java GregorianCalendar Class
GregorianCalendar is a calendar class in Java that represents the Gregorian calendar, inheriting from the Calendar class. This class provides the standard calendar system used by most countries in the world.
The GregorianCalendar class is located in the java.util package and is used for date and time related operations. It supports date calculations from year 1 AD to the future, and takes into account complex calendar rules such as leap years.
Basic Usage of GregorianCalendar
Creating a GregorianCalendar Instance
There are several ways to create a GregorianCalendar instance:
Example
GregorianCalendar calendar1 = new GregorianCalendar();
// Create an instance with the specified year, month, and date
// Note: months start from 0, 0 represents January
GregorianCalendar calendar2 = new GregorianCalendar(2023, 10, 15);
// Create an instance with the specified year, month, date, hour, and minute
GregorianCalendar calendar3 = new GregorianCalendar(2023, 10, 15, 14, 30);
// Create an instance with the specified year, month, date, hour, minute, and second
GregorianCalendar calendar4 = new GregorianCalendar(2023, 10, 15, 14, 30, 45);
Getting Date and Time Information
Example
int year = calendar.get(Calendar.YEAR); // Get the year
int month = calendar.get(Calendar.MONTH); // Get the month (0-11)
int day = calendar.get(Calendar.DAY_OF_MONTH); // Get the date
int hour = calendar.get(Calendar.HOUR_OF_DAY); // Get the hour (24-hour format)
int minute = calendar.get(Calendar.MINUTE); // Get the minute
int second = calendar.get(Calendar.SECOND); // Get the second
Setting Date and Time
Example
// Set the year
calendar.set(Calendar.YEAR, 2024);
// Set the month (0-11)
calendar.set(Calendar.MONTH, Calendar.JANUARY);
// Set the date
calendar.set(Calendar.DAY_OF_MONTH, 1);
// Set year, month, and day simultaneously
calendar.set(2024, Calendar.JANUARY, 1);
Common Methods of GregorianCalendar
Date Calculation
Example
// Add 10 days
calendar.add(Calendar.DAY_OF_MONTH, 10);
// Subtract 2 months
calendar.add(Calendar.MONTH, -2);
Comparing Dates
Example
GregorianCalendar cal2 = new GregorianCalendar(2023, Calendar.DECEMBER, 25);
// Compare two dates
int result = cal1.compareTo(cal2); // Returns a negative number if cal1 is before cal2, a positive number if after, and 0 if equal
// Check if it is before a certain date
boolean isBefore = cal1.before(cal2);
// Check if it is after a certain date
boolean isAfter = cal1.after(cal2);
Checking for Leap Year
Example
boolean isLeapYear = new GregorianCalendar().isLeapYear(2024);
Converting to Date Object
Example
Date date = calendar.getTime();
Setting from Date Object
Example
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(date);
Date Formatting Output
GregorianCalendar itself does not provide formatting methods, but it can be used together with SimpleDateFormat:
Example
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = sdf.format(calendar.getTime());
System.out.println(formattedDate); // Output: 2023-11-15 14:30:00
Notes
Months Start from 0
In GregorianCalendar, months start from 0, where 0 represents January and 11 represents December. For better code readability, it is recommended to use the constants defined in the Calendar class:
Example
new GregorianCalendar(2023, 10, 15);
// Recommended
new GregorianCalendar(2023, Calendar.NOVEMBER, 15);
Thread Safety
GregorianCalendar is not thread-safe. If it needs to be used in a multi-threaded environment, synchronization should be performed or separate instances should be created for each thread.
Alternatives
In Java 8 and above, it is recommended to use the new date and time API (classes in the java.time package), such as LocalDate, LocalDateTime, etc. They have a more reasonable design and are more convenient to use.
Complete Example
Example
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class GregorianCalendarExample {
public static void main(String[] args) {
// Create an instance of the current date
GregorianCalendar calendar = new GregorianCalendar();
// Display the current date and time
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("Current time: " + sdf.format(calendar.getTime()));
// Check if this year is a leap year
int year = calendar.get(Calendar.YEAR);
boolean isLeap = calendar.isLeapYear(year);
System.out.println(year + "year " + (isLeap ? " is" : " is not") + "a leap year");
// Add 30 days
calendar.add(Calendar.DAY_OF_MONTH, 30);
System.out.println("30 days later: " + sdf.format(calendar.getTime()));
// Create an instance of a specific date
GregorianCalendar birthday = new GregorianCalendar(1990, Calendar.JULY, 15);
System.out.println("Birthday: " + sdf.format(birthday.getTime()));
}
}
The output result is:
当前时间: 2025-05-01 11:37:43 2025年不是闰年 30天后: 2025-05-31 11:37:43 生日: 1990-07-15 00:00:00
Method Summary
GregorianCalendar is a concrete implementation class of Calendar. The following are its commonly used methods:
Constructors
| Method | Description |
|---|---|
GregorianCalendar() |
Creates an object using the default time zone and locale |
GregorianCalendar(int year, int month, int dayOfMonth) |
Creates an object with the specified year, month, and day |
GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute) |
Creates an object with the specified year, month, day, hour, and minute |
GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute, int second) |
Creates an object with the specified year, month, day, hour, minute, and second |
GregorianCalendar(Locale locale) |
Creates an object using the default time zone and the specified locale |
GregorianCalendar(TimeZone zone) |
Creates an object using the specified time zone and the default locale |
GregorianCalendar(TimeZone zone, Locale locale) |
Creates an object using the specified time zone and locale |
Specific Methods
| Method | Description |
|---|---|
boolean isLeapYear(int year) |
Determines whether the specified year is a leap year |
void setGregorianChange(Date date) |
Sets the conversion date from the Julian calendar to the Gregorian calendar |
Date getGregorianChange() |
Gets the conversion date from the Julian calendar to the Gregorian calendar |
Common Methods Inherited from Calendar
| Method | Description |
|---|---|
int get(int field) |
Gets the value of the specified field |
void set(int field, int value) |
Sets the value of the specified field |
void add(int field, int amount) |
Adds or subtracts the specified amount of time |
void roll(int field, int amount) |
Adds or subtracts time without changing larger fields |
boolean after(Object when) |
Determines whether it is after the specified time |
boolean before(Object when) |
Determines whether it is before the specified time |
long getTimeInMillis() |
Returns the time value (milliseconds) |
void setTimeInMillis(long millis) |
Sets the Calendar with a millisecond time value |
Date getTime() |
Returns the corresponding Date object |
void setTime(Date date) |
Sets the time using a Date object |
Field Constants (Inherited from Calendar)
| Field | Description |
|---|---|
YEAR |
Year field |
MONTH |
Month field (0-11) |
DATE / DAY_OF_MONTH |
Day of the month |
DAY_OF_WEEK |
Day of the week (1-7, 1=Sunday) |
HOUR |
Hour in 12-hour format |
HOUR_OF_DAY |
Hour in 24-hour format |
MINUTE |
Minute |
SECOND |
second |
MILLISECOND |
Millisecond |
AM_PM |
AM or PM |