Java GregorianCalendar Example
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.
Contents
Get Current Time
To get current time we can use following constructors.GregorianCalendar() GregorianCalendar(Locale aLocale) GregorianCalendar(TimeZone zone) GregorianCalendar(TimeZone zone, Locale locale)
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)
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());
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 convertGregorianCalendar
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);
Fri Sep 24 09:15:59 IST 2021
roll
Method
1.
public void roll(int field, boolean up)
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());
Fri Sep 10 00:00:00 IST 2021 Sun Oct 10 00:00:00 IST 2021 Sat Oct 10 00:00:00 IST 2020
public void roll(int field, int amount)
GregorianCalendar gc = new GregorianCalendar(2021, 8, 10); System.out.println(gc.getTime()); gc.roll(Calendar.MONTH, 5); System.out.println(gc.getTime());
Fri Sep 10 00:00:00 IST 2021 Wed Feb 10 00:00:00 IST 2021
Set Date
1.void setWeekDate(int weekYear, int weekOfYear, int dayOfWeek)
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());
Mon Apr 06 09:25:43 IST 2020
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)
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 staticfrom
method of GregorianCalendar
.
static GregorianCalendar from(ZonedDateTime zdt)
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());
Thu Sep 23 18:17:20 IST 2021
Convert to ZonedDateTime
Find theGregorianCalendar
method.
ZonedDateTime toZonedDateTime()
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);
2021-09-26T10:07:50+05:30[Asia/Calcutta]
Format Date
ConvertGregorianCalendar
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);
26 September, 2021 2021-Sep-26 10:07:50
Compare Times
Compare Gregorian calendars usingCalendar.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 convertGregorianCalendar
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); } }
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 theGregorianCalendar
methods.
void setGregorianChange(Date date) final Date getGregorianChange()
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());
Wed May 15 00:00:00 IST 1560
Get GregorianCalendar Informations
1.int getActualMaximum(int field)
GregorianCalendar gc = new GregorianCalendar(2021, 8, 26, 10, 7, 50); System.out.println(gc.getActualMaximum(Calendar.MONTH)); // 11
int getActualMinimum(int field)
GregorianCalendar gc = new GregorianCalendar(2021, 8, 26, 10, 7, 50); System.out.println(gc.getActualMinimum(Calendar.MONTH)); // 0
int getGreatestMinimum(int field)
GregorianCalendar
instance.
GregorianCalendar gc = new GregorianCalendar(2021, 8, 26, 10, 7, 50); System.out.println(gc.getGreatestMinimum(Calendar.MONTH)); // 0
int getLeastMaximum(int field)
GregorianCalendar
instance.
GregorianCalendar gc = new GregorianCalendar(2021, 8, 26, 10, 7, 50); System.out.println(gc.getLeastMaximum(Calendar.MONTH)); // 11
int getMaximum(int field)
GregorianCalendar
instance.
GregorianCalendar gc = new GregorianCalendar(2021, 8, 26, 10, 7, 50); System.out.println(gc.getMaximum(Calendar.MONTH)); // 11
int getMinimum(int field)
GregorianCalendar
instance.
GregorianCalendar gc = new GregorianCalendar(2021, 8, 26, 10, 7, 50); System.out.println(gc.getMinimum(Calendar.MONTH)); // 0
int getWeekYear()
GregorianCalendar
.
GregorianCalendar gc = new GregorianCalendar(2021, 8, 26, 10, 7, 50); System.out.println(gc.getWeekYear()); // 2021
boolean isLeapYear(int year)
GregorianCalendar gc = new GregorianCalendar(2021, 8, 26, 10, 7, 50); System.out.println(gc.isLeapYear(2021)); // false
final boolean isWeekDateSupported()
GregorianCalendar
supports week dates.
GregorianCalendar gc = new GregorianCalendar(2021, 8, 26, 10, 7, 50); System.out.println(gc.isWeekDateSupported()); // true
String getCalendarType()
GregorianCalendar gc = new GregorianCalendar(2021, 8, 26, 10, 7, 50); System.out.println(gc.getCalendarType()); // Gregory
static final int AD
12.
static final int BC