In this article, we will learn how to convert Date to LocalTime using newly introduced method in Java 1.9 version
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 Date to LocalTime in Java 1.9 version :
- First, convert Date to an Instant using toInstant() method of Date class
- In Java 1.9 version a new method LocalTime.ofInstant() introduced for conversion from an Instant to LocalTime,
- LocalTime.ofInstant(Instant, ZoneId) – gets an instance of LocalTime from an Instant and zone ID passed
ConvertJavaUtilDateToLocalTime.java
package in.bench.resources.java9.conversion;
import java.time.Instant;
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 -> LocalTime
LocalTime localTime = LocalTime
.ofInstant(instant, zoneId);
System.out.print("\nLocalTime is :- \n"
+ localTime);
}
}
Output:
Current Date/time is :-
Thu Aug 25 09:38:34 IST 2022
Default time-zone is :-
Asia/Calcutta
Instant is :-
2022-08-25T04:08:34.908Z
LocalTime is :-
09:38:34.908
2. Convert LocalTime to 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 information is required
- Date.from() method accepts Instant as input-argument
- First, convert LocalTime to LocalDateTime by adding/combining LocalDate using atDate() method of LocalTime
- Second, convert LocalDateTime to ZonedDateTime by adding/combining Zone information using atZone() method of LocalDateTime
- Third, convert ZonedDateTime to Instant using toInstant() method of ZonedDateTime which returns an Instant
- Finally, 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 remain 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.java9.conversion;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Month;
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.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 -> 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/time is :- \n" + date);
}
}
Output:
Current system time is :-
09:55:16.088092700
Current system date is :-
2022-08-26
Default time-zone is :-
Asia/Calcutta
Current Instant at UTC/GMT is :-
2022-08-26T04:25:16.088092700Z
Converted Date/time is :-
Fri Aug 26 09:55:16 IST 2022
Related Articles:
- Java 8 – How to convert java.util.Date to LocalDate and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalTime and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDateTime and vice-versa ?
- Java 8 – How to convert java.util.Date to ZonedDateTime and vice-versa ?
- Java 8 – How to convert java.util.Date to OffsetDateTime and vice-versa ?
- Java 8 – How to convert java.util.Date to Instant and vice-versa ?
- Java 8 – How to convert java.util.Date to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert java.util.Date to Calendar and vice-versa ?
- Java 8 – How to convert java.util.Date to GregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to XMLGregorianCalendar and vice-versa ?
- Java 9 – How to convert java.util.Date to LocalDate using ofInstant() method ?
- Java 9 – How to convert java.util.Date to LocalTime using ofInstant() method ?
- Java – How to convert java.util.Date to String in different formats ?
- Java – How to convert String to java.util.Date in different formats ?
- Java – How to get Date/time with AM/PM marker and Zone ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/9/docs/api/java/time/LocalTime.html
- https://docs.oracle.com/javase/9/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/9/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/9/docs/api/java/time/ZoneId.html
- https://docs.oracle.com/javase/9/docs/api/java/time/Month.html
- https://docs.oracle.com/javase/9/docs/api/java/util/Date.html
Happy Coding !!
Happy Learning !!