Java 8 – How to convert Calendar to GregorianCalendar and vice-versa ?

In this article, we will learn how to convert Calendar to GregorianCalendar and vice-versa in Java

Calendar to GregorianCalendar conversion & vice-versa:

It is very simple and easy to convert Calendar to GregorianCalendar and viceversa using casting & assigning

  • Convert Calendar to GregorianCalendar using downcasting
    • First, get Calendar instance
    • And then assign calendar to GregorianCalendar by downcasting
  • Convert GregorianCalendar to Calendar using simple assigning or upcasting
    • First, instantiate GregorianCalendar object
    • And then assign GregorianCalendar to calendar by upcasting

1. Convert Calendar to GregorianCalendar :

  • First, get Calendar instance using getInstance() method of Calendar
  • For Calendar to GregorianCalendar conversion,
    • Simply assign Calendar instance to GregorianCalendar object using downcasting
  • Note: GregorianCalendar is a sub-class of Calendar class
  • Finally, print GregorianCalendar/Date in different formats using DateFormat/SimpleDateFormat classes

ConvertCalendarToGregorianCalendar.java

package in.bench.resources.java8.date.examples;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class ConvertCalendarToGregorianCalendar {

	public static void main(String[] args) {

		// 1. get Calendar instance
		Calendar calendar = Calendar.getInstance();
		System.out.println("Calendar is :- \n" 
				+ calendar);


		// 2. cast Calendar instance to GregorianCalendar object
		GregorianCalendar gregorianCalendar = (GregorianCalendar) calendar;
		System.out.println("\nGregorianCalendar is :- \n" 
				+ gregorianCalendar);


		// 2.1 DateFormatter 1
		DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
		String formattedTimestampInStr1 = dateFormat.format(gregorianCalendar.getTime());
		System.out.println("\nFormatted Date in (dd-MM-yyyy HH:mm:ss) format is :- \n" 
				+ formattedTimestampInStr1);


		// 2.2 DateFormatter 1
		dateFormat = new SimpleDateFormat("dd/MMM/yy HH:mm");
		String formattedTimestampInStr2 = dateFormat.format(gregorianCalendar.getTime());
		System.out.println("\nFormatted Date in (dd/MMM/yy HH:mm) format is :- \n" 
				+ formattedTimestampInStr2);


		// 2.3 DateFormatter 1
		dateFormat = new SimpleDateFormat("EEEE dd MMMM, yyyy hh:mm:ss a z");
		String formattedTimestampInStr3 = dateFormat.format(gregorianCalendar.getTime());
		System.out.print("\nFormatted Date in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :- \n" 
				+ formattedTimestampInStr3);
	}
}

Output:

Calendar is :- 
java.util.GregorianCalendar[time=1661847908395,areFieldsSet=true,areAllFieldsSet=true,
lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,
dstSavings=0,useDaylight=false,transitions=7,lastRule=null],firstDayOfWeek=1,
minimalDaysInFirstWeek=1,ERA=1,YEAR=2022,MONTH=7,WEEK_OF_YEAR=36,
WEEK_OF_MONTH=5,DAY_OF_MONTH=30,DAY_OF_YEAR=242,DAY_OF_WEEK=3,
DAY_OF_WEEK_IN_MONTH=5,AM_PM=1,HOUR=1,HOUR_OF_DAY=13,MINUTE=55,
SECOND=8,MILLISECOND=395,ZONE_OFFSET=19800000,DST_OFFSET=0]

GregorianCalendar is :- 
java.util.GregorianCalendar[time=1661847908395,areFieldsSet=true,areAllFieldsSet=true,
lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,
dstSavings=0,useDaylight=false,transitions=7,lastRule=null],firstDayOfWeek=1,
minimalDaysInFirstWeek=1,ERA=1,YEAR=2022,MONTH=7,WEEK_OF_YEAR=36,
WEEK_OF_MONTH=5,DAY_OF_MONTH=30,DAY_OF_YEAR=242,DAY_OF_WEEK=3,
DAY_OF_WEEK_IN_MONTH=5,AM_PM=1,HOUR=1,HOUR_OF_DAY=13,MINUTE=55,
SECOND=8,MILLISECOND=395,ZONE_OFFSET=19800000,DST_OFFSET=0]

Formatted Date in (dd-MM-yyyy HH:mm:ss) format is :- 
30-08-2022 13:55:08

Formatted Date in (dd/MMM/yy HH:mm) format is :- 
30/Aug/22 13:55

Formatted Date in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :- 
Tuesday 30 August, 2022 01:55:08 pm IST

2. Convert GregorianCalendar to Calendar :

  • First, instantiate GregorianCalendar object
  • For GregorianCalendar to Calendar conversion,
    • Simply assign GregorianCalendar object to Calendar instance using upcasting
  • Note: Calendar is a super-class of GregorianCalendar class
  • Finally, print Calendar/Date in different formats using DateFormat/SimpleDateFormat classes

ConvertGregorianCalendarToCalendar.java

package in.bench.resources.java8.date.examples;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class ConvertGregorianCalendarToCalendar {

	public static void main(String[] args) {

		// 1. get GregorianCalendar object
		GregorianCalendar gregorianCalendar = new GregorianCalendar();
		System.out.println("GregorianCalendar is :- \n" +
				gregorianCalendar);


		// 2. assign GregorianCalendar -> Calendar
		Calendar calendar = gregorianCalendar;
		System.out.println("\nCalendar is :- \n" 
				+ calendar);


		// 2.1 DateFormatter 1
		DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
		String formattedTimestampInStr1 = dateFormat.format(calendar.getTime());
		System.out.println("\nFormatted Date in (dd-MM-yyyy HH:mm:ss) format is :- \n" 
				+ formattedTimestampInStr1);


		// 2.2 DateFormatter 1
		dateFormat = new SimpleDateFormat("dd/MMM/yy HH:mm");
		String formattedTimestampInStr2 = dateFormat.format(calendar.getTime());
		System.out.println("\nFormatted Date in (dd/MMM/yy HH:mm) format is :- \n" 
				+ formattedTimestampInStr2);


		// 2.3 DateFormatter 1
		dateFormat = new SimpleDateFormat("EEEE dd MMMM, yyyy hh:mm:ss a z");
		String formattedTimestampInStr3 = dateFormat.format(calendar.getTime());
		System.out.print("\nFormatted Date in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :- \n" 
				+ formattedTimestampInStr3);
	}
}

Output:

GregorianCalendar is :- 
java.util.GregorianCalendar[time=1661847979104,areFieldsSet=true,areAllFieldsSet=true,
lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,
dstSavings=0,useDaylight=false,transitions=7,lastRule=null],firstDayOfWeek=1,
minimalDaysInFirstWeek=1,ERA=1,YEAR=2022,MONTH=7,WEEK_OF_YEAR=36,
WEEK_OF_MONTH=5,DAY_OF_MONTH=30,DAY_OF_YEAR=242,DAY_OF_WEEK=3,
DAY_OF_WEEK_IN_MONTH=5,AM_PM=1,HOUR=1,HOUR_OF_DAY=13,MINUTE=56,
SECOND=19,MILLISECOND=104,ZONE_OFFSET=19800000,DST_OFFSET=0]

Calendar is :- 
java.util.GregorianCalendar[time=1661847979104,areFieldsSet=true,areAllFieldsSet=true,
lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,
dstSavings=0,useDaylight=false,transitions=7,lastRule=null],firstDayOfWeek=1,
minimalDaysInFirstWeek=1,ERA=1,YEAR=2022,MONTH=7,WEEK_OF_YEAR=36,
WEEK_OF_MONTH=5,DAY_OF_MONTH=30,DAY_OF_YEAR=242,DAY_OF_WEEK=3,
DAY_OF_WEEK_IN_MONTH=5,AM_PM=1,HOUR=1,HOUR_OF_DAY=13,MINUTE=56,
SECOND=19,MILLISECOND=104,ZONE_OFFSET=19800000,DST_OFFSET=0]

Formatted Date in (dd-MM-yyyy HH:mm:ss) format is :- 
30-08-2022 13:56:19

Formatted Date in (dd/MMM/yy HH:mm) format is :- 
30/Aug/22 13:56

Formatted Date in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :- 
Tuesday 30 August, 2022 01:56:19 pm IST

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert Calendar to XMLGregorianCalendar and vice-versa ?
Java 8 – How to convert Timestamp to XMLGregorianCalendar and vice-versa ?