Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java GregorianCalendar Class

Java GregorianCalendar Class

GregorianCalendar is a calendar class in Java that represents the Gregorian calendar; it inherits from the Calendar class. This class provides the standard calendar system, which is also the calendar system 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 takes into account complex calendar rules such as leap years.


Basic Usage of GregorianCalendar

Create a GregorianCalendar instance

There are several ways to create a GregorianCalendar instance:

Example

// Create an instance representing the current date and time

GregorianCalendar

calendar1

=

new

GregorianCalendar

(

)

;

// Create an instance with the specified year, month, and date

// Note: months start from 0, where 0 means 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

)

;

Get Date and Time Information

Example

GregorianCalendar

calendar

=

new

GregorianCalendar

(

)

;

int

year

=

calendar.

get

(

Calendar

.

YEAR

)

;

     

// Get 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 minute

int

second

=

calendar.

get

(

Calendar

.

SECOND

)

;

 

// Get second

Set Date and Time

Example

GregorianCalendar

calendar

=

new

GregorianCalendar

(

)

;

// Set the year

calendar.

set

(

Calendar

.

YEAR

,

2024

)

;

// Set the month (0-11)

calendar.

set

(

Calendar

.

MONTH

,

Calendar

.

JANUARY

)

;

// Set the day of month

calendar.

set

(

Calendar

.

DAY_OF_MONTH

,

1

)

;

// Set year, month, and day at once

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

)

;

// Set time (milliseconds since 00:00:00 GMT, January 1, 1970)

int

result

=

cal1.

compareTo

(

cal2

)

;

 

// cal1 before cal2 returns negative, after returns positive, equal returns 0

// Check whether it is before a certain date

boolean

isBefore

=

cal1.

before

(

cal2

)

;

// Check whether it is after a certain date

boolean

isAfter

=

cal1.

after

(

cal2

)

;

Determining Leap Years

Example

// Check whether the given year is a leap year

boolean

isLeapYear

=

new

GregorianCalendar

(

)

.

isLeapYear

(

2024

)

;

Convert to a Date Object

Example

GregorianCalendar

calendar

=

new

GregorianCalendar

(

)

;

Date

date

=

calendar.

getTime

(

)

;

Set from a 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 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: 0 means January and 11 means December. For 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 applied or a separate instance 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 and LocalDateTime, which have a more reasonable design and are 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 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 whether the current year is a leap year

       

int

year

=

calendar.

get

(

Calendar

.

YEAR

)

;

       

boolean

isLeap

=

calendar.

isLeapYear

(

year

)

;

       

System

.

out

.

println

(

year

"year"

(

isLeap

?

"Yes"

:

"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 of a specific date

       

GregorianCalendar

birthday

=

new

GregorianCalendar

(

1990

,

Calendar

.

JULY

,

15

)

;

       

System

.

out

.

println

(

"Birthday: "

sdf.

format

(

birthday.

getTime

(

)

)

)

;

   

}

}

The output 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. Its commonly used methods are as follows:

Constructor

Method Description
GregorianCalendar() Create an object using the default time zone and locale
GregorianCalendar(int year, int month, int dayOfMonth) Create an object with the specified year, month, and day
GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute) Create an object with the specified year, month, day, hour, and minute
GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute, int second) Create an object with the specified year, month, day, hour, minute, and second
GregorianCalendar(Locale locale) Create an object using the default time zone and a specified locale
GregorianCalendar(TimeZone zone) Create an object using a specified time zone and the default locale
GregorianCalendar(TimeZone zone, Locale locale) Create an object using a specified time zone and locale

Specific Methods

Method Description
boolean isLeapYear(int year) Determine whether the specified year is a leap year
void setGregorianChange(Date date) Set the conversion date from Julian to Gregorian calendar
Date getGregorianChange() Get the conversion date from Julian to Gregorian calendar

Common Methods Inherited from Calendar

Method Description
int get(int field) Get the value of a specified field
void set(int field, int value) Set the value of a specified field
void add(int field, int amount) Add or subtract a specified amount of time
void roll(int field, int amount) Add or subtract a time amount without changing larger fields
boolean after(Object when) Check whether it is after a specified time
boolean before(Object when) Check whether it is before a specified time
long getTimeInMillis() Return the time value (milliseconds)
void setTimeInMillis(long millis) Set the Calendar using a millisecond time value
Date getTime() Return the corresponding Date object
void setTime(Date date) Set the time using a Date

Field Constants (inherited from Calendar)

Field Description
YEAR Year field
MONTH Month field (0-11)
DATE / DAY_OF_MONTH Day of month
DAY_OF_WEEK Day of week (1-7, 1=Sunday)
HOUR Hour (12-hour)
HOUR_OF_DAY Hour (24-hour)
MINUTE Minute
SECOND Second
MILLISECOND Millisecond
AM_PM AM or PM