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

In this article, we will learn how to convert LocalTime to GregorianCalendar using different methods provided in Java 1.8 version and vice-versa

LocalTime to GregorianCalendar conversion & vice-versa :

  • There are 3 new methods introduced in Java 1.8 version for GregorianCalendar class, those are
    1. from(ZonedDateTime) – This static method obtains an instance of GregorianCalendar with the default locale from a ZonedDateTime object
    2. toZonedDateTime() – This method converts invoking GregorianCalendar object to a ZonedDateTime that represents the same point on the time-line as that of GregorianCalendar object
    3. getCalendarType() – Returns "gregory" as the calendar type

1. Convert LocalTime to GregorianCalendar :

  • LocalTime consist of only Time information whereas GregorianCalendar requires Date, Time and Zone information
    • GregorianCalendar = Date + LocalTime + Zone information
    • For LocalTime to GregorianCalendar conversion, Time-Zone and Time information is required
  • GregorianCalendar.from() method accepts ZonedDateTime as input-argument and returns GregorianCalendar
  • Convert LocalTime to ZonedDateTime
    • First, convert LocalTime to LocalDateTime using atDate() method passing LocalDate as argument
    • Second, convert LocalDateTime to ZonedDateTime using atZone() method passing ZoneId as argument
    • Finally, pass converted ZonedDateTime to GregorianCalendar.from() method which returns GregorianCalendar
  • Converted GregorianCalendar will have,
    1. Date part added as today’s date
    2. Adding System default Zone information
    3. Time part same as that of LocalTime
  • In short, LocalTime -> LocalDateTime -> ZonedDateTime -> GregorianCalendar
  • Lets see an example for conversion of LocalTime to GregorianCalendar in the below illustration

ConvertLocalTimeToGregorianCalendar.java

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

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.GregorianCalendar;

public class ConvertLocalTimeToGregorianCalendar {

	public static void main(String[] args) {

		// 1. get current system time
		LocalTime localTime = LocalTime.now();
		System.out.println("Current system time is :- \n" 
				+ localTime);


		// 2. get current system date
		LocalDate localDate = LocalDate.of(2022, Month.AUGUST, 26);
		System.out.println("\nCurrent system date is :- \n" 
				+ localDate);


		// 3. get default time zone
		ZoneId zoneId = ZoneId.systemDefault();
		System.out.println("\nDefault time-zone is :- \n" 
				+ zoneId);


		// 4. convert LocalTime -> LocalDateTime -> ZonedDateTime
		ZonedDateTime zonedDateTime = localTime // LocalTime
				.atDate(localDate) // LocalDateTime
				.atZone(zoneId); // ZonedDateTime
		System.out.println("\nZonedDateTime is :- \n" 
				+ zonedDateTime);


		// 5. convert LocalTime -> LocalDateTime -> ZonedDateTime -> GregorianCalendar 
		GregorianCalendar gregorianCalendar = GregorianCalendar.from(zonedDateTime);
		System.out.println("\nConversion of LocalTime to GregorianCalendar is :- \n"
				+ gregorianCalendar	);


		// 6. print date/time
		System.out.print("\nDate/time is :- \n" + gregorianCalendar.getTime());
	}
}

Output:

Current system time is :- 
20:18:16.161348900

Current system date is :- 
2022-08-26

Default time-zone is :- 
Asia/Calcutta

ZonedDateTime is :- 
2022-08-26T20:18:16.161348900+05:30[Asia/Calcutta]

Conversion of LocalTime to GregorianCalendar is :- 
java.util.GregorianCalendar[time=1661525296161,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=2,
minimalDaysInFirstWeek=4,ERA=1,YEAR=2022,MONTH=7,WEEK_OF_YEAR=34,
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=18,
SECOND=16,MILLISECOND=161,ZONE_OFFSET=19800000,DST_OFFSET=0]

Date/time is :- 
Fri Aug 26 20:18:16 IST 2022

2. Convert GregorianCalendar to LocalTime :

  • Instantiate GregorianCalendar object for conversion to LocalTime
  • Conversion steps
    1. Convert GregorianCalendar to ZonedDateTime using toZonedDateTime() method
    2. And then invoke toLocalTime() method which returns LocalTime
  • In short, GregorianCalendar -> ZonedDateTime -> LocalTime
  • Lets see an example for conversion of GregorianCalendar to LocalTime in the below illustration

ConvertGregorianCalendarToLocalTime.java

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

import java.time.LocalTime;
import java.time.ZoneId;
import java.util.GregorianCalendar;

public class ConvertGregorianCalendarToLocalTime {

	public static void main(String[] args) {

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


		// 2. print date/time from GregorianCalendar
		System.out.println("\nCurrent Date/time is :- \n" 
				+ gregorianCalendar.getTime());


		// 3. get system default zone
		ZoneId zoneId = ZoneId.systemDefault();
		System.out.println("\nDefault System Zone is :- \n" 
				+ zoneId);


		// 4. convert GregorianCalendar -> ZonedDateTime -> LocalTime
		LocalTime localTime = gregorianCalendar
				.toZonedDateTime()
				.toLocalTime();
		System.out.print("\nConversion of GregorianCalendar to LocalTime is :- \n"
				+ localTime);
	}
}

Output:

GregorianCalendar is :- 
java.util.GregorianCalendar[time=1661438967154,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=25,DAY_OF_YEAR=237,DAY_OF_WEEK=5,
DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=8,HOUR_OF_DAY=20,MINUTE=19,
SECOND=27,MILLISECOND=154,ZONE_OFFSET=19800000,DST_OFFSET=0]

Current Date/time is :- 
Thu Aug 25 20:19:27 IST 2022

Default System Zone is :- 
Asia/Calcutta

Conversion of GregorianCalendar to LocalTime is :- 
20:19:27.154

Related Articles:

References:

Happy Coding !!
Happy Learning !!

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