Java Gregoriancalendar Class
GregorianCalendar is a calendar class in Java that represents the Gregorian calendar. It extends the Calendar class. This class provides the standard calendar system, which is also used by most countries in the world.
The GregorianCalendar class is 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 considers complex calendar rules such as leap years.
* * *
## Basic Usage of GregorianCalendar
### Creating GregorianCalendar Instances
There are multiple ways to create a GregorianCalendar instance:
## Example
// Create an instance representing the current date and time
GregorianCalendar calendar1 =new GregorianCalendar();
// Create an instance with specified year, month, and day
// Note: months start from 0, 0 represents January
GregorianCalendar calendar2 =new GregorianCalendar(2023, 10, 15);
// Create an instance with specified year, month, day, hour, and minute
GregorianCalendar calendar3 =new GregorianCalendar(2023, 10, 15, 14, 30);
// Create an instance with specified year, month, day, hour, minute, and second
GregorianCalendar calendar4 =new GregorianCalendar(2023, 10, 15, 14, 30, 45);
### Getting Date and Time Information
## Example
GregorianCalendar calendar =new GregorianCalendar();
int year = calendar.get(Calendar.YEAR);// Get year
int month = calendar.get(Calendar.MONTH);// Get month (0-11)
int day = calendar.get(Calendar.DAY_OF_MONTH);// Get day
int hour = calendar.get(Calendar.HOUR_OF_DAY);// Get hour (24-hour format)
int minute = calendar.get(Calendar.MINUTE);// Get minute
int second = calendar.get(Calendar.SECOND);// Get second
### Setting Date and Time
## Example
GregorianCalendar calendar =new GregorianCalendar();
// Set year
calendar.set(Calendar.YEAR, 2024);
// Set month (0-11)
calendar.set(Calendar.MONTH, Calendar.JANUARY);
// Set day
calendar.set(Calendar.DAY_OF_MONTH, 1);
// Set year, month, and day at the same time
calendar.set(2024, Calendar.JANUARY, 1);
* * *
## Common Methods of GregorianCalendar
### Date Calculation
## Example
GregorianCalendar calendar =new GregorianCalendar(2023, Calendar.NOVEMBER, 15);
// Add 10 days
calendar.add(Calendar.DAY_OF_MONTH, 10);
// Subtract 2 months
calendar.add(Calendar.MONTH, -2);
### Comparing Dates
## Example
GregorianCalendar cal1 =new GregorianCalendar(2023, Calendar.NOVEMBER, 15);
GregorianCalendar cal2 =new GregorianCalendar(2023, Calendar.DECEMBER, 25);
// Compare two dates
int result = cal1.compareTo(cal2);// Returns negative if cal1 is before cal2, positive if after, 0 if equal
// Check if before a certain date
boolean isBefore = cal1.before(cal2);
// Check if after a certain date
boolean isAfter = cal1.after(cal2);
### Determining Leap Years
## Example
// Determine if a specified year is a leap year
boolean isLeapYear =new GregorianCalendar().isLeapYear(2024);
### Converting to Date Object
## Example
GregorianCalendar calendar =new GregorianCalendar();
Date date = calendar.getTime();
### Setting from Date Object
## Example
Date date =new Date();
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
GregorianCalendar calendar =new GregorianCalendar(2023, Calendar.NOVEMBER, 15, 14, 30);
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
// Not recommended
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 a separate instance should be created for each thread.
### Alternative
For 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., which are better designed and easier to use.
* * *
## Complete Example
## Example
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class GregorianCalendarExample {
public static void main(String[] args){
// Create an instance for the current date
GregorianCalendar calendar =new GregorianCalendar();
// Display 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("After 30 days: "+ sdf.format(calendar.getTime()));
// Create an instance for a specific date
GregorianCalendar birthday =new GregorianCalendar(1990, Calendar.JULY, 15);
System.out.println("Birthday: "+ sdf.format(birthday.getTime()));
}
}
Output:
Current time: 2025-05-01 11:37:432025 year is not a leap yearAfter 30 days: 2025-05-31 11:37:43Birthday: 1990-07-15 00:00:00
* * *
## Method Summary
GregorianCalendar is a concrete implementation class of Calendar. Here are its commonly used methods:
### Constructors
| Method | Description |
| --- | --- |
| `GregorianCalendar()` | Creates an object with default time zone and locale |
| `GregorianCalendar(int year, int month, int dayOfMonth)` | Creates an object with specified year, month, and day |
| `GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute)` | Creates an object with specified year, month, day, hour, and minute |
| `GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute, int second)` | Creates an object with specified
YouTip