In this article, we will learn how to convert LocalTime to java.util.Date using different methods provided in Java 1.8 version and vice-versa
Date class in Java 1.8 version :
- 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 java.util.Date :
- LocalTime consist of only Time information whereas java.util.Date requires Date, Time and Zone information
- java.util.Date = Date + LocalTime + Zone information
- For LocalTime to java.util.Date conversion, Time-Zone and Date informations is required
- Date.from() method accepts Instant as input–argument
- Convert LocalTime to LocalDateTime using atDate() method passing LocalDate as argument
- And then convert LocalDateTime to ZonedDateTime using atZone() method passing ZoneId as argument and then invoke toInstant() method which returns an Instant
- Now, pass above converted Instant to Date.from() method which will return java.util.Date
- Converted java.util.Date 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
- Lets see an example for conversion of LocalTime to java.util.Date in the below illustration
ConvertLocalTimeToJavaUtilDate.java
package in.bench.resources.java8.date.examples;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
public class ConvertLocalTimeToJavaUtilDate {
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. finally convert to Date
Date date = Date.from(instant);
System.out.print("\nConverted date is :- \n" + date);
}
}
Output:
Current system time is :-
20:20:59.403629200
Current system date is :-
2022-08-23
Default time-zone is :-
Asia/Calcutta
Current Instant at UTC/GMT is :-
2022-08-23T14:50:59.403629200Z
Converted date is :-
Tue Aug 23 20:20:59 IST 2022
2. Convert java.util.Date to LocalTime :
- Get java.util.Date instantiating Date object for conversion to LocalTime
- Conversion steps –
- Convert Date to Instant using toInstant() method which returns an Instant
- And then invoke ofInstant() method passing converted Instant & ZoneId as arguments which returns LocalDateTime
- And then invoke toLocalTime() method which returns LocalTime
- In short, java.util.Date -> Instant -> LocalDateTime -> LocalTime
- Read Java 8 – How to convert java.util.Date to LocalTime in different ways ?
- Lets see an example for conversion of java.util.Date to LocalTime in the below illustration
ConvertJavaUtilDateToLocalTime.java
package in.bench.resources.java8.date.examples;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
public class ConvertJavaUtilDateToLocalTime {
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. get default time zone
ZoneId zoneId = ZoneId.systemDefault();
System.out.println("\nDefault time-zone is :- \n"
+ zoneId);
// 3. First, convert Date to Instant
Instant instant = date.toInstant();
System.out.println("\nInstant is :- \n"
+ instant);
// 4. Second, Instant -> LocalDateTime
LocalDateTime localDateTime = LocalDateTime
.ofInstant(instant, zoneId);
System.out.println("\nLocalDateTime is :- \n"
+ localDateTime);
// 5. Finally convert, Date -> Instant -> LocalDateTime -> LocalTime
LocalTime localTime = localDateTime.toLocalTime();
System.out.print("\nLocalTime is :- \n" + localTime);
}
}
Output:
Current Date/time is :-
Tue Aug 23 20:23:17 IST 2022
Default time-zone is :-
Asia/Calcutta
Instant is :-
2022-08-23T14:53:17.928Z
LocalDateTime is :-
2022-08-23T20:23:17.928
LocalTime is :-
20:23:17.928
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/LocalDateTime.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 !!