In this article, we will learn how to convert an Date to LocalDate 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 LocalDate 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 LocalDate.ofInstant() introduced for conversion from an Instant to LocalDate,
- LocalDate.ofInstant(Instant, ZoneId) – gets an instance of LocalDate from an Instant and zone ID passed
ConvertJavaUtilDateToLocalDate.java
package in.bench.resources.java9.conversion;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
public class ConvertJavaUtilDateToLocalDate {
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 -> LocalDate
LocalDate localDate = LocalDate
.ofInstant(instant, zoneId);
System.out.print("\nLocalDate is :- \n"
+ localDate);
}
}
Output:
Current Date/time is :-
Wed Aug 24 16:17:45 IST 2022
Default time-zone is :-
Asia/Calcutta
Instant is :-
2022-08-24T10:47:45.621Z
LocalDate is :-
2022-08-24
2. Convert LocalDate to Date :
- LocalDate consist of only Date information whereas java.util.Date requires Date, Time and Zone information
- java.util.Date = LocalDate + Time + Zone information
- For LocalDate to java.util.Date conversion, Time-Zone and Time information is required
- Date.from() method accepts Instant as input-argument
- Convert LocalDate to ZonedDateTime using atStartOfDay() 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 same as that of LocalDate
- Adding System default Zone information
- Time part fields hour/minute/second values set to 00
- In short, LocalDate -> ZonedDateTime -> Instant -> java.util.Date
- Lets see an example for conversion of LocalDate to java.util.Date in the below illustration
ConvertLocalDateToJavaUtilDate.java
package in.bench.resources.java9.conversion;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
public class ConvertLocalDateToJavaUtilDate {
public static void main(String[] args) {
// 1. get current System Date
LocalDate localDate = LocalDate.now();
System.out.println("Current System Date is :- \n"
+ localDate);
// 2. get system default zone
ZoneId zoneId = ZoneId.systemDefault();
System.out.println("\nDefault System Zone is :- \n"
+ zoneId);
// 3. First, convert LocalDate to ZonedDateTime
ZonedDateTime zonedDateTime = localDate.atStartOfDay(zoneId);
System.out.println("\nZonedDateTime is :- \n"
+ zonedDateTime);
// 4. Second, convert ZonedDateTime to Instant
Instant instant = zonedDateTime.toInstant();
System.out.println("\nInstant is :- \n"
+ instant);
// 5. LocalDate -> ZonedDateTime -> Date
Date date = Date.from(zonedDateTime.toInstant());
System.out.print("\nConversion of LocalDate to java.util.Date is :- \n"
+ date);
}
}
Output:
Current System Date is :-
2022-08-24
Default System Zone is :-
Asia/Calcutta
ZonedDateTime is :-
2022-08-24T00:00+05:30[Asia/Calcutta]
Instant is :-
2022-08-23T18:30:00Z
Conversion of LocalDate to java.util.Date is :-
Wed Aug 24 00:00:00 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/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 !!