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

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

Date to Calendar conversion & vice-versa :

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

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

1. Convert Date to Calendar :

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

ConvertJavaUtilDateToCalendar.java

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

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

public class ConvertJavaUtilDateToCalendar {

	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 Calendar
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		System.out.println("\nCalendar is :- \n" 
				+ calendar);


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


		// 3.1 DateFormatter 1
		DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
		String formattedTimestampInStr1 = dateFormat.format(calendar.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(calendar.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(calendar.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 :- 
Fri Aug 26 20:09:28 IST 2022

Calendar is :- 
java.util.GregorianCalendar[time=1661524768415,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=35,
WEEK_OF_MONTH=4,DAY_OF_MONTH=26,DAY_OF_YEAR=238,DAY_OF_WEEK=6,
DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=8,HOUR_OF_DAY=20,MINUTE=9,
SECOND=28,MILLISECOND=415,ZONE_OFFSET=19800000,DST_OFFSET=0]

Calendar Date/time is :- 
Fri Aug 26 20:09:28 IST 2022

Formatted Timestamp in (dd-MM-yyyy HH:mm:ss) format is :- 
26-08-2022 20:09:28

Formatted Timestamp in (dd/MMM/yy HH:mm) format is :- 
26/Aug/22 20:09

Formatted Timestamp in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :- 
Friday 26 August, 2022 08:09:28 pm IST

2. Convert Calendar to Date :

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

ConvertCalendarToJavaUtilDate.java

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

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

public class ConvertCalendarToJavaUtilDate {

	public static void main(String[] args) {

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


		// 2. convert Calendar to Date
		Date date = calendar.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:

Calendar is :- 
java.util.GregorianCalendar[time=1661524804370,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=35,
WEEK_OF_MONTH=4,DAY_OF_MONTH=26,DAY_OF_YEAR=238,DAY_OF_WEEK=6,
DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=8,HOUR_OF_DAY=20,MINUTE=10,
SECOND=4,MILLISECOND=370,ZONE_OFFSET=19800000,DST_OFFSET=0]

Converted Date/time is :- 
Fri Aug 26 20:10:04 IST 2022

Formatted Date in (dd-MM-yyyy HH:mm:ss) format is :- 
26-08-2022 20:10:04

Formatted Date in (dd/MMM/yy HH:mm) format is :- 
26/Aug/22 20:10

Formatted Date in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :- 
Friday 26 August, 2022 08:10:04 pm IST

Related Articles:

References:

Happy Coding !!
Happy Learning !!

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