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
- from(Instant) – This static method obtains an instance of
Date
from anInstant
object - toInstant() – This method converts invoking
Date
object to anInstant
- from(Instant) – This static method obtains an instance of
- 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
- Convert LocalTime to LocalDateTime using atDate() method passing LocalDate as argument
- And then invoke atZone() method on LocalDateTime instance passing ZoneId as argument which returns ZonedDateTime
- And then invoke toInstant() method on ZonedDateTime instance which returns an Instant
- Now, pass above converted Instant to Date.from() method which will return java.util.Date
- 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,
- Date part added as today’s date
- Adding System default Zone information
- 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 –
- Convert Calendar to Date using getTime() method
- And then invoke toInstant() method for Date to Instant conversion
- And then invoke atZone() method passing ZoneId as argument for Instant to ZonedDateTime conversion
- 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:
- Java 8 – LocalTime with method details and examples
- Java 8 – How to get Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to form LocalTime passing Hour, Minute and Second fields ?
- Java 8 – How to parse LocalTime in String form ?
- Java 8 – How to convert String to LocalTime ?
- Java 8 – How to convert LocalTime to String ?
- Java 8 – How to convert LocalTime in different formats ?
- Java 8 – How to convert LocalTime in different Format Style ?
- Java 8 – How to convert LocalTime to LocalDateTime ?
- Java 8 – How to convert LocalTime to ZonedDateTime ?
- Java 8 – How to convert LocalTime to an OffsetDateTime ?
- Java 8 – How to convert LocalTime to an Instant ?
- Java 8 – How to convert LocalTime to an OffsetTime ?
- Java 8 – How to convert LocalTime to Seconds and vice-versa ?
- Java 8 – How to convert LocalTime to Nanoseconds and vice-versa ?
- Java 8 – How to convert LocalTime to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalTime to Calendar and vice-versa ?
- Java 8 – How to convert LocalTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalTime in different ways ?
- Java 8 – How to add Hour, Minute and Second fields to LocalTime ?
- Java 8 – How to subtract Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to alter Hour, Minute and Second fields of LocalTime ?
- Java 8 – How to check whether a LocalTime is Before another LocalTime ?
- Java 8 – How to check whether a LocalTime is After another LocalTime ?
- Java 8 – How to compare two LocalTime instances ?
- Java 8 – How to find time duration between two LocalTime instances ?
- Java 8 – What are all the Temporal Fields supported by LocalTime ?
- Java 8 – What are all the Temporal Units supported by LocalTime ?
- Java 9 – Find difference between two LocalTime instances upto nanosecond precision ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html
- https://docs.oracle.com/javase/8/docs/api/java/sql/Timestamp.html
Happy Coding !!
Happy Learning !!