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

In this article, we will learn how to convert LocalTime to Calendar using atDate() method of LocalTime provided in Java 1.8 version and vice-versa

LocalTime to Calendar conversion & vice-versa :

Direct conversion from LocalTime to Calendar isn’t possible and hence convert LocalTime to java.util.Date and then to Calendar using getInstance() & setTime() methods

  • There are 2 new methods introduced in Java 1.8 version for Date class, those are
    1. from(Instant) – This static method obtains an instance of Date from an Instant object
    2. toInstant() – This method converts invoking Date object to an Instant
  • Note: Many legacy methods of Date class are deprecated

1. Convert LocalTime to Calendar :

  • LocalTime consist of only Time information whereas Calendar requires Date, Time and Zone information
    • Calendar = Date + LocalTime + Zone information
    • For LocalTime to Calendar conversion, Time-Zone and Date information is required
  • Date.from() method accepts Instant as input-argument
    1. Convert LocalTime to LocalDateTime using atDate() method passing LocalDate as argument
    2. And then invoke atZone() method on LocalDateTime instance passing ZoneId as argument which returns ZonedDateTime
    3. And then invoke toInstant() method on ZonedDateTime instance which returns an Instant
    4. Now, pass above converted Instant to Date.from() method which will return java.util.Date
    5. For converting java.util.Date to Calendar, get an instance of Calendar using getInstance() method and invoke setTime() method passing converted Date object as argument
  • Converted Calendar 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 -> Instant -> java.util.Date -> Calendar
  • Lets see an example for conversion of LocalTime to Calendar in the below illustration

ConvertLocalTimeToJavaUtilCalendar.java

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

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

public class ConvertLocalTimeToJavaUtilCalendar {

	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.now();
		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 -> Instant
		Instant instant = localTime // LocalTime
				.atDate(localDate) // LocalDateTime
				.atZone(zoneId) // ZonedDateTime
				.toInstant(); // Instant
		System.out.println("\nCurrent Instant at UTC/GMT is :- \n" 
				+ instant);


		// 5. convert to Date
		Date date = Date.from(instant);
		System.out.println("\nConverted Date/time is :- \n" + date);


		// 6. convert Date to Calendar
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		System.out.print("\nCalendar date/time is :- \n" + calendar);
	}
}

Output:

Current system time is :- 
20:02:14.322983600

Current system date is :- 
2022-08-25

Default time-zone is :- 
Asia/Calcutta

Current Instant at UTC/GMT is :- 
2022-08-25T14:32:14.322983600Z

Converted Date/time is :- 
Thu Aug 25 20:02:14 IST 2022

Calendar date/time is :- 
java.util.GregorianCalendar[time=1661437934322,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=2,
SECOND=14,MILLISECOND=322,ZONE_OFFSET=19800000,DST_OFFSET=0]

2. Convert Calendar to LocalTime :

  • Get Calendar object using getInstance() method of Calendar for conversion to LocalTime
  • Conversion steps
    1. Convert Calendar to Date using getTime() method
    2. And then invoke toInstant() method for Date to Instant conversion
    3. And then invoke atZone() method passing ZoneId as argument for Instant to ZonedDateTime conversion
    4. And then invoke toLocalTime() method which returns LocalTime
  • In short, Calendar -> java.util.Date -> Instant -> ZonedDateTime -> LocalTime
  • Lets see an example for conversion of Calendar to LocalTime in the below illustration

ConvertJavaUtilCalendarToLocalTime.java

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

import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

public class ConvertJavaUtilCalendarToLocalTime {

	public static void main(String[] args) {

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


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


		// 3. convert java.util.Calendar -> Date
		Date date = calendar.getTime();
		System.out.println("\nCurrent Date/time is :- \n" + date);


		// 4. convert java.util.Calendar -> Date -> LocalTime
		LocalTime localTime = date // Date
				.toInstant() // Instant
				.atZone(zoneId) // ZonedDateTime
				.toLocalTime(); // LocalTime
		System.out.print("\nConversion of Calendar to LocalTime is :- \n"
				+ localTime);
	}
}

Output:

Calendar Date/time is :- 
java.util.GregorianCalendar[time=1661438016027,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=3,
SECOND=36,MILLISECOND=27,ZONE_OFFSET=19800000,DST_OFFSET=0]

Default System Zone is :- 
Asia/Calcutta

Current Date/time is :- 
Thu Aug 25 20:03:36 IST 2022

Conversion of Calendar to LocalTime is :- 
20:03:36.027

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert LocalTime to GregorianCalendar and vice-versa ?
Java 8 – How to convert LocalTime to java.sql.Timestamp and vice-versa ?