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

In this article, we will learn how to convert Date to GregorianCalendar and viceversa in Java

Date to GregorianCalendar conversion & vice-versa :

It is very simple and easy to convert Date to GregorianCalendar and viceversa using setTime() & getTime() methods –

  • Convert Date to GregorianCalendar using setTime() method
    • First, get Date object
    • And then instantiate GregorianCalendar & pass date object in setTime() method of GregorianCalendar
  • Convert GregorianCalendar to Date using getTime() method
    • First, instantiate GregorianCalendar
    • And then invoke getTime() method

1. Convert Date to GregorianCalendar :

  • Instantiate Date class which will return current Date/time/zone in (E MMM dd HH:mm:ss z yyyy) format
  • For Date to GregorianCalendar conversion, instantiate GregorianCalendar object and then invoke setTime() method passing date object obtained in the above step
  • Finally, print GregorianCalendar/Calendar/Date in different formats using DateFormat/SimpleDateFormat classes

ConvertJavaUtilDateToGregorianCalendar.java

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

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

public class ConvertJavaUtilDateToGregorianCalendar {

	public static void main(String[] args) {

		// 1. get current date/time using java.util.Date
		Date date = new Date();
		System.out.println("Current Date/time is :- \n" 
				+ date);


		// 2. convert Date to GregorianCalendar
		GregorianCalendar gregorianCalendar = new GregorianCalendar();
		gregorianCalendar.setTime(date);
		System.out.println("\nGregorianCalendar is :- \n" 
				+ gregorianCalendar);


		// 3. print date/time from Calendar
		System.out.println("\nGregorianCalendar Date/time is :- \n" 
				+ gregorianCalendar.getTime());


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


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


		// 3.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 Timestamp in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :- \n" 
				+ formattedTimestampInStr3);
	}
}

Output:

Current Date/time is :- 
Mon Aug 29 13:43:06 IST 2022

GregorianCalendar is :- 
java.util.GregorianCalendar[time=1661760786324,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=29,DAY_OF_YEAR=241,DAY_OF_WEEK=2,
DAY_OF_WEEK_IN_MONTH=5,AM_PM=1,HOUR=1,HOUR_OF_DAY=13,MINUTE=43,
SECOND=6,MILLISECOND=324,ZONE_OFFSET=19800000,DST_OFFSET=0]

GregorianCalendar Date/time is :- 
Mon Aug 29 13:43:06 IST 2022

Formatted Timestamp in (dd-MM-yyyy HH:mm:ss) format is :- 
29-08-2022 13:43:06

Formatted Timestamp in (dd/MMM/yy HH:mm) format is :- 
29/Aug/22 13:43

Formatted Timestamp in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :- 
Monday 29 August, 2022 01:43:06 pm IST

2. Convert GregorianCalendar to Date :

  • First, instantiate GregorianCalendar object
  • For GregorianCalendar to Date conversion, invoke getTime() method on GregorianCalendar object which will return current date/time in (E MMM dd HH:mm:ss z yyyy) format
  • Finally, print GregorianCalendar/Calendar/Date in different formats using DateFormat/SimpleDateFormat classes

ConvertGregorianCalendarToJavaUtilDate.java

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

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

public class ConvertGregorianCalendarToJavaUtilDate {

	public static void main(String[] args) {

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


		// 2. convert GregorianCalendar to Date
		Date date = gregorianCalendar.getTime();
		System.out.println("\nConverted Date/time is :- \n" 
				+ date);	


		// 2.1 DateFormatter 1
		DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
		String formattedTimestampInStr1 = dateFormat.format(date);
		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(date);
		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(date);
		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=1661760823810,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=29,DAY_OF_YEAR=241,DAY_OF_WEEK=2,
DAY_OF_WEEK_IN_MONTH=5,AM_PM=1,HOUR=1,HOUR_OF_DAY=13,MINUTE=43,
SECOND=43,MILLISECOND=810,ZONE_OFFSET=19800000,DST_OFFSET=0]

Converted Date/time is :- 
Mon Aug 29 13:43:43 IST 2022

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

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

Formatted Date in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :- 
Monday 29 August, 2022 01:43:43 pm IST

Related Articles:

References:

Happy Coding !!
Happy Learning !!

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